Spaces:
Runtime error
Runtime error
Commit Β·
92d9618
1
Parent(s): 7931238
fix: clean rewrite with gr.mount_gradio_app and @spaces.GPU at module level
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
IndicF5 HuggingFace Space β ZeroGPU
|
| 3 |
"""
|
| 4 |
import io
|
| 5 |
import os
|
|
@@ -10,7 +10,7 @@ import gradio as gr
|
|
| 10 |
import soundfile as sf
|
| 11 |
import spaces
|
| 12 |
import torch
|
| 13 |
-
from fastapi import
|
| 14 |
from fastapi.responses import Response, JSONResponse
|
| 15 |
|
| 16 |
# ββ paths βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -75,40 +75,45 @@ def gradio_fn(ref_audio_path, ref_text, text):
|
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
gr.Markdown("## IndicF5 TTS")
|
| 77 |
ref_audio = gr.Audio(type="filepath", label="Reference Audio (5-10s)")
|
| 78 |
-
|
| 79 |
text_input = gr.Textbox(label="Text to Synthesize")
|
| 80 |
output_audio = gr.Audio(label="Output")
|
| 81 |
-
gr.Button("Synthesize").click(
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
+
IndicF5 HuggingFace Space β ZeroGPU + FastAPI /synthesize endpoint.
|
| 3 |
"""
|
| 4 |
import io
|
| 5 |
import os
|
|
|
|
| 10 |
import soundfile as sf
|
| 11 |
import spaces
|
| 12 |
import torch
|
| 13 |
+
from fastapi import File, Form, UploadFile, HTTPException
|
| 14 |
from fastapi.responses import Response, JSONResponse
|
| 15 |
|
| 16 |
# ββ paths βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 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_box = 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(
|
| 82 |
+
gradio_fn,
|
| 83 |
+
inputs=[ref_audio, ref_text_box, text_input],
|
| 84 |
+
outputs=output_audio
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# ββ mount FastAPI routes on Gradio's app after blocks context βββββββββββββββββ
|
| 88 |
+
app = demo.app if hasattr(demo, "app") else None
|
| 89 |
+
|
| 90 |
+
# Use gr.mount_gradio_app pattern β Gradio SDK handles the server
|
| 91 |
+
from fastapi import FastAPI
|
| 92 |
+
_api = FastAPI()
|
| 93 |
+
|
| 94 |
+
@_api.get("/health")
|
| 95 |
+
def health():
|
| 96 |
+
return JSONResponse({"status": "ok"})
|
| 97 |
+
|
| 98 |
+
@_api.post("/synthesize")
|
| 99 |
+
async def synthesize(
|
| 100 |
+
text: str = Form(...),
|
| 101 |
+
ref_text: str = Form(...),
|
| 102 |
+
language: str = Form("english"),
|
| 103 |
+
ref_audio: UploadFile = File(...),
|
| 104 |
+
):
|
| 105 |
+
audio_bytes = await ref_audio.read()
|
| 106 |
+
if len(audio_bytes) < 1024:
|
| 107 |
+
raise HTTPException(status_code=400, detail="Reference audio too small")
|
| 108 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
|
| 109 |
+
tmp.write(audio_bytes)
|
| 110 |
+
tmp_path = tmp.name
|
| 111 |
+
try:
|
| 112 |
+
audio_out, sample_rate = _synthesize(tmp_path, ref_text, text)
|
| 113 |
+
finally:
|
| 114 |
+
os.unlink(tmp_path)
|
| 115 |
+
buf = io.BytesIO()
|
| 116 |
+
sf.write(buf, audio_out, sample_rate, format="WAV")
|
| 117 |
+
return Response(content=buf.getvalue(), media_type="audio/wav")
|
| 118 |
+
|
| 119 |
+
app = gr.mount_gradio_app(_api, demo, path="/")
|