import os # Suppress TensorFlow info logs os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Hide INFO & WARNING, only show errors # Optional: disable oneDNN logs if you want completely consistent float ops # os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0' import gradio as gr from TTS.api import TTS # Initialize Coqui TTS model (downloaded once and cached) # Replace with any model name from https://huggingface.co/coqui-ai # "tts_models/en/ljspeech/tacotron2-DDC" is small and works offline tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC") def generate_speech(text): """ Takes text input and generates speech audio using Coqui TTS. Returns the path to the audio file for Gradio playback. """ if not text.strip(): return None output_path = "output.wav" # Generate speech and save to file tts.tts_to_file(text=text, file_path=output_path) return output_path # Simple Gradio UI with gr.Blocks() as demo: gr.Markdown("# Offline Coqui TTS (Hugging Face Space)") gr.Markdown("Enter text below and hear it synthesized offline using Coqui TTS.") with gr.Row(): text_input = gr.Textbox(label="Enter Text", placeholder="Type something...", lines=2) with gr.Row(): speak_button = gr.Button("Generate Speech") with gr.Row(): audio_output = gr.Audio(label="Generated Audio", type="filepath") speak_button.click(fn=generate_speech, inputs=text_input, outputs=audio_output) if __name__ == "__main__": # Launch Gradio app (port/host auto-managed by HF Spaces) demo.launch()