File size: 3,892 Bytes
c56be8b
370bc4f
c56be8b
 
 
585e5ce
c56be8b
 
267b1a6
c56be8b
 
 
6863c79
 
 
 
 
98e72e1
 
 
e907488
 
 
c2e6f04
e907488
 
 
 
 
c2e6f04
e907488
 
 
 
370bc4f
3e06de5
 
 
c56be8b
 
267b1a6
c2e6f04
 
267b1a6
c2e6f04
267b1a6
c2e6f04
c56be8b
267b1a6
 
 
 
 
370bc4f
267b1a6
 
 
 
370bc4f
267b1a6
 
 
370bc4f
 
 
 
267b1a6
 
 
 
92d9618
 
267b1a6
92d9618
 
 
3e06de5
267b1a6
92d9618
 
 
 
 
 
 
 
c2e6f04
92d9618
 
 
 
267b1a6
92d9618
 
 
 
 
 
d110f92
e016345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
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()