Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,27 @@ import tempfile
|
|
3 |
import os
|
4 |
from openai import OpenAI
|
5 |
from gtts import gTTS
|
6 |
-
import
|
7 |
|
8 |
# Initialize OpenAI client
|
9 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
def openai_tts(text, voice, model):
|
12 |
try:
|
13 |
response = client.audio.speech.create(
|
@@ -16,7 +32,6 @@ def openai_tts(text, voice, model):
|
|
16 |
input=text
|
17 |
)
|
18 |
|
19 |
-
# Save the audio to a temporary file
|
20 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
21 |
temp_audio.write(response.content)
|
22 |
temp_audio_path = temp_audio.name
|
@@ -29,18 +44,17 @@ def google_tts(text, lang, tld):
|
|
29 |
try:
|
30 |
tts = gTTS(text=text, lang=lang, tld=tld, slow=False)
|
31 |
|
32 |
-
# Save the audio to a temporary file
|
33 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
34 |
tts.save(temp_audio.name)
|
35 |
temp_audio_path = temp_audio.name
|
36 |
|
37 |
-
return temp_audio_path, f"Speech generated with Google TTS using {lang} language and {tld}
|
38 |
except Exception as e:
|
39 |
return None, f"Error in Google TTS speech generation: {str(e)}"
|
40 |
|
41 |
# Gradio interface
|
42 |
with gr.Blocks() as iface:
|
43 |
-
gr.Markdown("# OpenAI TTS and Google TTS Tool")
|
44 |
|
45 |
text_input = gr.Textbox(label="Enter text for speech generation")
|
46 |
tts_method = gr.Radio(["OpenAI TTS", "Google TTS"], label="TTS Method", value="OpenAI TTS")
|
@@ -52,18 +66,18 @@ with gr.Blocks() as iface:
|
|
52 |
|
53 |
with gr.Group() as google_options:
|
54 |
gr.Markdown("Google TTS Options")
|
55 |
-
google_lang_input = gr.Dropdown(
|
56 |
-
|
57 |
|
58 |
speech_button = gr.Button("Generate Speech")
|
59 |
speech_output = gr.Audio(label="Generated Speech")
|
60 |
speech_message = gr.Textbox(label="Message")
|
61 |
|
62 |
-
def generate_speech(text, method, openai_voice, openai_model, google_lang,
|
63 |
if method == "OpenAI TTS":
|
64 |
return openai_tts(text, openai_voice, openai_model)
|
65 |
else:
|
66 |
-
return google_tts(text, google_lang,
|
67 |
|
68 |
def update_visible_options(method):
|
69 |
return (
|
@@ -71,10 +85,14 @@ with gr.Blocks() as iface:
|
|
71 |
gr.Group.update(visible=(method == "Google TTS"))
|
72 |
)
|
73 |
|
|
|
|
|
|
|
74 |
speech_button.click(generate_speech,
|
75 |
-
inputs=[text_input, tts_method, openai_voice_input, openai_model_input, google_lang_input,
|
76 |
outputs=[speech_output, speech_message])
|
77 |
|
78 |
tts_method.change(update_visible_options, inputs=[tts_method], outputs=[openai_options, google_options])
|
|
|
79 |
|
80 |
iface.launch()
|
|
|
3 |
import os
|
4 |
from openai import OpenAI
|
5 |
from gtts import gTTS
|
6 |
+
from gtts.lang import tts_langs
|
7 |
|
8 |
# Initialize OpenAI client
|
9 |
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
10 |
|
11 |
+
# Get available languages for Google TTS
|
12 |
+
google_langs = tts_langs()
|
13 |
+
|
14 |
+
# Define voice options for some common languages
|
15 |
+
google_voice_options = {
|
16 |
+
"en": ["com.au", "ca", "co.uk", "com", "co.in", "ie", "co.za"],
|
17 |
+
"es": ["com", "es", "com.mx"],
|
18 |
+
"fr": ["fr", "ca"],
|
19 |
+
"de": ["de", "at"],
|
20 |
+
"it": ["it", "com"],
|
21 |
+
"ja": ["jp"],
|
22 |
+
"ko": ["com"],
|
23 |
+
"pt": ["com.br", "pt"],
|
24 |
+
"zh": ["cn", "tw"]
|
25 |
+
}
|
26 |
+
|
27 |
def openai_tts(text, voice, model):
|
28 |
try:
|
29 |
response = client.audio.speech.create(
|
|
|
32 |
input=text
|
33 |
)
|
34 |
|
|
|
35 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
36 |
temp_audio.write(response.content)
|
37 |
temp_audio_path = temp_audio.name
|
|
|
44 |
try:
|
45 |
tts = gTTS(text=text, lang=lang, tld=tld, slow=False)
|
46 |
|
|
|
47 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
48 |
tts.save(temp_audio.name)
|
49 |
temp_audio_path = temp_audio.name
|
50 |
|
51 |
+
return temp_audio_path, f"Speech generated with Google TTS using {lang} language and {tld} voice variant"
|
52 |
except Exception as e:
|
53 |
return None, f"Error in Google TTS speech generation: {str(e)}"
|
54 |
|
55 |
# Gradio interface
|
56 |
with gr.Blocks() as iface:
|
57 |
+
gr.Markdown("# OpenAI TTS and Enhanced Google TTS Tool")
|
58 |
|
59 |
text_input = gr.Textbox(label="Enter text for speech generation")
|
60 |
tts_method = gr.Radio(["OpenAI TTS", "Google TTS"], label="TTS Method", value="OpenAI TTS")
|
|
|
66 |
|
67 |
with gr.Group() as google_options:
|
68 |
gr.Markdown("Google TTS Options")
|
69 |
+
google_lang_input = gr.Dropdown(list(google_langs.keys()), label="Select Language", value="en")
|
70 |
+
google_voice_input = gr.Dropdown(google_voice_options["en"], label="Select Voice Variant", value="com")
|
71 |
|
72 |
speech_button = gr.Button("Generate Speech")
|
73 |
speech_output = gr.Audio(label="Generated Speech")
|
74 |
speech_message = gr.Textbox(label="Message")
|
75 |
|
76 |
+
def generate_speech(text, method, openai_voice, openai_model, google_lang, google_voice):
|
77 |
if method == "OpenAI TTS":
|
78 |
return openai_tts(text, openai_voice, openai_model)
|
79 |
else:
|
80 |
+
return google_tts(text, google_lang, google_voice)
|
81 |
|
82 |
def update_visible_options(method):
|
83 |
return (
|
|
|
85 |
gr.Group.update(visible=(method == "Google TTS"))
|
86 |
)
|
87 |
|
88 |
+
def update_google_voice_options(lang):
|
89 |
+
return gr.Dropdown.update(choices=google_voice_options.get(lang, ["com"]), value="com")
|
90 |
+
|
91 |
speech_button.click(generate_speech,
|
92 |
+
inputs=[text_input, tts_method, openai_voice_input, openai_model_input, google_lang_input, google_voice_input],
|
93 |
outputs=[speech_output, speech_message])
|
94 |
|
95 |
tts_method.change(update_visible_options, inputs=[tts_method], outputs=[openai_options, google_options])
|
96 |
+
google_lang_input.change(update_google_voice_options, inputs=[google_lang_input], outputs=[google_voice_input])
|
97 |
|
98 |
iface.launch()
|