wifix199's picture
Update app.py
b849a33 verified
raw
history blame
1.64 kB
import gradio as gr
from gtts import gTTS
from pydub import AudioSegment
import tempfile
import os
def generate_tts(text, language, pitch_shift):
tts = gTTS(text=text, lang=language)
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
tts.save(temp_file.name)
temp_file_path = temp_file.name
try:
audio = AudioSegment.from_file(temp_file_path, format="mp3")
except Exception as e:
print(f"Error loading audio file: {e}")
return None
# Apply pitch shifting (more pronounced)
if pitch_shift:
audio = audio._spawn(audio.raw_data, overrides={"frame_rate": int(audio.frame_rate * 0.8)})
audio = audio.set_frame_rate(44100)
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as out_file:
audio.export(out_file.name, format="mp3")
out_file_path = out_file.name
os.remove(temp_file_path)
return out_file_path
def chatbot(text, language, male_voice):
output_audio_path = generate_tts(text, language, male_voice)
if output_audio_path is None:
return "Error generating audio"
return output_audio_path
iface = gr.Interface(
fn=chatbot,
inputs=[
gr.Textbox(label="Enter your text"),
gr.Dropdown(label="Select Language", choices=["en", "es", "fr", "de", "it", "hi"], value="en"),
gr.Checkbox(label="Male Voice", value=True)
],
outputs=gr.Audio(label="Generated Audio"),
live=True,
title="Text-to-Speech AI Chatbot",
description="Generate speech with different languages and voice options."
)
iface.launch()