VoiceOversV3 / app.py
capradeepgujaran's picture
Update app.py
ead2fe3 verified
raw
history blame
3.84 kB
import gradio as gr
import tempfile
import os
from openai import OpenAI
from gtts import gTTS
from gtts.lang import tts_langs
# Initialize OpenAI client
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# Get available languages for Google TTS
google_langs = tts_langs()
# Define voice options for some common languages
google_voice_options = {
"en": ["com.au", "ca", "co.uk", "com", "co.in", "ie", "co.za"],
"es": ["com", "es", "com.mx"],
"fr": ["fr", "ca"],
"de": ["de", "at"],
"it": ["it", "com"],
"ja": ["jp"],
"ko": ["com"],
"pt": ["com.br", "pt"],
"zh": ["cn", "tw"]
}
def openai_tts(text, voice, model):
try:
response = client.audio.speech.create(
model=model,
voice=voice,
input=text
)
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
temp_audio.write(response.content)
temp_audio_path = temp_audio.name
return temp_audio_path, f"Speech generated with OpenAI TTS using {voice} voice and {model} model"
except Exception as e:
return None, f"Error in OpenAI TTS speech generation: {str(e)}"
def google_tts(text, lang, tld):
try:
tts = gTTS(text=text, lang=lang, tld=tld, slow=False)
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
tts.save(temp_audio.name)
temp_audio_path = temp_audio.name
return temp_audio_path, f"Speech generated with Google TTS using {lang} language and {tld} voice variant"
except Exception as e:
return None, f"Error in Google TTS speech generation: {str(e)}"
# Gradio interface
with gr.Blocks() as iface:
gr.Markdown("# OpenAI TTS and Enhanced Google TTS Tool")
text_input = gr.Textbox(label="Enter text for speech generation")
tts_method = gr.Radio(["OpenAI TTS", "Google TTS"], label="TTS Method", value="OpenAI TTS")
with gr.Group() as openai_options:
gr.Markdown("OpenAI TTS Options")
openai_voice_input = gr.Dropdown(["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Select Voice", value="nova")
openai_model_input = gr.Dropdown(["tts-1", "tts-1-hd"], label="Select Model", value="tts-1")
with gr.Group() as google_options:
gr.Markdown("Google TTS Options")
google_lang_input = gr.Dropdown(list(google_langs.keys()), label="Select Language", value="en")
google_voice_input = gr.Dropdown(google_voice_options["en"], label="Select Voice Variant", value="com")
speech_button = gr.Button("Generate Speech")
speech_output = gr.Audio(label="Generated Speech")
speech_message = gr.Textbox(label="Message")
def generate_speech(text, method, openai_voice, openai_model, google_lang, google_voice):
if method == "OpenAI TTS":
return openai_tts(text, openai_voice, openai_model)
else:
return google_tts(text, google_lang, google_voice)
def update_visible_options(method):
return (
gr.Group.update(visible=(method == "OpenAI TTS")),
gr.Group.update(visible=(method == "Google TTS"))
)
def update_google_voice_options(lang):
return gr.Dropdown.update(choices=google_voice_options.get(lang, ["com"]), value="com")
speech_button.click(generate_speech,
inputs=[text_input, tts_method, openai_voice_input, openai_model_input, google_lang_input, google_voice_input],
outputs=[speech_output, speech_message])
tts_method.change(update_visible_options, inputs=[tts_method], outputs=[openai_options, google_options])
google_lang_input.change(update_google_voice_options, inputs=[google_lang_input], outputs=[google_voice_input])
iface.launch()