import gradio as gr from TTS.api import TTS # ========================= # Load Model (CPU / Zero GPU) # ========================= print("Loading ai4bharat Indic TTS model (CPU)...") tts = TTS( model_name="ai4bharat/indic-tts-coqui-misc", gpu=False, progress_bar=False ) print("Model loaded successfully.") # ========================= # TTS Function # ========================= def text_to_speech(text): if not text or not text.strip(): return None output_path = "tts_output.wav" tts.tts_to_file( text=text, file_path=output_path, language="hi" ) return output_path # ========================= # Fake Voice Clone Handler # (Explains limitation clearly) # ========================= def voice_clone(text, reference_audio): """ NOTE: ai4bharat/indic-tts-coqui-misc DOES NOT support voice cloning. This function falls back to normal TTS. """ if not text or not text.strip(): return None output_path = "clone_fallback.wav" tts.tts_to_file( text=text, file_path=output_path, language="hi" ) return output_path # ========================= # Gradio UI # ========================= with gr.Blocks(title="Hindi TTS (Zero GPU)") as demo: gr.Markdown( """ ## 🗣 Hindi Text to Speech (Zero GPU) **Model:** ai4bharat/indic-tts-coqui-misc **Hardware:** CPU / Zero GPU ⚠️ **Voice cloning is NOT supported by this model.** Reference audio upload is shown only for UI completeness. """ ) with gr.Tab("🔊 Text to Speech"): tts_text = gr.Textbox( label="Hindi Text", placeholder="यहाँ हिंदी टेक्स्ट लिखें...", lines=4 ) tts_btn = gr.Button("Generate Voice") tts_audio = gr.Audio(type="filepath", label="Output Audio") tts_btn.click( fn=text_to_speech, inputs=tts_text, outputs=tts_audio ) with gr.Tab("🎙 Voice Clone (Fallback)"): clone_text = gr.Textbox( label="Hindi Text", placeholder="यहाँ टेक्स्ट लिखें...", lines=4 ) ref_audio = gr.Audio( label="Upload Reference Voice (Not Used)", type="filepath" ) clone_btn = gr.Button("Generate (TTS Fallback)") clone_audio = gr.Audio(type="filepath", label="Generated Audio") clone_btn.click( fn=voice_clone, inputs=[clone_text, ref_audio], outputs=clone_audio ) demo.launch()