Upload 3 files
Browse files- Dockerfile.txt +39 -0
- README.md +6 -6
- app.py +356 -0
Dockerfile.txt
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
# ๅฎ่ฃ
็ณป็ปไพ่ต๏ผๅซ soxใlibsndfile๏ผ
|
| 4 |
+
RUN apt-get update && apt-get install -y \
|
| 5 |
+
sox \
|
| 6 |
+
libsox-fmt-all \
|
| 7 |
+
libsndfile1 \
|
| 8 |
+
ffmpeg \
|
| 9 |
+
git \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
WORKDIR /app
|
| 13 |
+
|
| 14 |
+
# ๅ
่ฃ
CPU ็ torch๏ผHF ๅ
่ดน Space ๆ GPU๏ผ
|
| 15 |
+
RUN pip install --no-cache-dir \
|
| 16 |
+
torch==2.3.1+cpu \
|
| 17 |
+
torchaudio==2.3.1+cpu \
|
| 18 |
+
--index-url https://download.pytorch.org/whl/cpu
|
| 19 |
+
|
| 20 |
+
# ๅ่ฃ
ๅ
ถไปไพ่ต
|
| 21 |
+
RUN pip install --no-cache-dir \
|
| 22 |
+
transformers==4.57.3 \
|
| 23 |
+
accelerate \
|
| 24 |
+
einops \
|
| 25 |
+
gradio \
|
| 26 |
+
fastapi \
|
| 27 |
+
uvicorn[standard] \
|
| 28 |
+
python-multipart \
|
| 29 |
+
librosa \
|
| 30 |
+
soundfile \
|
| 31 |
+
numpy \
|
| 32 |
+
onnxruntime \
|
| 33 |
+
qwen-tts
|
| 34 |
+
|
| 35 |
+
COPY app.py .
|
| 36 |
+
|
| 37 |
+
EXPOSE 7860
|
| 38 |
+
|
| 39 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
| 8 |
-
-
|
| 9 |
-
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Qwen3 TTS
|
| 3 |
+
emoji: ๐๏ธ
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
+
license: apache-2.0
|
| 10 |
+
---
|
|
|
app.py
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import io
|
| 3 |
+
import gc
|
| 4 |
+
import tempfile
|
| 5 |
+
import torch
|
| 6 |
+
import soundfile as sf
|
| 7 |
+
import numpy as np
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
|
| 10 |
+
from fastapi.responses import StreamingResponse
|
| 11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
+
from typing import Optional
|
| 13 |
+
import uvicorn
|
| 14 |
+
|
| 15 |
+
# โโ ่ฎพๅค & ็ฒพๅบฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 16 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 17 |
+
DTYPE = torch.bfloat16
|
| 18 |
+
|
| 19 |
+
print(f"[TTS] Device={DEVICE}, dtype={DTYPE}")
|
| 20 |
+
from qwen_tts import Qwen3TTSModel
|
| 21 |
+
|
| 22 |
+
# โโ ๅฏๅจๅชๅ ่ฝฝ Base โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 23 |
+
print("[TTS] Loading Base model...")
|
| 24 |
+
model_base = Qwen3TTSModel.from_pretrained(
|
| 25 |
+
"Qwen/Qwen3-TTS-12Hz-1.7B-Base",
|
| 26 |
+
device_map=DEVICE,
|
| 27 |
+
dtype=DTYPE,
|
| 28 |
+
)
|
| 29 |
+
print("[TTS] Base model ready.")
|
| 30 |
+
LANGUAGES = model_base.get_supported_languages()
|
| 31 |
+
|
| 32 |
+
# โโ CustomVoice ๆ้ๅ ่ฝฝ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 33 |
+
model_custom = None
|
| 34 |
+
|
| 35 |
+
def _load_custom():
|
| 36 |
+
global model_custom
|
| 37 |
+
if model_custom is not None:
|
| 38 |
+
return
|
| 39 |
+
print("[TTS] Loading CustomVoice...")
|
| 40 |
+
model_custom = Qwen3TTSModel.from_pretrained(
|
| 41 |
+
"Qwen/Qwen3-TTS-12Hz-1.7B-CustomVoice",
|
| 42 |
+
device_map=DEVICE,
|
| 43 |
+
dtype=DTYPE,
|
| 44 |
+
)
|
| 45 |
+
print("[TTS] CustomVoice ready.")
|
| 46 |
+
|
| 47 |
+
def _unload_custom():
|
| 48 |
+
global model_custom
|
| 49 |
+
if model_custom is None:
|
| 50 |
+
return
|
| 51 |
+
print("[TTS] Unloading CustomVoice...")
|
| 52 |
+
del model_custom
|
| 53 |
+
model_custom = None
|
| 54 |
+
gc.collect()
|
| 55 |
+
if DEVICE == "cuda":
|
| 56 |
+
torch.cuda.empty_cache()
|
| 57 |
+
print("[TTS] CustomVoice unloaded.")
|
| 58 |
+
|
| 59 |
+
# โโ Speaker ๅ่กจ๏ผๅฏๅจๆถ่ทๅไธๆฌก๏ผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 60 |
+
print("[TTS] Fetching speaker list...")
|
| 61 |
+
_load_custom()
|
| 62 |
+
SPEAKERS = model_custom.get_supported_speakers()
|
| 63 |
+
_unload_custom()
|
| 64 |
+
print(f"[TTS] Speakers: {SPEAKERS}")
|
| 65 |
+
|
| 66 |
+
# โโ ๅ
จๅฑ anchor ๅญๅจ role -> {sr, audio, text} โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 67 |
+
_anchors: dict = {}
|
| 68 |
+
|
| 69 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 70 |
+
# ๅทฅๅ
ท
|
| 71 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 72 |
+
|
| 73 |
+
def _wav_bytes(audio: np.ndarray, sr: int) -> bytes:
|
| 74 |
+
buf = io.BytesIO()
|
| 75 |
+
sf.write(buf, audio, sr, format="WAV")
|
| 76 |
+
buf.seek(0)
|
| 77 |
+
return buf.read()
|
| 78 |
+
|
| 79 |
+
def _wav_response(audio: np.ndarray, sr: int, name: str = "out.wav") -> StreamingResponse:
|
| 80 |
+
return StreamingResponse(
|
| 81 |
+
io.BytesIO(_wav_bytes(audio, sr)),
|
| 82 |
+
media_type="audio/wav",
|
| 83 |
+
headers={"Content-Disposition": f"attachment; filename={name}"},
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def _tmp_wav(sr: int, audio: np.ndarray) -> str:
|
| 87 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as f:
|
| 88 |
+
sf.write(f.name, audio, sr)
|
| 89 |
+
return f.name
|
| 90 |
+
|
| 91 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 92 |
+
# FastAPI
|
| 93 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 94 |
+
|
| 95 |
+
app_api = FastAPI(title="Qwen3-TTS", version="4.0.0")
|
| 96 |
+
app_api.add_middleware(CORSMiddleware, allow_origins=["*"],
|
| 97 |
+
allow_methods=["*"], allow_headers=["*"])
|
| 98 |
+
|
| 99 |
+
@app_api.get("/health")
|
| 100 |
+
def health():
|
| 101 |
+
return {
|
| 102 |
+
"status": "ok", "device": DEVICE,
|
| 103 |
+
"custom_loaded": model_custom is not None,
|
| 104 |
+
"anchors": list(_anchors.keys()),
|
| 105 |
+
"languages": LANGUAGES,
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
# โโ /batch_anchor โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 109 |
+
# ไธๆฌกๆงๅปบ็ซๆๆ่ง่ฒ anchor๏ผCustomVoice ๅชๅ ่ฝฝไธๆฌก
|
| 110 |
+
@app_api.post("/batch_anchor")
|
| 111 |
+
async def batch_anchor(
|
| 112 |
+
roles: str = Form(...), # JSON list
|
| 113 |
+
speakers: str = Form(...), # JSON list
|
| 114 |
+
texts: str = Form(...), # JSON list
|
| 115 |
+
languages: str = Form(...), # JSON list
|
| 116 |
+
instructs: str = Form(...), # JSON list
|
| 117 |
+
):
|
| 118 |
+
import json, base64
|
| 119 |
+
roles_ = json.loads(roles)
|
| 120 |
+
speakers_ = json.loads(speakers)
|
| 121 |
+
texts_ = json.loads(texts)
|
| 122 |
+
languages_ = json.loads(languages)
|
| 123 |
+
instructs_ = json.loads(instructs)
|
| 124 |
+
|
| 125 |
+
if not (len(roles_) == len(speakers_) == len(texts_) == len(languages_) == len(instructs_)):
|
| 126 |
+
raise HTTPException(400, "all lists must be same length")
|
| 127 |
+
|
| 128 |
+
results = {}
|
| 129 |
+
try:
|
| 130 |
+
_load_custom()
|
| 131 |
+
for role, spk, text, lang, inst in zip(
|
| 132 |
+
roles_, speakers_, texts_, languages_, instructs_):
|
| 133 |
+
inst = inst if inst and inst.strip() else None
|
| 134 |
+
wavs, sr = model_custom.generate_custom_voice(
|
| 135 |
+
text=text, language=lang, speaker=spk, instruct=inst)
|
| 136 |
+
audio = wavs[0]
|
| 137 |
+
_anchors[role] = {"sr": sr, "audio": audio, "text": text}
|
| 138 |
+
wav_b64 = base64.b64encode(_wav_bytes(audio, sr)).decode()
|
| 139 |
+
results[role] = {"sr": sr, "wav_b64": wav_b64}
|
| 140 |
+
print(f"[batch_anchor] โ {role} ({spk})")
|
| 141 |
+
finally:
|
| 142 |
+
_unload_custom()
|
| 143 |
+
|
| 144 |
+
return results
|
| 145 |
+
|
| 146 |
+
# โโ /tts (ๅ
ผๅฎนๆง็๏ผๅ่ง่ฒ) โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 147 |
+
@app_api.post("/tts")
|
| 148 |
+
async def tts_single(
|
| 149 |
+
text: str = Form(...),
|
| 150 |
+
speaker: str = Form("Vivian"),
|
| 151 |
+
language: str = Form("chinese"),
|
| 152 |
+
instruct: str = Form(""),
|
| 153 |
+
role: str = Form("_narrator"),
|
| 154 |
+
set_anchor: bool = Form(False), # FIX: ไป
ๅฝๆพๅผ่ฆๆฑๆถๆ(้ๆฐ)ๅๅ
ฅ/่ฆ็ _anchors[role]
|
| 155 |
+
):
|
| 156 |
+
if not text.strip():
|
| 157 |
+
raise HTTPException(400, "text is empty")
|
| 158 |
+
inst = instruct if instruct.strip() else None
|
| 159 |
+
try:
|
| 160 |
+
_load_custom()
|
| 161 |
+
wavs, sr = model_custom.generate_custom_voice(
|
| 162 |
+
text=text, language=language, speaker=speaker, instruct=inst)
|
| 163 |
+
audio = wavs[0]
|
| 164 |
+
# FIX: ไนๅ่ฟ้ๆ ๆกไปถ่ฆ็ _anchors[role]๏ผๅฏผ่ดไปปไฝไธๆฌกๅธฆ instruct ็
|
| 165 |
+
# ๆ
็ปชๅฐ่ฏ๏ผspeak_emotion / dialogue ้้ narrator ่ก๏ผ้ฝไผๆๅทฒ็ปๅปบ็ซๅฅฝ็
|
| 166 |
+
# anchor ้กถๆฟๆ๏ผๅ็ปญ /clone ๅฐฑไผ็จ่ฟๅฅ"ๅธฆๆ
็ปช"็้ณ้ขๅฝๅ่๏ผ้ ๆ้ณ่ฒ่ทๅใ
|
| 167 |
+
# ็ฐๅจ๏ผๅชๆๆพๅผไผ set_anchor=true๏ผๅปบ็ซ/้ๅปบ้็นๆถ๏ผๆๅๅ
ฅใ
|
| 168 |
+
if set_anchor or role not in _anchors:
|
| 169 |
+
_anchors[role] = {"sr": sr, "audio": audio, "text": text}
|
| 170 |
+
return _wav_response(audio, sr, "speech.wav")
|
| 171 |
+
except Exception as e:
|
| 172 |
+
raise HTTPException(500, str(e))
|
| 173 |
+
finally:
|
| 174 |
+
_unload_custom()
|
| 175 |
+
|
| 176 |
+
# โโ /clone โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 177 |
+
# โ ๏ธ ไฟฎๆน๏ผๆฏๆไธค็งๆนๅผ - ไฝฟ็จๆๅก็ซฏ anchor ๆ็ดๆฅไธไผ ๅ่้ณ้ข
|
| 178 |
+
@app_api.post("/clone")
|
| 179 |
+
async def clone(
|
| 180 |
+
text: str = Form(...),
|
| 181 |
+
language: str = Form("chinese"),
|
| 182 |
+
role: str = Form("_narrator"),
|
| 183 |
+
reference_text: str = Form(""),
|
| 184 |
+
reference_audio: UploadFile = File(None),
|
| 185 |
+
):
|
| 186 |
+
if not text.strip():
|
| 187 |
+
raise HTTPException(400, "text is empty")
|
| 188 |
+
|
| 189 |
+
tmp_path = None
|
| 190 |
+
try:
|
| 191 |
+
# FIX: ไนๅ่ฟ้"ๆๅก็ซฏ anchor"ๆฐธ่ฟไผๅ
๏ผๅฏผ่ดๅช่ฆ role ๅจ _anchors ้
|
| 192 |
+
# ๏ผๆฏๅฆ็จ create ๅปบ็ซ่ฟ๏ผ๏ผ/clone ๅฐฑๅฎๅ
จๆ ่งๅฎขๆท็ซฏไผ ๆฅ็ reference_audio/
|
| 193 |
+
# reference_text โโ workflow.sh ้็"ๆปๅจๅ่"ๆบๅถๅฝขๅ่่ฎพ๏ผ่ไธไธๆฆ
|
| 194 |
+
# _anchors[role] ่ขซๅซ็่ฐ็จ๏ผๅฆๆ
็ปชๅฐ่ฏ๏ผ่ฆ็๏ผๅ
จ็จ้ฝไผ่ท็่ทๅใ
|
| 195 |
+
# ็ฐๅจๆนไธบ๏ผๅฎขๆท็ซฏๆพๅผไผ ไบ reference_audio + reference_text ๅฐฑไผๅ
็จๅฎ
|
| 196 |
+
# ๏ผๆปๅจๅ่ / ๅผบๅถๅ้้ฝ้่ฟ่ฟไธชๅๆฐๆงๅถ๏ผ๏ผๅฎขๆท็ซฏๆฒกไผ ๆถๆ้ๅๆๅก็ซฏ anchorใ
|
| 197 |
+
if reference_audio is not None and reference_text.strip():
|
| 198 |
+
suffix = os.path.splitext(reference_audio.filename)[-1] or ".wav"
|
| 199 |
+
with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as f:
|
| 200 |
+
f.write(await reference_audio.read())
|
| 201 |
+
tmp_path = f.name
|
| 202 |
+
ref_text = reference_text
|
| 203 |
+
print(f"[clone] ไฝฟ็จๅฎขๆท็ซฏๅ่้ณ้ข: {reference_audio.filename}")
|
| 204 |
+
|
| 205 |
+
elif role in _anchors:
|
| 206 |
+
anchor = _anchors[role]
|
| 207 |
+
tmp_path = _tmp_wav(anchor["sr"], anchor["audio"])
|
| 208 |
+
ref_text = anchor["text"]
|
| 209 |
+
print(f"[clone] ไฝฟ็จๆๅก็ซฏ anchor: {role}")
|
| 210 |
+
|
| 211 |
+
else:
|
| 212 |
+
raise HTTPException(
|
| 213 |
+
400, f"No anchor for role='{role}'. Call /batch_anchor or /tts first, or provide reference_audio and reference_text.")
|
| 214 |
+
|
| 215 |
+
# ็ๆๅ
้
|
| 216 |
+
wavs, sr = model_base.generate_voice_clone(
|
| 217 |
+
text=text, ref_audio=tmp_path,
|
| 218 |
+
ref_text=ref_text, language=language)
|
| 219 |
+
return _wav_response(wavs[0], sr, "clone.wav")
|
| 220 |
+
|
| 221 |
+
except HTTPException:
|
| 222 |
+
raise
|
| 223 |
+
except Exception as e:
|
| 224 |
+
raise HTTPException(500, str(e))
|
| 225 |
+
finally:
|
| 226 |
+
if tmp_path and os.path.exists(tmp_path):
|
| 227 |
+
os.unlink(tmp_path)
|
| 228 |
+
|
| 229 |
+
# โโ /clear_anchors โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 230 |
+
@app_api.post("/clear_anchors")
|
| 231 |
+
def clear_anchors():
|
| 232 |
+
_anchors.clear()
|
| 233 |
+
return {"status": "cleared"}
|
| 234 |
+
|
| 235 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 236 |
+
# Gradio UI
|
| 237 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 238 |
+
|
| 239 |
+
def ui_first(text, language, speaker, instruct):
|
| 240 |
+
if not text.strip():
|
| 241 |
+
return None, "โ ่ฏท่พๅ
ฅๆๆฌ"
|
| 242 |
+
try:
|
| 243 |
+
_load_custom()
|
| 244 |
+
wavs, sr = model_custom.generate_custom_voice(
|
| 245 |
+
text=text, language=language, speaker=speaker,
|
| 246 |
+
instruct=instruct or None)
|
| 247 |
+
audio = wavs[0]
|
| 248 |
+
_anchors["_ui"] = {"sr": sr, "audio": audio, "text": text}
|
| 249 |
+
return (sr, audio), f"โ
็ๆๅฎๆฏ๏ผๅฃฐ้ณๅทฒ้ๅฎ๏ผ{speaker}๏ผ"
|
| 250 |
+
except Exception as e:
|
| 251 |
+
return None, f"โ {e}"
|
| 252 |
+
finally:
|
| 253 |
+
_unload_custom()
|
| 254 |
+
|
| 255 |
+
def ui_clone(text, language):
|
| 256 |
+
if not text.strip():
|
| 257 |
+
return None, "โ ่ฏท่พๅ
ฅๆๆฌ"
|
| 258 |
+
if "_ui" not in _anchors:
|
| 259 |
+
return None, "โ ่ฏทๅ
ๅจใ็ฌฌไธๆฎตใTab ็ๆๅนถ้ๅฎๅฃฐ้ณ"
|
| 260 |
+
anchor = _anchors["_ui"]
|
| 261 |
+
tmp = _tmp_wav(anchor["sr"], anchor["audio"])
|
| 262 |
+
try:
|
| 263 |
+
wavs, sr = model_base.generate_voice_clone(
|
| 264 |
+
text=text, ref_audio=tmp,
|
| 265 |
+
ref_text=anchor["text"], language=language)
|
| 266 |
+
return (sr, wavs[0]), "โ
็ๆๆๅ"
|
| 267 |
+
except Exception as e:
|
| 268 |
+
return None, f"โ {e}"
|
| 269 |
+
finally:
|
| 270 |
+
os.unlink(tmp)
|
| 271 |
+
|
| 272 |
+
def ui_manual_clone(text, language, ref_audio, ref_text):
|
| 273 |
+
if not text.strip() or ref_audio is None:
|
| 274 |
+
return None, "โ ่ฏทๅกซๅๆๆฌๅนถไธไผ ๅ่้ณ้ข"
|
| 275 |
+
if not ref_text.strip():
|
| 276 |
+
return None, "โ ๅ่ๆๆฌไธบๅฟ
ๅกซ้กน"
|
| 277 |
+
sr_r, audio_np = ref_audio
|
| 278 |
+
tmp = _tmp_wav(sr_r, audio_np)
|
| 279 |
+
try:
|
| 280 |
+
wavs, sr = model_base.generate_voice_clone(
|
| 281 |
+
text=text, ref_audio=tmp,
|
| 282 |
+
ref_text=ref_text, language=language)
|
| 283 |
+
return (sr, wavs[0]), "โ
ๅ
้ๆๅ"
|
| 284 |
+
except Exception as e:
|
| 285 |
+
return None, f"โ {e}"
|
| 286 |
+
finally:
|
| 287 |
+
os.unlink(tmp)
|
| 288 |
+
|
| 289 |
+
def ui_unlock():
|
| 290 |
+
_anchors.pop("_ui", None)
|
| 291 |
+
return "๐ ๅทฒ่งฃ้"
|
| 292 |
+
|
| 293 |
+
with gr.Blocks(title="Qwen3-TTS", theme=gr.themes.Soft()) as gradio_ui:
|
| 294 |
+
gr.Markdown(
|
| 295 |
+
"# ๐๏ธ Qwen3-TTS 1.7B\n"
|
| 296 |
+
"**ๅทฅไฝๆต**๏ผ็ฌฌไธๆฎตๅปบ็ซ anchor โ ๅ็ปญๅ
จ้จ clone ไฟๆๅฃฐ้ณไธ่ด\n\n"
|
| 297 |
+
"> CustomVoice ๅชๅจ็ๆ็ฌฌไธๆฎตๆถไธดๆถๅ ่ฝฝ๏ผ็จๅฎ็ซๅป้ๆพใ"
|
| 298 |
+
)
|
| 299 |
+
with gr.Tabs():
|
| 300 |
+
with gr.Tab("โ ็ฌฌไธๆฎต"):
|
| 301 |
+
with gr.Row():
|
| 302 |
+
with gr.Column():
|
| 303 |
+
f_text = gr.Textbox(label="็ฌฌไธๆฎตๆๆฌ", lines=4)
|
| 304 |
+
f_lang = gr.Dropdown(LANGUAGES, value="chinese", label="่ฏญ่จ")
|
| 305 |
+
f_speaker = gr.Dropdown(SPEAKERS, value=SPEAKERS[0], label="Speaker")
|
| 306 |
+
f_inst = gr.Textbox(label="้ฃๆ ผๆไปค๏ผๅฏ้๏ผ",
|
| 307 |
+
placeholder="ไพ๏ผ็จๆฒ็จณ็ๆญ้ณ่
")
|
| 308 |
+
with gr.Row():
|
| 309 |
+
f_btn = gr.Button("๐ต ็ๆๅนถ้ๅฎ", variant="primary")
|
| 310 |
+
f_unlock = gr.Button("๐ ่งฃ้", variant="secondary")
|
| 311 |
+
with gr.Column():
|
| 312 |
+
f_audio = gr.Audio(label="่พๅบ", type="numpy")
|
| 313 |
+
f_status = gr.Textbox(label="็ถๆ", interactive=False)
|
| 314 |
+
f_btn.click(ui_first, [f_text, f_lang, f_speaker, f_inst], [f_audio, f_status])
|
| 315 |
+
f_unlock.click(ui_unlock, [], f_status)
|
| 316 |
+
|
| 317 |
+
with gr.Tab("โก ๅ็ปญๆฎต่ฝ"):
|
| 318 |
+
gr.Markdown("ไฝฟ็จ้ๅฎๅฃฐ้ณ๏ผๆฏๆฌกๅช้่พๅ
ฅๆๆฌ๏ผๅฃฐ้ณๅฎๅ
จไธ่ดใ")
|
| 319 |
+
with gr.Row():
|
| 320 |
+
with gr.Column():
|
| 321 |
+
b_text = gr.Textbox(label="็ฎๆ ๆๆฌ", lines=5)
|
| 322 |
+
b_lang = gr.Dropdown(LANGUAGES, value="chinese", label="่ฏญ่จ")
|
| 323 |
+
b_btn = gr.Button("๐ต ็ๆ", variant="primary")
|
| 324 |
+
with gr.Column():
|
| 325 |
+
b_audio = gr.Audio(label="่พๅบ", type="numpy")
|
| 326 |
+
b_status = gr.Textbox(label="็ถๆ", interactive=False)
|
| 327 |
+
b_btn.click(ui_clone, [b_text, b_lang], [b_audio, b_status])
|
| 328 |
+
|
| 329 |
+
with gr.Tab("โข ๆๅจๅ
้"):
|
| 330 |
+
with gr.Row():
|
| 331 |
+
with gr.Column():
|
| 332 |
+
c_ref = gr.Audio(label="ๅ่้ณ้ข๏ผ3~10 ็ง๏ผ", type="numpy")
|
| 333 |
+
c_rtext = gr.Textbox(label="ๅ่้ณ้ขๅฏนๅบๆๆฌ๏ผๅฟ
ๅกซ๏ผ", lines=2)
|
| 334 |
+
c_text = gr.Textbox(label="็ฎๆ ๆๆฌ", lines=4)
|
| 335 |
+
c_lang = gr.Dropdown(LANGUAGES, value="chinese", label="่ฏญ่จ")
|
| 336 |
+
c_btn = gr.Button("๐ต ็ๆ", variant="primary")
|
| 337 |
+
with gr.Column():
|
| 338 |
+
c_audio = gr.Audio(label="่พๅบ", type="numpy")
|
| 339 |
+
c_status = gr.Textbox(label="็ถๆ", interactive=False)
|
| 340 |
+
c_btn.click(ui_manual_clone,
|
| 341 |
+
[c_text, c_lang, c_ref, c_rtext],
|
| 342 |
+
[c_audio, c_status])
|
| 343 |
+
|
| 344 |
+
gr.Markdown(
|
| 345 |
+
"---\n### API\n"
|
| 346 |
+
"- `POST /batch_anchor` โ ๆน้ๅปบ็ซๆๆ่ง่ฒ anchor๏ผCustomVoice ๅชๅ ่ฝฝไธๆฌก๏ผ\n"
|
| 347 |
+
"- `POST /tts` โ ๅ่ง่ฒๅปบ็ซ anchor๏ผๅ
ผๅฎนๆง็๏ผ\n"
|
| 348 |
+
"- `POST /clone` โ ็จ anchor ๆๅ่้ณ้ขๅ
้็ๆ\n"
|
| 349 |
+
"- `POST /clear_anchors` โ ๆธ
้คๆๆ anchor\n"
|
| 350 |
+
"- `GET /health` โ ็ถๆ\n"
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
final_app = gr.mount_gradio_app(app_api, gradio_ui, path="/")
|
| 354 |
+
|
| 355 |
+
if __name__ == "__main__":
|
| 356 |
+
uvicorn.run(final_app, host="0.0.0.0", port=7860)
|