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

fix: clean rewrite with gr.mount_gradio_app and @spaces.GPU at module level

Browse files
Files changed (1) hide show
  1. app.py +42 -37
app.py CHANGED
@@ -1,5 +1,5 @@
1
  """
2
- IndicF5 HuggingFace Space β€” ZeroGPU Gradio Space with FastAPI /synthesize endpoint.
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 FastAPI, File, Form, UploadFile, HTTPException
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
- 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)]})
 
 
 
 
 
 
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="/")