""" IndicF5 HuggingFace Space — CPU basic (free tier), Gradio SDK. """ import io import os import sys import tempfile import gradio as gr import soundfile as sf import torch _BASE_DIR = os.path.dirname(os.path.abspath(__file__)) _INDICF5_DIR = os.path.join(_BASE_DIR, "IndicF5") _ckpt_path = os.path.join(_INDICF5_DIR, "model.safetensors") _vocab_path = os.path.join(_INDICF5_DIR, "checkpoints", "vocab.txt") if _INDICF5_DIR not in sys.path: sys.path.insert(0, _INDICF5_DIR) _hf_token = os.environ.get("HF_TOKEN", "") if not os.path.isfile(_ckpt_path): from huggingface_hub import hf_hub_download print("[INDICF5] Downloading model.safetensors …") os.makedirs(_INDICF5_DIR, exist_ok=True) hf_hub_download(repo_id="ai4bharat/IndicF5", filename="model.safetensors", local_dir=_INDICF5_DIR, token=_hf_token) if not os.path.isfile(_vocab_path): from huggingface_hub import hf_hub_download print("[INDICF5] Downloading vocab.txt …") os.makedirs(os.path.join(_INDICF5_DIR, "checkpoints"), exist_ok=True) hf_hub_download(repo_id="ai4bharat/IndicF5", filename="checkpoints/vocab.txt", local_dir=_INDICF5_DIR, token=_hf_token) print("[INDICF5] Loading model on CPU …") from f5_tts.infer.utils_infer import (infer_process, load_checkpoint, load_model, load_vocoder, preprocess_ref_audio_text) from f5_tts.model import DiT vocoder = load_vocoder(is_local=False, local_path=None, device="cpu") model = load_model( DiT, dict(dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4), mel_spec_type="vocos", vocab_file=_vocab_path, device="cpu", ) model = load_checkpoint(model, _ckpt_path, device="cpu", use_ema=True) print("[INDICF5] Model ready") def _synthesize(ref_audio_path: str, ref_text: str, text: str): ref_audio_proc, ref_text_proc = preprocess_ref_audio_text(ref_audio_path, ref_text) audio_out, sample_rate, _ = infer_process( ref_audio_proc, ref_text_proc, text, model, vocoder, mel_spec_type="vocos", device="cpu", ) return audio_out, sample_rate # ── minimal Gradio UI ───────────────────────────────────────────────────────── import gradio.blocks as _gb _gb.Blocks.get_api_info = lambda self, *a, **kw: {"named_endpoints": {}, "unnamed_endpoints": {}} with gr.Blocks() as demo: gr.Markdown("## IndicF5 TTS API\nUse `POST /api/synthesize` with multipart form data.") gr.Textbox(value="API is running", label="Status", interactive=False) # ── FastAPI routes ───────────────────────────────────────────────────────────── from fastapi import File, Form, HTTPException, UploadFile from fastapi.responses import JSONResponse, Response @demo.app.get("/health", include_in_schema=False) def health(): return JSONResponse({"status": "ok"}) @demo.app.post("/api/synthesize", include_in_schema=False) async def synthesize( text: str = Form(...), ref_text: str = Form(...), language: str = Form("english"), ref_audio: UploadFile = File(...), ): audio_bytes = await ref_audio.read() if len(audio_bytes) < 1024: raise HTTPException(400, "Reference audio too small") with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp: tmp.write(audio_bytes) tmp_path = tmp.name try: audio_out, sample_rate = _synthesize(tmp_path, ref_text, text) finally: os.unlink(tmp_path) buf = io.BytesIO() sf.write(buf, audio_out, sample_rate, format="WAV") return Response(content=buf.getvalue(), media_type="audio/wav") demo.launch()