akshaylinson commited on
Commit
c56be8b
·
1 Parent(s): e7431e6

init indicf5 api

Browse files
Files changed (3) hide show
  1. README.md +120 -8
  2. app.py +133 -0
  3. requirements.txt +13 -0
README.md CHANGED
@@ -1,14 +1,126 @@
1
  ---
2
- title: Indicf5 Api
3
- emoji: 📊
4
- colorFrom: yellow
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 6.30.0
8
- python_version: '3.12'
9
  app_file: app.py
10
  pinned: false
11
- short_description: indicF5 server for codeovice
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: IndicF5 API
3
+ emoji: 🎙️
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: "4.44.0"
 
8
  app_file: app.py
9
  pinned: false
10
+ space_hardware: zero-a10g
11
  ---
12
 
13
+ # IndicF5 TTS API Space
14
+
15
+ A Gradio Space wrapping [ai4bharat/IndicF5](https://huggingface.co/ai4bharat/IndicF5) with a FastAPI `/synthesize` endpoint for CodeVoice.
16
+
17
+ **Free tier — Gradio SDK, 2 vCPU / 16 GB RAM, no Docker required.**
18
+
19
+ ## API Endpoints
20
+
21
+ | Method | Path | Description |
22
+ |--------|------|-------------|
23
+ | GET | `/health` | Liveness check |
24
+ | POST | `/synthesize` | Zero-shot TTS synthesis |
25
+
26
+ ### POST /synthesize
27
+
28
+ | Field | Type | Description |
29
+ |-------|------|-------------|
30
+ | `text` | string | Text to synthesize |
31
+ | `ref_text` | string | Transcript of the reference audio |
32
+ | `language` | string | e.g. `malayalam`, `hindi`, `tamil`, `english` |
33
+ | `ref_audio` | file | Reference WAV (5–10 seconds) |
34
+
35
+ Returns `audio/wav` bytes.
36
+
37
+ ## Supported Languages
38
+
39
+ Malayalam, Hindi, Tamil, Telugu, Kannada, Bengali, Gujarati, Marathi, Punjabi, Odia, Assamese, Urdu, English
40
+
41
+ ---
42
+
43
+ ## Setup Guide
44
+
45
+ ### Step 1 — Create the Space
46
+
47
+ 1. Go to [huggingface.co/new-space](https://huggingface.co/new-space)
48
+ 2. Space name: `indicf5-api`
49
+ 3. Select **Gradio** as the SDK (free, no Docker needed)
50
+ 4. Hardware: **CPU basic** (free — 2 vCPU / 16 GB RAM)
51
+ 5. Visibility: **Public**
52
+
53
+ ### Step 2 — Push files
54
+
55
+ Clone the Space repo and push the two files:
56
+
57
+ ```bash
58
+ git clone https://huggingface.co/spaces/YOUR_USERNAME/indicf5-api
59
+ cd indicf5-api
60
+
61
+ # Copy from your CodeVoice repo
62
+ cp /path/to/codevoice/huggingface_space/app.py .
63
+ cp /path/to/codevoice/huggingface_space/requirements.txt .
64
+ cp /path/to/codevoice/huggingface_space/README.md .
65
+
66
+ git add . && git commit -m "init indicf5 api" && git push
67
+ ```
68
+
69
+ > No `Dockerfile` needed — Gradio SDK handles the server automatically.
70
+
71
+ ### Step 3 — Wait for build
72
+
73
+ First build installs deps and downloads the IndicF5 model (~1.4 GB). Takes **5–10 minutes**. Watch logs in the Space UI under **Logs** tab.
74
+
75
+ ### Step 4 — Test the endpoint
76
+
77
+ ```bash
78
+ # Health check
79
+ curl https://YOUR_USERNAME-indicf5-api.hf.space/health
80
+
81
+ # Synthesis test
82
+ curl -X POST https://YOUR_USERNAME-indicf5-api.hf.space/synthesize \
83
+ -F "text=नमस्ते, मेरा नाम अर्जुन है" \
84
+ -F "ref_text=Hello, this is my reference audio" \
85
+ -F "language=hindi" \
86
+ -F "ref_audio=@/path/to/reference.wav" \
87
+ --output output.wav
88
+ ```
89
+
90
+ ### Step 5 — Connect to CodeVoice
91
+
92
+ Add to `backend/.env`:
93
+
94
+ ```
95
+ INDICF5_URL=https://YOUR_USERNAME-indicf5-api.hf.space
96
+ INDICF5_TIMEOUT=120
97
+ ```
98
+
99
+ Restart the CodeVoice backend. Done.
100
+
101
+ ### Step 6 — Keep-Alive cron (recommended)
102
+
103
+ Free Spaces sleep after ~15 min of inactivity. Add this to your server's crontab:
104
+
105
+ ```bash
106
+ # crontab -e
107
+ */10 8-22 * * * curl -s https://YOUR_USERNAME-indicf5-api.hf.space/health > /dev/null
108
+ ```
109
+
110
+ ---
111
+
112
+ ## How the URL works
113
+
114
+ Your Space URL pattern: `https://YOUR_USERNAME-indicf5-api.hf.space`
115
+
116
+ - `/` → Gradio demo UI (browser)
117
+ - `/health` → JSON liveness (CodeVoice keep-alive)
118
+ - `/synthesize` → multipart POST (CodeVoice backend calls this)
119
+
120
+ ## Limitations
121
+
122
+ - Cold start after sleep: 50–90 seconds
123
+ - No concurrency (CodeVoice's Redis/RQ queue handles this automatically)
124
+ - ~20–35 seconds per synthesis on CPU
125
+ - Reference WAV (~250 KB) sent on every request
126
+ - Not suitable for real-time voice agents
app.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from huggingface_hub import snapshot_download
19
+
20
+ # ── load model once at startup ────────────────────────────────────────────────
21
+ MODEL_REPO = "ai4bharat/IndicF5"
22
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
23
+ print(f"[INDICF5-SPACE] Loading model on {DEVICE} …")
24
+
25
+ _model_dir = snapshot_download(repo_id=MODEL_REPO)
26
+ _ckpt_path = os.path.join(_model_dir, "model_1200000.pt")
27
+ _vocab_path = os.path.join(_model_dir, "vocab.txt")
28
+
29
+ from f5_tts.infer.utils_infer import (
30
+ load_model,
31
+ load_vocoder,
32
+ preprocess_ref_audio_text,
33
+ infer_process,
34
+ )
35
+ from f5_tts.model import DiT
36
+
37
+ vocoder = load_vocoder(is_local=False, local_path=None, device=DEVICE)
38
+ F5TTS_model_cfg = dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4)
39
+ model = load_model(
40
+ DiT,
41
+ F5TTS_model_cfg,
42
+ ckpt_path=_ckpt_path,
43
+ mel_spec_type="vocos",
44
+ vocab_file=_vocab_path,
45
+ device=DEVICE,
46
+ )
47
+ print("[INDICF5-SPACE] Model ready")
48
+
49
+
50
+ # ── shared synthesis logic ────────────────────────────────────────────────────
51
+ @spaces.GPU
52
+ def _synthesize(ref_audio_path: str, ref_text: str, text: str) -> tuple:
53
+ ref_audio_proc, ref_text_proc = preprocess_ref_audio_text(ref_audio_path, ref_text)
54
+ audio_out, sample_rate, _ = infer_process(
55
+ ref_audio_proc,
56
+ ref_text_proc,
57
+ text,
58
+ model,
59
+ vocoder,
60
+ mel_spec_type="vocos",
61
+ device=DEVICE,
62
+ )
63
+ return audio_out, sample_rate
64
+
65
+
66
+ # ── FastAPI sub-app (used by CodeVoice backend) ───────────────────────────────
67
+ api = FastAPI()
68
+
69
+
70
+ @api.get("/health")
71
+ def health():
72
+ return JSONResponse({"status": "ok", "device": DEVICE})
73
+
74
+
75
+ @api.post("/synthesize")
76
+ async def synthesize(
77
+ text: str = Form(...),
78
+ ref_text: str = Form(...),
79
+ language: str = Form("english"),
80
+ ref_audio: UploadFile = File(...),
81
+ ):
82
+ audio_bytes = await ref_audio.read()
83
+ if len(audio_bytes) < 1024:
84
+ raise HTTPException(status_code=400, detail="Reference audio too small")
85
+
86
+ with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
87
+ tmp.write(audio_bytes)
88
+ tmp_path = tmp.name
89
+
90
+ try:
91
+ audio_out, sample_rate = _synthesize(tmp_path, ref_text, text)
92
+ finally:
93
+ os.unlink(tmp_path)
94
+
95
+ buf = io.BytesIO()
96
+ sf.write(buf, audio_out, sample_rate, format="WAV")
97
+ return Response(content=buf.getvalue(), media_type="audio/wav")
98
+
99
+
100
+ # ── Gradio UI (required by Gradio SDK) ───────────────────────────────────────
101
+ def gradio_synthesize(ref_audio_path, ref_text, text, language):
102
+ if not ref_audio_path:
103
+ return None
104
+ try:
105
+ audio_out, sample_rate = _synthesize(ref_audio_path, ref_text, text)
106
+ buf = io.BytesIO()
107
+ sf.write(buf, audio_out, sample_rate, format="WAV")
108
+ buf.seek(0)
109
+ return (sample_rate, audio_out)
110
+ except Exception as e:
111
+ raise gr.Error(str(e))
112
+
113
+
114
+ with gr.Blocks(title="IndicF5 TTS") as demo:
115
+ gr.Markdown("## IndicF5 — Indic Language Zero-Shot TTS\nUpload a reference WAV and synthesize in any Indic language.")
116
+ with gr.Row():
117
+ ref_audio = gr.Audio(label="Reference Audio (5–10s)", type="filepath")
118
+ ref_text = gr.Textbox(label="Reference Transcript")
119
+ text_input = gr.Textbox(label="Text to Synthesize")
120
+ language = gr.Dropdown(
121
+ choices=["english", "hindi", "malayalam", "tamil", "telugu", "kannada",
122
+ "bengali", "gujarati", "marathi", "punjabi", "odia", "assamese", "urdu"],
123
+ value="hindi", label="Language"
124
+ )
125
+ output_audio = gr.Audio(label="Output")
126
+ gr.Button("Synthesize").click(
127
+ gradio_synthesize,
128
+ inputs=[ref_audio, ref_text, text_input, language],
129
+ outputs=output_audio,
130
+ )
131
+
132
+ # Mount FastAPI routes onto the Gradio app
133
+ app = gr.mount_gradio_app(api, demo, path="/")
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ spaces
3
+ fastapi
4
+ python-multipart
5
+ soundfile
6
+ torch
7
+ torchaudio
8
+ huggingface_hub
9
+ transformers
10
+ vocos
11
+ einops
12
+ x-transformers
13
+ git+https://github.com/AI4Bharat/IndicF5.git