mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
feat: turn detection modify
This commit is contained in:
parent
62b53042d4
commit
c82787b7d7
6 changed files with 1443 additions and 27 deletions
|
|
@ -2,7 +2,7 @@ FROM nvidia/cuda:12.3.2-devel-ubuntu22.04
|
||||||
|
|
||||||
ENV DEBIAN_FRONTEND=noninteractive
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
|
|
||||||
# 基础依赖
|
# Install system dependencies
|
||||||
RUN apt update && apt install -y \
|
RUN apt update && apt install -y \
|
||||||
software-properties-common \
|
software-properties-common \
|
||||||
curl \
|
curl \
|
||||||
|
|
@ -12,25 +12,24 @@ RUN apt update && apt install -y \
|
||||||
&& apt install -y python3.12 python3.12-venv python3.12-dev \
|
&& apt install -y python3.12 python3.12-venv python3.12-dev \
|
||||||
&& apt clean && rm -rf /var/lib/apt/lists/*
|
&& apt clean && rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
# 安装 pip(自动带上 distutils)
|
# Install uv
|
||||||
RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
|
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
|
||||||
|
ENV PATH="/root/.local/bin:$PATH"
|
||||||
# 设置默认 python 和 pip
|
|
||||||
RUN ln -s /usr/bin/python3.12 /usr/bin/python && ln -s /usr/local/bin/pip /usr/bin/pip
|
|
||||||
|
|
||||||
WORKDIR /workspace/
|
WORKDIR /workspace/
|
||||||
EXPOSE 8080
|
EXPOSE 18000
|
||||||
|
|
||||||
|
# Set timezone
|
||||||
#设置时区timedatectl status|| /etc/timezone
|
|
||||||
ENV TimeZone=Etc/UTC
|
ENV TimeZone=Etc/UTC
|
||||||
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone
|
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone
|
||||||
|
|
||||||
|
# Copy project files
|
||||||
|
COPY pyproject.toml uv.lock* /workspace/
|
||||||
|
|
||||||
COPY requirements.txt /workspace/
|
# Install dependencies
|
||||||
|
RUN uv sync --frozen --no-dev
|
||||||
|
|
||||||
|
# Copy source code
|
||||||
COPY . /workspace/
|
COPY . /workspace/
|
||||||
|
|
||||||
RUN pip install --no-cache-dir -r requirements.txt
|
CMD ["uv", "run", "python", "main.py"]
|
||||||
|
|
||||||
CMD ["python", "main.py"]
|
|
||||||
|
|
|
||||||
17
main/turn_detection/pyproject.toml
Normal file
17
main/turn_detection/pyproject.toml
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
[project]
|
||||||
|
name = "turn-detection"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Turn Detection Service based on TEN-framework"
|
||||||
|
requires-python = ">=3.10"
|
||||||
|
dependencies = [
|
||||||
|
"torch>=2.0.0",
|
||||||
|
"transformers>=4.40.0",
|
||||||
|
"fastapi>=0.100.0",
|
||||||
|
"uvicorn>=0.20.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
[dependency-groups]
|
||||||
|
dev = [
|
||||||
|
"pytest>=8.0.0",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
torch==2.8.0
|
|
||||||
transformers==4.57.3
|
|
||||||
fastapi==0.124.0
|
|
||||||
uvicorn==0.38.0
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
docker run --restart=on-failure:3 \
|
docker run --restart=on-failure:3 \
|
||||||
--gpus all \
|
--gpus all \
|
||||||
-itd \
|
-d \
|
||||||
-p 8002:18000 \
|
-p 18000:18000 \
|
||||||
-v /data/yangxianjie/TEN_Turn_Detection/:/data/yangxianjie/TEN_Turn_Detection/ \
|
-v /home/ubuntu/.cache/huggingface:/root/.cache/huggingface \
|
||||||
--name turn_detect \
|
--name turn_detect \
|
||||||
turn_det:latest
|
turn_det:latest
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,56 @@
|
||||||
from transformers import AutoTokenizer, AutoModelForCausalLM
|
from transformers import AutoTokenizer, AutoModelForCausalLM
|
||||||
import torch
|
import torch
|
||||||
import os
|
import os
|
||||||
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
|
||||||
|
|
||||||
|
def get_device():
|
||||||
|
"""Auto-detect the best available device: CUDA > MPS > CPU"""
|
||||||
|
if torch.cuda.is_available():
|
||||||
|
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
||||||
|
return torch.device("cuda")
|
||||||
|
elif torch.backends.mps.is_available():
|
||||||
|
return torch.device("mps")
|
||||||
|
else:
|
||||||
|
return torch.device("cpu")
|
||||||
|
|
||||||
|
|
||||||
|
def get_dtype(device):
|
||||||
|
"""Get appropriate dtype for the device"""
|
||||||
|
if device.type == "cuda":
|
||||||
|
return torch.bfloat16
|
||||||
|
elif device.type == "mps":
|
||||||
|
return torch.float16 # MPS works better with float16
|
||||||
|
else:
|
||||||
|
return torch.float32 # CPU needs float32 for bfloat16 models
|
||||||
|
|
||||||
|
|
||||||
|
# Auto-detect device
|
||||||
|
DEVICE = get_device()
|
||||||
|
DTYPE = get_dtype(DEVICE)
|
||||||
|
print(f"Using device: {DEVICE}, dtype: {DTYPE}")
|
||||||
|
|
||||||
# Load model and tokenizer
|
# Load model and tokenizer
|
||||||
model_id = 'TEN-framework/TEN_Turn_Detection'
|
model_id = 'TEN-framework/TEN_Turn_Detection'
|
||||||
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True, torch_dtype=torch.bfloat16)
|
model = AutoModelForCausalLM.from_pretrained(
|
||||||
|
model_id,
|
||||||
|
trust_remote_code=True,
|
||||||
|
torch_dtype=DTYPE
|
||||||
|
)
|
||||||
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
||||||
|
|
||||||
# Move model to GPU
|
# Move model to device
|
||||||
model = model.cuda()
|
model = model.to(DEVICE)
|
||||||
model.eval()
|
model.eval()
|
||||||
|
|
||||||
# Function for inference
|
|
||||||
def analyze_text(text, system_prompt=""):
|
def analyze_text(text, system_prompt=""):
|
||||||
inf_messages = [{"role":"system", "content":system_prompt}] + [{"role":"user", "content":text}]
|
"""Function for inference"""
|
||||||
|
inf_messages = [{"role": "system", "content": system_prompt}] + [{"role": "user", "content": text}]
|
||||||
input_ids = tokenizer.apply_chat_template(
|
input_ids = tokenizer.apply_chat_template(
|
||||||
inf_messages,
|
inf_messages,
|
||||||
add_generation_prompt=True,
|
add_generation_prompt=True,
|
||||||
return_tensors="pt"
|
return_tensors="pt"
|
||||||
).cuda()
|
).to(DEVICE)
|
||||||
|
|
||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
outputs = model.generate(
|
outputs = model.generate(
|
||||||
|
|
|
||||||
1372
main/turn_detection/uv.lock
Normal file
1372
main/turn_detection/uv.lock
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue