feat: turn detection modify

This commit is contained in:
Harold 2025-12-19 14:10:23 +08:00
parent 62b53042d4
commit c82787b7d7
6 changed files with 1443 additions and 27 deletions

View file

@ -2,7 +2,7 @@ FROM nvidia/cuda:12.3.2-devel-ubuntu22.04
ENV DEBIAN_FRONTEND=noninteractive
# 基础依赖
# Install system dependencies
RUN apt update && apt install -y \
software-properties-common \
curl \
@ -12,25 +12,24 @@ RUN apt update && apt install -y \
&& apt install -y python3.12 python3.12-venv python3.12-dev \
&& apt clean && rm -rf /var/lib/apt/lists/*
# 安装 pip(自动带上 distutils)
RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
# 设置默认 python 和 pip
RUN ln -s /usr/bin/python3.12 /usr/bin/python && ln -s /usr/local/bin/pip /usr/bin/pip
# Install uv
RUN curl -LsSf https://astral.sh/uv/install.sh | sh
ENV PATH="/root/.local/bin:$PATH"
WORKDIR /workspace/
EXPOSE 8080
EXPOSE 18000
#设置时区timedatectl status|| /etc/timezone
# Set timezone
ENV TimeZone=Etc/UTC
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/
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", "main.py"]
CMD ["uv", "run", "python", "main.py"]

View 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",
]

View file

@ -1,4 +0,0 @@
torch==2.8.0
transformers==4.57.3
fastapi==0.124.0
uvicorn==0.38.0

View file

@ -1,8 +1,8 @@
docker run --restart=on-failure:3 \
--gpus all \
-itd \
-p 8002:18000 \
-v /data/yangxianjie/TEN_Turn_Detection/:/data/yangxianjie/TEN_Turn_Detection/ \
-d \
-p 18000:18000 \
-v /home/ubuntu/.cache/huggingface:/root/.cache/huggingface \
--name turn_detect \
turn_det:latest

View file

@ -1,24 +1,56 @@
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
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
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)
# Move model to GPU
model = model.cuda()
# Move model to device
model = model.to(DEVICE)
model.eval()
# Function for inference
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(
inf_messages,
add_generation_prompt=True,
return_tensors="pt"
).cuda()
).to(DEVICE)
with torch.no_grad():
outputs = model.generate(

1372
main/turn_detection/uv.lock Normal file

File diff suppressed because it is too large Load diff