Spaces:
Runtime error
Runtime error
Commit Β·
7931238
1
Parent(s): b296e35
fix: use demo.launch with on_startup for ZeroGPU compatibility
Browse files
app.py
CHANGED
|
@@ -49,9 +49,9 @@ model = load_checkpoint(model, _ckpt_path, device="cpu", use_ema=True)
|
|
| 49 |
print("[INDICF5-SPACE] Model ready")
|
| 50 |
|
| 51 |
|
| 52 |
-
# ββ synthesis
|
| 53 |
@spaces.GPU
|
| 54 |
-
def _synthesize(ref_audio_path, ref_text, text):
|
| 55 |
gpu = "cuda" if torch.cuda.is_available() else "cpu"
|
| 56 |
model.to(gpu)
|
| 57 |
vocoder.to(gpu)
|
|
@@ -72,47 +72,43 @@ def gradio_fn(ref_audio_path, ref_text, text):
|
|
| 72 |
audio_out, sample_rate = _synthesize(ref_audio_path, ref_text, text)
|
| 73 |
return (sample_rate, audio_out)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
],
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
if __name__ == "__main__":
|
| 117 |
-
import uvicorn
|
| 118 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 49 |
print("[INDICF5-SPACE] Model ready")
|
| 50 |
|
| 51 |
|
| 52 |
+
# ββ synthesis βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 53 |
@spaces.GPU
|
| 54 |
+
def _synthesize(ref_audio_path: str, ref_text: str, text: str):
|
| 55 |
gpu = "cuda" if torch.cuda.is_available() else "cpu"
|
| 56 |
model.to(gpu)
|
| 57 |
vocoder.to(gpu)
|
|
|
|
| 72 |
audio_out, sample_rate = _synthesize(ref_audio_path, ref_text, text)
|
| 73 |
return (sample_rate, audio_out)
|
| 74 |
|
| 75 |
+
with gr.Blocks() as demo:
|
| 76 |
+
gr.Markdown("## IndicF5 TTS")
|
| 77 |
+
ref_audio = gr.Audio(type="filepath", label="Reference Audio (5-10s)")
|
| 78 |
+
ref_text = gr.Textbox(label="Reference Transcript")
|
| 79 |
+
text_input = gr.Textbox(label="Text to Synthesize")
|
| 80 |
+
output_audio = gr.Audio(label="Output")
|
| 81 |
+
gr.Button("Synthesize").click(gradio_fn, inputs=[ref_audio, ref_text, text_input], outputs=output_audio)
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
# ββ FastAPI custom routes βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 85 |
+
def create_api(app: FastAPI):
|
| 86 |
+
@app.get("/health")
|
| 87 |
+
def health():
|
| 88 |
+
return JSONResponse({"status": "ok"})
|
| 89 |
+
|
| 90 |
+
@app.post("/synthesize")
|
| 91 |
+
async def synthesize(
|
| 92 |
+
text: str = Form(...),
|
| 93 |
+
ref_text: str = Form(...),
|
| 94 |
+
language: str = Form("english"),
|
| 95 |
+
ref_audio: UploadFile = File(...),
|
| 96 |
+
):
|
| 97 |
+
audio_bytes = await ref_audio.read()
|
| 98 |
+
if len(audio_bytes) < 1024:
|
| 99 |
+
raise HTTPException(status_code=400, detail="Reference audio too small")
|
| 100 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 101 |
+
tmp.write(audio_bytes)
|
| 102 |
+
tmp_path = tmp.name
|
| 103 |
+
try:
|
| 104 |
+
audio_out, sample_rate = _synthesize(tmp_path, ref_text, text)
|
| 105 |
+
finally:
|
| 106 |
+
os.unlink(tmp_path)
|
| 107 |
+
buf = io.BytesIO()
|
| 108 |
+
sf.write(buf, audio_out, sample_rate, format="WAV")
|
| 109 |
+
return Response(content=buf.getvalue(), media_type="audio/wav")
|
| 110 |
+
|
| 111 |
+
return app
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
demo.launch(app_kwargs={"on_startup": [lambda: create_api(demo.app)]})
|
|
|
|
|
|
|
|
|
|
|
|