mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
feat:turn detection
This commit is contained in:
parent
59c711acae
commit
62b53042d4
5 changed files with 150 additions and 0 deletions
36
main/turn_detection/Dockerfile
Normal file
36
main/turn_detection/Dockerfile
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
FROM nvidia/cuda:12.3.2-devel-ubuntu22.04
|
||||||
|
|
||||||
|
ENV DEBIAN_FRONTEND=noninteractive
|
||||||
|
|
||||||
|
# 基础依赖
|
||||||
|
RUN apt update && apt install -y \
|
||||||
|
software-properties-common \
|
||||||
|
curl \
|
||||||
|
git \
|
||||||
|
&& add-apt-repository -y ppa:deadsnakes/ppa \
|
||||||
|
&& apt update -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
|
||||||
|
|
||||||
|
WORKDIR /workspace/
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
|
||||||
|
#设置时区timedatectl status|| /etc/timezone
|
||||||
|
ENV TimeZone=Etc/UTC
|
||||||
|
RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone
|
||||||
|
|
||||||
|
|
||||||
|
COPY requirements.txt /workspace/
|
||||||
|
|
||||||
|
COPY . /workspace/
|
||||||
|
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
CMD ["python", "main.py"]
|
||||||
60
main/turn_detection/main.py
Normal file
60
main/turn_detection/main.py
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
from fastapi import FastAPI, BackgroundTasks
|
||||||
|
import uvicorn
|
||||||
|
from pydantic import BaseModel
|
||||||
|
import time
|
||||||
|
import logging
|
||||||
|
from turn_det import analyze_text
|
||||||
|
|
||||||
|
# ---------------------
|
||||||
|
# 日志初始化
|
||||||
|
# ---------------------
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s [%(levelname)s] %(message)s"
|
||||||
|
)
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# ---------------------
|
||||||
|
# FastAPI 实例
|
||||||
|
# ---------------------
|
||||||
|
app = FastAPI(title="My HTTP Service")
|
||||||
|
|
||||||
|
# ---------------------
|
||||||
|
# 请求体
|
||||||
|
# ---------------------
|
||||||
|
class Item(BaseModel):
|
||||||
|
text: str
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------
|
||||||
|
# 路由
|
||||||
|
# ---------------------
|
||||||
|
|
||||||
|
@app.get("/health")
|
||||||
|
def health():
|
||||||
|
"""健康检查接口"""
|
||||||
|
return {
|
||||||
|
"status": "ok",
|
||||||
|
"timestamp": int(time.time())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
def root():
|
||||||
|
return {"message": "Hello from FastAPI!"}
|
||||||
|
|
||||||
|
|
||||||
|
@app.post("/turn-detect")
|
||||||
|
def process(item: Item):
|
||||||
|
"""接收 JSON 请求并处理"""
|
||||||
|
logger.info(f"Received: {item}")
|
||||||
|
res = analyze_text(item.text)
|
||||||
|
logger.info(f"result: {res}")
|
||||||
|
result = {
|
||||||
|
"text": item.text,
|
||||||
|
"result":res
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
uvicorn.run(app, host="0.0.0.0", port=18000)
|
||||||
4
main/turn_detection/requirements.txt
Normal file
4
main/turn_detection/requirements.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
torch==2.8.0
|
||||||
|
transformers==4.57.3
|
||||||
|
fastapi==0.124.0
|
||||||
|
uvicorn==0.38.0
|
||||||
8
main/turn_detection/start_docker.sh
Executable file
8
main/turn_detection/start_docker.sh
Executable file
|
|
@ -0,0 +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/ \
|
||||||
|
--name turn_detect \
|
||||||
|
turn_det:latest
|
||||||
|
|
||||||
42
main/turn_detection/turn_det.py
Normal file
42
main/turn_detection/turn_det.py
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
from transformers import AutoTokenizer, AutoModelForCausalLM
|
||||||
|
import torch
|
||||||
|
import os
|
||||||
|
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
|
||||||
|
# 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)
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
||||||
|
|
||||||
|
# Move model to GPU
|
||||||
|
model = model.cuda()
|
||||||
|
model.eval()
|
||||||
|
|
||||||
|
# Function for inference
|
||||||
|
def analyze_text(text, system_prompt=""):
|
||||||
|
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()
|
||||||
|
|
||||||
|
with torch.no_grad():
|
||||||
|
outputs = model.generate(
|
||||||
|
input_ids,
|
||||||
|
max_new_tokens=1,
|
||||||
|
do_sample=True,
|
||||||
|
top_p=0.1,
|
||||||
|
temperature=0.1,
|
||||||
|
pad_token_id=tokenizer.eos_token_id
|
||||||
|
)
|
||||||
|
|
||||||
|
response = outputs[0][input_ids.shape[-1]:]
|
||||||
|
return tokenizer.decode(response, skip_special_tokens=True)
|
||||||
|
|
||||||
|
if __name__=="__main__":
|
||||||
|
# Example usage
|
||||||
|
text = "Hello I have a question about"
|
||||||
|
result = analyze_text(text)
|
||||||
|
print(f"Input: '{text}'")
|
||||||
|
print(f"Turn Detection Result: '{result}'")
|
||||||
|
|
||||||
Loading…
Reference in a new issue