akshaylinson commited on
Commit
7931238
Β·
1 Parent(s): b296e35

fix: use demo.launch with on_startup for ZeroGPU compatibility

Browse files
Files changed (1) hide show
  1. app.py +42 -46
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 (ZeroGPU: @spaces.GPU required at module level) ─────────────────
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
- demo = gr.Interface(
76
- fn=gradio_fn,
77
- inputs=[
78
- gr.Audio(type="filepath", label="Reference Audio (5-10s)"),
79
- gr.Textbox(label="Reference Transcript"),
80
- gr.Textbox(label="Text to Synthesize"),
81
- ],
82
- outputs=gr.Audio(label="Output"),
83
- title="IndicF5 TTS",
84
- )
85
-
86
- # ── FastAPI routes mounted on Gradio app ──────────────────────────────────────
87
- _api = FastAPI()
88
-
89
- @_api.get("/health")
90
- def health():
91
- return JSONResponse({"status": "ok"})
92
-
93
- @_api.post("/synthesize")
94
- async def synthesize(
95
- text: str = Form(...),
96
- ref_text: str = Form(...),
97
- language: str = Form("english"),
98
- ref_audio: UploadFile = File(...),
99
- ):
100
- audio_bytes = await ref_audio.read()
101
- if len(audio_bytes) < 1024:
102
- raise HTTPException(status_code=400, detail="Reference audio too small")
103
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
104
- tmp.write(audio_bytes)
105
- tmp_path = tmp.name
106
- try:
107
- audio_out, sample_rate = _synthesize(tmp_path, ref_text, text)
108
- finally:
109
- os.unlink(tmp_path)
110
- buf = io.BytesIO()
111
- sf.write(buf, audio_out, sample_rate, format="WAV")
112
- return Response(content=buf.getvalue(), media_type="audio/wav")
113
-
114
- app = gr.mount_gradio_app(_api, demo, path="/")
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)]})