File size: 1,571 Bytes
0a4e34a
 
 
 
 
 
c6ddca4
0a4e34a
 
 
 
 
 
c6ddca4
0a4e34a
 
 
 
 
 
 
 
 
 
 
c6ddca4
0a4e34a
 
 
 
 
 
 
 
 
 
 
c6ddca4
0a4e34a
4434ec3
0a4e34a
 
 
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
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()