akshaylinson commited on
Commit
585e5ce
Β·
1 Parent(s): 6863c79

fix: use gradio 5.9.1 compatible with ZeroGPU

Browse files
Files changed (2) hide show
  1. app.py +27 -63
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,49 +1,36 @@
1
  """
2
- IndicF5 HuggingFace Space β€” Gradio SDK with FastAPI /synthesize endpoint.
3
- SDK: gradio (free tier, 2 vCPU / 16 GB RAM)
4
-
5
- The Gradio UI is a thin demo wrapper. The real endpoint used by CodeVoice is
6
- POST /synthesize mounted via gr.mount_gradio_app onto the FastAPI sub-app.
7
  """
8
  import io
9
  import os
 
10
  import tempfile
11
 
12
- import gradio as gr
13
  import soundfile as sf
14
  import spaces
15
  import torch
16
  from fastapi import FastAPI, File, Form, UploadFile, HTTPException
17
  from fastapi.responses import Response, JSONResponse
18
 
19
- # ── load model once at startup ────────────────────────────────────────────────
20
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
21
  print(f"[INDICF5-SPACE] Loading model on {DEVICE} …")
22
 
23
- # Model files are bundled in the IndicF5/ subfolder β€” no download needed
24
  _BASE_DIR = os.path.dirname(os.path.abspath(__file__))
25
  _INDICF5_DIR = os.path.join(_BASE_DIR, "IndicF5")
26
  _ckpt_path = os.path.join(_INDICF5_DIR, "model.safetensors")
27
  _vocab_path = os.path.join(_INDICF5_DIR, "checkpoints", "vocab.txt")
28
 
29
- # Install f5_tts from the bundled repo
30
- import sys
31
  if _INDICF5_DIR not in sys.path:
32
  sys.path.insert(0, _INDICF5_DIR)
33
 
34
- from f5_tts.infer.utils_infer import (
35
- load_model,
36
- load_vocoder,
37
- preprocess_ref_audio_text,
38
- infer_process,
39
- )
40
  from f5_tts.model import DiT
41
 
42
  vocoder = load_vocoder(is_local=False, local_path=None, device=DEVICE)
43
- F5TTS_model_cfg = dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4)
44
  model = load_model(
45
  DiT,
46
- F5TTS_model_cfg,
47
  ckpt_path=_ckpt_path,
48
  mel_spec_type="vocos",
49
  vocab_file=_vocab_path,
@@ -52,31 +39,24 @@ model = load_model(
52
  print("[INDICF5-SPACE] Model ready")
53
 
54
 
55
- # ── shared synthesis logic ────────────────────────────────────────────────────
56
  @spaces.GPU
57
- def _synthesize(ref_audio_path: str, ref_text: str, text: str) -> tuple:
58
  ref_audio_proc, ref_text_proc = preprocess_ref_audio_text(ref_audio_path, ref_text)
59
  audio_out, sample_rate, _ = infer_process(
60
- ref_audio_proc,
61
- ref_text_proc,
62
- text,
63
- model,
64
- vocoder,
65
- mel_spec_type="vocos",
66
- device=DEVICE,
67
  )
68
  return audio_out, sample_rate
69
 
70
 
71
- # ── FastAPI sub-app (used by CodeVoice backend) ───────────────────────────────
72
  api = FastAPI()
73
 
74
-
75
  @api.get("/health")
76
  def health():
77
  return JSONResponse({"status": "ok", "device": DEVICE})
78
 
79
-
80
  @api.post("/synthesize")
81
  async def synthesize(
82
  text: str = Form(...),
@@ -87,52 +67,36 @@ async def synthesize(
87
  audio_bytes = await ref_audio.read()
88
  if len(audio_bytes) < 1024:
89
  raise HTTPException(status_code=400, detail="Reference audio too small")
90
-
91
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
92
  tmp.write(audio_bytes)
93
  tmp_path = tmp.name
94
-
95
  try:
96
  audio_out, sample_rate = _synthesize(tmp_path, ref_text, text)
97
  finally:
98
  os.unlink(tmp_path)
99
-
100
  buf = io.BytesIO()
101
  sf.write(buf, audio_out, sample_rate, format="WAV")
102
  return Response(content=buf.getvalue(), media_type="audio/wav")
103
 
104
 
105
- # ── Gradio UI (required by Gradio SDK) ───────────────────────────────────────
106
- def gradio_synthesize(ref_audio_path, ref_text, text, language):
 
 
107
  if not ref_audio_path:
108
  return None
109
- try:
110
- audio_out, sample_rate = _synthesize(ref_audio_path, ref_text, text)
111
- buf = io.BytesIO()
112
- sf.write(buf, audio_out, sample_rate, format="WAV")
113
- buf.seek(0)
114
- return (sample_rate, audio_out)
115
- except Exception as e:
116
- raise gr.Error(str(e))
117
-
118
-
119
- with gr.Blocks(title="IndicF5 TTS") as demo:
120
- gr.Markdown("## IndicF5 β€” Indic Language Zero-Shot TTS\nUpload a reference WAV and synthesize in any Indic language.")
121
- with gr.Row():
122
- ref_audio = gr.Audio(label="Reference Audio (5–10s)", type="filepath")
123
- ref_text = gr.Textbox(label="Reference Transcript")
124
- text_input = gr.Textbox(label="Text to Synthesize")
125
- language = gr.Dropdown(
126
- choices=["english", "hindi", "malayalam", "tamil", "telugu", "kannada",
127
- "bengali", "gujarati", "marathi", "punjabi", "odia", "assamese", "urdu"],
128
- value="hindi", label="Language"
129
- )
130
- output_audio = gr.Audio(label="Output")
131
- gr.Button("Synthesize").click(
132
- gradio_synthesize,
133
- inputs=[ref_audio, ref_text, text_input, language],
134
- outputs=output_audio,
135
- )
136
 
137
- # Mount FastAPI routes onto the Gradio app
138
  app = gr.mount_gradio_app(api, demo, path="/")
 
1
  """
2
+ IndicF5 HuggingFace Space β€” Gradio SDK + FastAPI /synthesize endpoint.
 
 
 
 
3
  """
4
  import io
5
  import os
6
+ import sys
7
  import tempfile
8
 
 
9
  import soundfile as sf
10
  import spaces
11
  import torch
12
  from fastapi import FastAPI, File, Form, UploadFile, HTTPException
13
  from fastapi.responses import Response, JSONResponse
14
 
15
+ # ── load model ────────────────────────────────────────────────────────────────
16
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
17
  print(f"[INDICF5-SPACE] Loading model on {DEVICE} …")
18
 
 
19
  _BASE_DIR = os.path.dirname(os.path.abspath(__file__))
20
  _INDICF5_DIR = os.path.join(_BASE_DIR, "IndicF5")
21
  _ckpt_path = os.path.join(_INDICF5_DIR, "model.safetensors")
22
  _vocab_path = os.path.join(_INDICF5_DIR, "checkpoints", "vocab.txt")
23
 
 
 
24
  if _INDICF5_DIR not in sys.path:
25
  sys.path.insert(0, _INDICF5_DIR)
26
 
27
+ from f5_tts.infer.utils_infer import load_model, load_vocoder, preprocess_ref_audio_text, infer_process
 
 
 
 
 
28
  from f5_tts.model import DiT
29
 
30
  vocoder = load_vocoder(is_local=False, local_path=None, device=DEVICE)
 
31
  model = load_model(
32
  DiT,
33
+ dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4),
34
  ckpt_path=_ckpt_path,
35
  mel_spec_type="vocos",
36
  vocab_file=_vocab_path,
 
39
  print("[INDICF5-SPACE] Model ready")
40
 
41
 
42
+ # ── synthesis ─────────────────────────────────────────────────────────────────
43
  @spaces.GPU
44
+ def _synthesize(ref_audio_path, ref_text, text):
45
  ref_audio_proc, ref_text_proc = preprocess_ref_audio_text(ref_audio_path, ref_text)
46
  audio_out, sample_rate, _ = infer_process(
47
+ ref_audio_proc, ref_text_proc, text, model, vocoder,
48
+ mel_spec_type="vocos", device=DEVICE,
 
 
 
 
 
49
  )
50
  return audio_out, sample_rate
51
 
52
 
53
+ # ── FastAPI (CodeVoice calls these) ───────────────────────────────────────────
54
  api = FastAPI()
55
 
 
56
  @api.get("/health")
57
  def health():
58
  return JSONResponse({"status": "ok", "device": DEVICE})
59
 
 
60
  @api.post("/synthesize")
61
  async def synthesize(
62
  text: str = Form(...),
 
67
  audio_bytes = await ref_audio.read()
68
  if len(audio_bytes) < 1024:
69
  raise HTTPException(status_code=400, detail="Reference audio too small")
 
70
  with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
71
  tmp.write(audio_bytes)
72
  tmp_path = tmp.name
 
73
  try:
74
  audio_out, sample_rate = _synthesize(tmp_path, ref_text, text)
75
  finally:
76
  os.unlink(tmp_path)
 
77
  buf = io.BytesIO()
78
  sf.write(buf, audio_out, sample_rate, format="WAV")
79
  return Response(content=buf.getvalue(), media_type="audio/wav")
80
 
81
 
82
+ # ── Gradio UI (satisfies Gradio SDK requirement) ──────────────────────────────
83
+ import gradio as gr
84
+
85
+ def gradio_fn(ref_audio_path, ref_text, text):
86
  if not ref_audio_path:
87
  return None
88
+ audio_out, sample_rate = _synthesize(ref_audio_path, ref_text, text)
89
+ return (sample_rate, audio_out)
90
+
91
+ demo = gr.Interface(
92
+ fn=gradio_fn,
93
+ inputs=[
94
+ gr.Audio(type="filepath", label="Reference Audio (5-10s)"),
95
+ gr.Textbox(label="Reference Transcript"),
96
+ gr.Textbox(label="Text to Synthesize"),
97
+ ],
98
+ outputs=gr.Audio(label="Output"),
99
+ title="IndicF5 TTS",
100
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
 
102
  app = gr.mount_gradio_app(api, demo, path="/")
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- gradio==4.44.0
2
  spaces
3
  fastapi
4
  python-multipart
 
1
+ gradio==5.9.1
2
  spaces
3
  fastapi
4
  python-multipart