Text_To_Voice / app.py
hamza2923's picture
Create app.py
f5436ef verified
raw
history blame
1.93 kB
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()