File size: 1,926 Bytes
f5436ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pyttsx3
import os

# Initialize the TTS engine
engine = pyttsx3.init()

# Get available voices
voices = engine.getProperty('voices')

# Create a dictionary to map voice names to IDs
voice_map = {f"{voice.name} ({voice.languages[0] if voice.languages else 'Unknown'})": voice.id for voice in voices}

def text_to_speech(text, voice_name, rate=200):
    """
    Convert text to speech with selected voice and rate.
    Saves output as an audio file and returns the file path.
    """
    # Initialize engine for each call to avoid threading issues with Gradio
    engine = pyttsx3.init()
    
    # Set voice
    voice_id = voice_map.get(voice_name)
    if not voice_id:
        return "Error: Selected voice not found."
    engine.setProperty('voice', voice_id)
    
    # Set speech rate
    engine.setProperty('rate', rate)
    
    # Save audio to a file
    output_file = "output.wav"
    engine.save_to_file(text, output_file)
    engine.runAndWait()
    
    return output_file

# Gradio interface
with gr.Blocks(title="Text-to-Speech with Different Voices") as demo:
    gr.Markdown("# Text-to-Speech Converter")
    gr.Markdown("Enter text and select a voice to convert it to speech with different voices and accents.")
    
    text_input = gr.Textbox(label="Enter Text", placeholder="Type your text here...")
    voice_dropdown = gr.Dropdown(choices=list(voice_map.keys()), label="Select Voice/Accent")
    rate_slider = gr.Slider(minimum=100, maximum=300, value=200, step=10, label="Speech Rate")
    
    convert_button = gr.Button("Convert to Speech")
    audio_output = gr.Audio(label="Generated Speech")
    
    convert_button.click(
        fn=text_to_speech,
        inputs=[text_input, voice_dropdown, rate_slider],
        outputs=audio_output
    )

# Launch the app (commented out for Hugging Face Spaces deployment)
# demo.launch()

if __name__ == "__main__":
    demo.launch()