File size: 4,838 Bytes
79b2661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4637e15
79b2661
 
4637e15
 
79b2661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""

Gradio Web UI Demo & Hugging Face Spaces App.

Enables instant interactive browser testing of the 10M TTS Model.

Thread-safe in-memory audio output (no shared temp file race conditions).

"""

import time
import numpy as np
import torch
import gradio as gr

from config import TTS10MConfig
from models.tts_engine import TTS10MEngine
from data.text_cleaner import TextTokenizer
from utils.checkpoint import load_checkpoint, find_checkpoint
from utils.long_form import synthesize_long_text

# Initialize Model on Startup
cfg = TTS10MConfig()
device = "cuda" if torch.cuda.is_available() else "cpu"
model = None

checkpoint = find_checkpoint()
if checkpoint:
    model = load_checkpoint(TTS10MEngine(cfg.model, cfg.training).to(device), checkpoint, device=device).eval()

tokenizer = TextTokenizer()


def tts_generate(text: str, steps: int = 48, speed: float = 0.92, cfg_scale: float = 1.5, temperature: float = 0.667):
    text_ids = tokenizer.text_to_ids(text)
    if text_ids.numel() == 0:
        return None, "Please enter some valid text."

    if model is None:
        return None, "Place trained acoustic weights in checkpoints/model.safetensors and restart the app."

    start_t = time.perf_counter()
    waveform = synthesize_long_text(
        model=model,
        tokenizer=tokenizer,
        text=text,
        device=torch.device(device),
        sample_rate=cfg.audio.sample_rate,
        num_steps=int(steps),
        speed=float(speed),
        cfg_scale=float(cfg_scale),
        temperature=float(temperature),
        sway_sampling_coef=cfg.model.sway_sampling_coef,
        min_frames=2,
    )

    latency_ms = (time.perf_counter() - start_t) * 1000
    duration_s = waveform.shape[-1] / float(cfg.audio.sample_rate)

    waveform = np.clip(waveform, -1.0, 1.0)
    int16_audio = (waveform * 32767.0).astype(np.int16)

    status = f"Generated in {latency_ms:.1f}ms ({duration_s:.2f}s audio) using {steps} ODE steps on {device.upper()}."
    return (cfg.audio.sample_rate, int16_audio), status


# Build Modern Gradio UI
with gr.Blocks(title="Murli V1.0 — Hindi TTS") as demo:
    gr.Markdown(
        """

        # 🎙️ Murli V1.0 — Hindi Text-to-Speech Engine (ViuAI)

        ### Lightweight 8.17M Acoustic Flow-Matching Speech Synthesizer (+ Vocos 24kHz)

        *Powered by Optimal Transport Conditional Flow Matching (OT-CFM)*

        """
    )

    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(
                label="Enter Text (Hindi Devanagari / English)",
                placeholder="नमस्ते, यह हमारा 10M हिंदी TTS मॉडल बोल रहा है...",
                lines=3,
                value="नमस्ते! यह हमारा नया हिंदी टेक्स्ट टू स्पीच मॉडल बोल रहा है।",
            )
            with gr.Row():
                steps_slider = gr.Slider(
                    minimum=8, maximum=64, value=48, step=4, label="ODE Steps (48=Cleanest)"
                )
                speed_slider = gr.Slider(
                    minimum=0.7, maximum=1.4, value=1.0, step=0.05, label="Speaking Speed"
                )
                cfg_slider = gr.Slider(
                    minimum=1.0, maximum=3.0, value=1.5, step=0.1, label="CFG Clarity Scale (1.5=Best)"
                )
                temp_slider = gr.Slider(
                    minimum=0.4, maximum=1.0, value=0.667, step=0.05, label="Noise Temp (0.667=Clean)"
                )
            generate_btn = gr.Button("🚀 Generate Speech", variant="primary")

        with gr.Column():
            audio_output = gr.Audio(label="Synthesized 24kHz HD Audio", type="numpy")
            status_output = gr.Markdown()

    generate_btn.click(
        fn=tts_generate,
        inputs=[text_input, steps_slider, speed_slider, cfg_slider, temp_slider],
        outputs=[audio_output, status_output],
    )

    gr.Examples(
        examples=[
            ["नमस्ते! यह हमारा दस मिलियन पैरामीटर वाला हिंदी मॉडल है।", 48, 1.0, 1.5, 0.667],
            ["आज मौसम बहुत सुहावना है और हवा ठंडी चल रही है।", 48, 1.0, 1.5, 0.667],
            ["आपका ऑडियो बिना किसी रोबोट बीप के बिल्कुल साफ बनता है।", 48, 1.0, 1.5, 0.667],
        ],
        inputs=[text_input, steps_slider, speed_slider, cfg_slider, temp_slider],
    )

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860, share=False)