capradeepgujaran commited on
Commit
97482f2
·
verified ·
1 Parent(s): 4b389b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -53
app.py CHANGED
@@ -1,45 +1,25 @@
1
  import gradio as gr
2
  import tempfile
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(
30
- model=model,
31
- voice=voice,
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
38
-
39
- return temp_audio_path, f"Speech generated with OpenAI TTS using {voice} voice and {model} model"
40
- except Exception as e:
41
- return None, f"Error in OpenAI TTS speech generation: {str(e)}"
42
-
43
  def google_tts(text, lang, tld):
44
  try:
45
  tts = gTTS(text=text, lang=lang, tld=tld, slow=False)
@@ -54,43 +34,30 @@ def google_tts(text, lang, tld):
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")
61
-
62
- with gr.Group() as openai_options:
63
- openai_voice_input = gr.Dropdown(["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Select Voice", value="nova")
64
- openai_model_input = gr.Dropdown(["tts-1", "tts-1-hd"], label="Select Model", value="tts-1")
65
 
66
  with gr.Group() as google_options:
67
  google_lang_input = gr.Dropdown(list(google_langs.keys()), label="Select Language", value="en")
68
- google_voice_input = gr.Dropdown(google_voice_options["en"], label="Select Voice Variant", value="com")
69
 
70
  speech_button = gr.Button("Generate Speech")
71
  speech_output = gr.Audio(label="Generated Speech")
72
  speech_message = gr.Textbox(label="Message")
73
 
74
- def generate_speech(text, method, openai_voice, openai_model, google_lang, google_voice):
75
- if method == "OpenAI TTS":
76
- return openai_tts(text, openai_voice, openai_model)
77
- else:
78
- return google_tts(text, google_lang, google_voice)
79
-
80
- def update_visible_options(method):
81
- if method == "OpenAI TTS":
82
- return gr.Group(visible=True), gr.Group(visible=False)
83
- else:
84
- return gr.Group(visible=False), gr.Group(visible=True)
85
 
86
  def update_google_voice_options(lang):
87
- return gr.Dropdown(choices=google_voice_options.get(lang, ["com"]), value="com")
88
 
89
  speech_button.click(generate_speech,
90
- inputs=[text_input, tts_method, openai_voice_input, openai_model_input, google_lang_input, google_voice_input],
91
  outputs=[speech_output, speech_message])
92
 
93
- tts_method.change(update_visible_options, inputs=[tts_method], outputs=[openai_options, google_options])
94
  google_lang_input.change(update_google_voice_options, inputs=[google_lang_input], outputs=[google_voice_input])
95
 
96
- iface.launch()
 
1
  import gradio as gr
2
  import tempfile
 
 
3
  from gtts import gTTS
4
  from gtts.lang import tts_langs
5
 
 
 
 
6
  # Get available languages for Google TTS
7
  google_langs = tts_langs()
8
 
9
+ # Define voice options with descriptive names for each language
10
  google_voice_options = {
11
+ "en": [("Australia", "com.au"), ("Canada", "ca"), ("United Kingdom", "co.uk"),
12
+ ("United States", "com"), ("India", "co.in"), ("Ireland", "ie"), ("South Africa", "co.za")],
13
+ "es": [("Spain", "com"), ("Latin America", "es"), ("Mexico", "com.mx")],
14
+ "fr": [("France", "fr"), ("Canada", "ca")],
15
+ "de": [("Germany", "de"), ("Austria", "at")],
16
+ "it": [("Italy", "it"), ("United States", "com")],
17
+ "ja": [("Japan", "jp")],
18
+ "ko": [("Korea", "com")],
19
+ "pt": [("Brazil", "com.br"), ("Portugal", "pt")],
20
+ "zh": [("China", "cn"), ("Taiwan", "tw")]
21
  }
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  def google_tts(text, lang, tld):
24
  try:
25
  tts = gTTS(text=text, lang=lang, tld=tld, slow=False)
 
34
 
35
  # Gradio interface
36
  with gr.Blocks() as iface:
37
+ gr.Markdown("# Google TTS Tool")
38
 
39
  text_input = gr.Textbox(label="Enter text for speech generation")
 
 
 
 
 
40
 
41
  with gr.Group() as google_options:
42
  google_lang_input = gr.Dropdown(list(google_langs.keys()), label="Select Language", value="en")
43
+ google_voice_input = gr.Dropdown([x[0] for x in google_voice_options["en"]], label="Select Voice Variant", value="United States")
44
 
45
  speech_button = gr.Button("Generate Speech")
46
  speech_output = gr.Audio(label="Generated Speech")
47
  speech_message = gr.Textbox(label="Message")
48
 
49
+ def generate_speech(text, google_lang, google_voice_desc):
50
+ # Find the tld (country code) based on the description
51
+ google_voice_tld = dict(google_voice_options[google_lang])[google_voice_desc]
52
+ return google_tts(text, google_lang, google_voice_tld)
 
 
 
 
 
 
 
53
 
54
  def update_google_voice_options(lang):
55
+ return gr.Dropdown(choices=[x[0] for x in google_voice_options.get(lang, [("United States", "com")])], value="United States")
56
 
57
  speech_button.click(generate_speech,
58
+ inputs=[text_input, google_lang_input, google_voice_input],
59
  outputs=[speech_output, speech_message])
60
 
 
61
  google_lang_input.change(update_google_voice_options, inputs=[google_lang_input], outputs=[google_voice_input])
62
 
63
+ iface.launch()