Spaces:
Running on T4
Running on T4
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py โ v2
|
| 2 |
"""
|
| 3 |
Darwin-TTS-1.7B-Cross v2 โ HuggingFace Space
|
| 4 |
"""
|
|
@@ -7,13 +7,24 @@ from pathlib import Path
|
|
| 7 |
from contextlib import asynccontextmanager
|
| 8 |
from fastapi import FastAPI, HTTPException
|
| 9 |
from fastapi.responses import HTMLResponse, Response
|
|
|
|
| 10 |
|
| 11 |
-
state = {"
|
| 12 |
|
| 13 |
@asynccontextmanager
|
| 14 |
async def lifespan(app: FastAPI):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
yield
|
| 16 |
-
state["
|
| 17 |
|
| 18 |
app = FastAPI(title="Darwin-TTS-1.7B-Cross", lifespan=lifespan)
|
| 19 |
|
|
@@ -31,9 +42,21 @@ async def synthesize(request: dict):
|
|
| 31 |
try:
|
| 32 |
from qwen_tts import Qwen3TTSModel
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
ref_path = "/tmp/darwin_ref.wav"
|
| 39 |
sf.write(ref_path, (0.1 * np.sin(2 * np.pi * 200 * np.linspace(0, 3, 72000))).astype(np.float32), 24000)
|
|
@@ -52,7 +75,7 @@ async def synthesize(request: dict):
|
|
| 52 |
return Response(
|
| 53 |
content=buf.read(),
|
| 54 |
media_type="audio/wav",
|
| 55 |
-
headers={"X-Duration": f"{len(wav)/sr:.1f}", "X-Model":
|
| 56 |
)
|
| 57 |
except Exception as e:
|
| 58 |
if model is not None: del model
|
|
@@ -61,7 +84,7 @@ async def synthesize(request: dict):
|
|
| 61 |
|
| 62 |
@app.get("/health")
|
| 63 |
async def health():
|
| 64 |
-
return {"status": "ok", "cuda": torch.cuda.is_available()}
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
import uvicorn
|
|
|
|
| 1 |
+
# app.py โ v2 (๋ ์ํผ ๋น๊ณต๊ฐ, speech_tokenizer ํด๊ฒฐ)
|
| 2 |
"""
|
| 3 |
Darwin-TTS-1.7B-Cross v2 โ HuggingFace Space
|
| 4 |
"""
|
|
|
|
| 7 |
from contextlib import asynccontextmanager
|
| 8 |
from fastapi import FastAPI, HTTPException
|
| 9 |
from fastapi.responses import HTMLResponse, Response
|
| 10 |
+
from safetensors import safe_open
|
| 11 |
|
| 12 |
+
state = {"darwin_weights": None}
|
| 13 |
|
| 14 |
@asynccontextmanager
|
| 15 |
async def lifespan(app: FastAPI):
|
| 16 |
+
# Darwin ๊ฐ์ค์น ์ฌ์ ๋ก๋
|
| 17 |
+
from huggingface_hub import hf_hub_download
|
| 18 |
+
print("๐ฆ Loading Darwin weights...")
|
| 19 |
+
path = hf_hub_download("FINAL-Bench/Darwin-TTS-1.7B-Cross", "model.safetensors")
|
| 20 |
+
weights = {}
|
| 21 |
+
with safe_open(path, framework="pt") as s:
|
| 22 |
+
for k in s.keys():
|
| 23 |
+
weights[k] = s.get_tensor(k)
|
| 24 |
+
state["darwin_weights"] = weights
|
| 25 |
+
print(f" โ
{len(weights)} tensors cached")
|
| 26 |
yield
|
| 27 |
+
state["darwin_weights"] = None
|
| 28 |
|
| 29 |
app = FastAPI(title="Darwin-TTS-1.7B-Cross", lifespan=lifespan)
|
| 30 |
|
|
|
|
| 42 |
try:
|
| 43 |
from qwen_tts import Qwen3TTSModel
|
| 44 |
|
| 45 |
+
# ํญ์ ์๋ณธ์์ ๋ก๋ (speech_tokenizer ํฌํจ)
|
| 46 |
+
model = Qwen3TTSModel.from_pretrained(
|
| 47 |
+
"Qwen/Qwen3-TTS-12Hz-1.7B-Base",
|
| 48 |
+
device_map="cuda:0", dtype=torch.bfloat16
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Darwin ๋ชจ๋: ์ฌ์ ๋ธ๋ ๋ฉ๋ ๊ฐ์ค์น๋ก ๊ต์ฒด
|
| 52 |
+
if use_darwin and state["darwin_weights"]:
|
| 53 |
+
cnt = 0
|
| 54 |
+
for n, p in model.model.named_parameters():
|
| 55 |
+
if n in state["darwin_weights"]:
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
p.copy_(state["darwin_weights"][n].to(p.device, p.dtype))
|
| 58 |
+
cnt += 1
|
| 59 |
+
print(f" Darwin weights applied: {cnt} tensors")
|
| 60 |
|
| 61 |
ref_path = "/tmp/darwin_ref.wav"
|
| 62 |
sf.write(ref_path, (0.1 * np.sin(2 * np.pi * 200 * np.linspace(0, 3, 72000))).astype(np.float32), 24000)
|
|
|
|
| 75 |
return Response(
|
| 76 |
content=buf.read(),
|
| 77 |
media_type="audio/wav",
|
| 78 |
+
headers={"X-Duration": f"{len(wav)/sr:.1f}", "X-Model": "Darwin" if use_darwin else "Original"},
|
| 79 |
)
|
| 80 |
except Exception as e:
|
| 81 |
if model is not None: del model
|
|
|
|
| 84 |
|
| 85 |
@app.get("/health")
|
| 86 |
async def health():
|
| 87 |
+
return {"status": "ok", "cuda": torch.cuda.is_available(), "darwin_loaded": state["darwin_weights"] is not None}
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
import uvicorn
|