capradeepgujaran commited on
Commit
fa1f44a
·
verified ·
1 Parent(s): 783f242

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -80
app.py CHANGED
@@ -1,19 +1,13 @@
1
  import gradio as gr
2
- import numpy as np
3
  import tempfile
4
  import os
5
  from openai import OpenAI
6
- import soundfile as sf
7
- import requests
8
- import json
9
 
10
  # Initialize OpenAI client
11
  client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
12
 
13
- # ChatTTS API endpoint and key
14
- CHATTTS_API_ENDPOINT = "https://api.chattts.com/v1/generate"
15
- CHATTTS_API_KEY = os.environ.get("CHATTTS_API_KEY")
16
-
17
  def openai_tts(text, voice, model):
18
  try:
19
  response = client.audio.speech.create(
@@ -31,94 +25,56 @@ def openai_tts(text, voice, model):
31
  except Exception as e:
32
  return None, f"Error in OpenAI TTS speech generation: {str(e)}"
33
 
34
- def chattts(text, voice, style):
35
  try:
36
- payload = {
37
- "text": text,
38
- "voice": voice,
39
- "style": style
40
- }
41
- headers = {
42
- "Content-Type": "application/json",
43
- "Authorization": f"Bearer {CHATTTS_API_KEY}"
44
- }
45
- response = requests.post(CHATTTS_API_ENDPOINT, json=payload, headers=headers)
46
- response.raise_for_status()
47
-
48
  # Save the audio to a temporary file
49
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
50
- temp_audio.write(response.content)
51
  temp_audio_path = temp_audio.name
52
 
53
- return temp_audio_path, f"Speech generated with ChatTTS using {voice} voice and {style} style"
54
- except Exception as e:
55
- return None, f"Error in ChatTTS speech generation: {str(e)}"
56
-
57
- def generate_simple_sound(description, duration, frequency):
58
- try:
59
- sample_rate = 44100
60
- t = np.linspace(0, duration, int(sample_rate * duration), False)
61
-
62
- if "rain" in description.lower():
63
- audio = np.random.normal(0, 0.1, len(t))
64
- elif "wind" in description.lower():
65
- audio = np.sin(2 * np.pi * frequency * t) * np.random.normal(1, 0.1, len(t))
66
- elif "bird" in description.lower():
67
- audio = np.sin(2 * np.pi * frequency * t) * np.exp(-0.5 * t)
68
- audio = np.tile(audio, int(duration / 0.5))[:len(t)]
69
- else:
70
- audio = np.sin(2 * np.pi * frequency * t)
71
-
72
- audio = audio / np.max(np.abs(audio))
73
-
74
- output_path = tempfile.mktemp(suffix=".wav")
75
- sf.write(output_path, audio, sample_rate)
76
-
77
- return output_path, f"Simple sound generated for '{description}'"
78
  except Exception as e:
79
- return None, f"Error in sound generation: {str(e)}"
80
 
81
  # Gradio interface
82
  with gr.Blocks() as iface:
83
- gr.Markdown("# OpenAI TTS, ChatTTS, and Simple Sound Generation Tool")
84
 
85
- with gr.Tab("Text-to-Speech"):
86
- text_input = gr.Textbox(label="Enter text for speech generation")
87
- tts_method = gr.Radio(["OpenAI TTS", "ChatTTS"], label="TTS Method", value="OpenAI TTS")
88
-
89
- with gr.Group():
90
- gr.Markdown("OpenAI TTS Options")
91
- openai_voice_input = gr.Dropdown(["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Select Voice", value="nova")
92
- openai_model_input = gr.Dropdown(["tts-1", "tts-1-hd"], label="Select Model", value="tts-1")
93
-
94
- with gr.Group():
95
- gr.Markdown("ChatTTS Options")
96
- chattts_voice_input = gr.Dropdown(["en-US-1", "en-US-2", "en-GB-1", "en-GB-2"], label="Select Voice", value="en-US-1")
97
- chattts_style_input = gr.Dropdown(["neutral", "happy", "sad", "angry", "fearful", "disgusted", "surprised"], label="Select Style", value="neutral")
98
-
99
- speech_button = gr.Button("Generate Speech")
100
- speech_output = gr.Audio(label="Generated Speech")
101
- speech_message = gr.Textbox(label="Message")
102
-
103
- with gr.Tab("Simple Sound Generation"):
104
- prompt_input = gr.Textbox(label="Sound Description", placeholder="Describe the sound (e.g., rain, wind, bird)...")
105
- duration_input = gr.Slider(label="Duration (seconds)", minimum=1.0, maximum=30.0, step=0.5, value=5.0)
106
- frequency_input = gr.Slider(label="Base Frequency (Hz)", minimum=20, maximum=2000, step=10, value=440)
107
- sound_button = gr.Button("Generate Sound")
108
- sound_output = gr.Audio(label="Generated Sound")
109
- sound_message = gr.Textbox(label="Message")
110
 
111
- def generate_speech(text, method, openai_voice, openai_model, chattts_voice, chattts_style):
112
  if method == "OpenAI TTS":
113
  return openai_tts(text, openai_voice, openai_model)
114
  else:
115
- return chattts(text, chattts_voice, chattts_style)
 
 
 
 
 
 
116
 
117
  speech_button.click(generate_speech,
118
- inputs=[text_input, tts_method, openai_voice_input, openai_model_input, chattts_voice_input, chattts_style_input],
119
  outputs=[speech_output, speech_message])
120
- sound_button.click(generate_simple_sound,
121
- inputs=[prompt_input, duration_input, frequency_input],
122
- outputs=[sound_output, sound_message])
123
 
124
  iface.launch()
 
1
  import gradio as gr
 
2
  import tempfile
3
  import os
4
  from openai import OpenAI
5
+ from gtts import gTTS
6
+ import base64
 
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(
 
25
  except Exception as e:
26
  return None, f"Error in OpenAI TTS speech generation: {str(e)}"
27
 
28
+ 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} 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")
47
+
48
+ with gr.Group() as openai_options:
49
+ gr.Markdown("OpenAI TTS Options")
50
+ openai_voice_input = gr.Dropdown(["alloy", "echo", "fable", "onyx", "nova", "shimmer"], label="Select Voice", value="nova")
51
+ openai_model_input = gr.Dropdown(["tts-1", "tts-1-hd"], label="Select Model", value="tts-1")
52
+
53
+ with gr.Group() as google_options:
54
+ gr.Markdown("Google TTS Options")
55
+ google_lang_input = gr.Dropdown(["en", "es", "fr", "de", "it", "ja", "ko", "pt", "ru", "zh-CN"], label="Select Language", value="en")
56
+ google_tld_input = gr.Dropdown(["com", "co.uk", "com.au", "co.in", "ca", "ie", "co.za"], label="Select TLD (Accent)", value="com")
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, google_tld):
63
  if method == "OpenAI TTS":
64
  return openai_tts(text, openai_voice, openai_model)
65
  else:
66
+ return google_tts(text, google_lang, google_tld)
67
+
68
+ def update_visible_options(method):
69
+ return (
70
+ gr.Group.update(visible=(method == "OpenAI TTS")),
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, google_tld_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()