jonathanagustin commited on
Commit
9813b68
1 Parent(s): bc73de2

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +31 -21
app.py CHANGED
@@ -3,6 +3,7 @@ import tempfile
3
  import openai
4
  import requests
5
 
 
6
  def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
7
  """
8
  Convert input text to speech using OpenAI's Text-to-Speech API.
@@ -20,7 +21,9 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
20
  gr.Error: If input parameters are invalid or API call fails.
21
  """
22
  if not api_key.strip():
23
- raise gr.Error("API key is required. Get an API key at: https://platform.openai.com/account/api-keys")
 
 
24
 
25
  if not input_text.strip():
26
  raise gr.Error("Input text cannot be empty.")
@@ -28,11 +31,7 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
28
  openai.api_key = api_key
29
 
30
  try:
31
- response = openai.Audio.create(
32
- text=input_text,
33
- voice=voice,
34
- model=model
35
- )
36
  except openai.OpenAIError as e:
37
  # Catch-all for OpenAI exceptions
38
  raise gr.Error(f"An OpenAI error occurred: {e}")
@@ -40,8 +39,10 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
40
  # Catch any other exceptions
41
  raise gr.Error(f"An unexpected error occurred: {e}")
42
 
43
- if not hasattr(response, 'audio'):
44
- raise gr.Error("Invalid response from OpenAI API. The response does not contain audio content.")
 
 
45
 
46
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
47
  temp_file.write(response.audio)
@@ -49,6 +50,7 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
49
 
50
  return temp_file_path
51
 
 
52
  def main():
53
  """
54
  Main function to create and launch the Gradio interface.
@@ -58,7 +60,8 @@ def main():
58
 
59
  # Predefine voice previews URLs
60
  VOICE_PREVIEWS = {
61
- voice: f"https://cdn.openai.com/API/docs/audio/{voice}.wav" for voice in VOICE_OPTIONS
 
62
  }
63
 
64
  with gr.Blocks() as demo:
@@ -80,27 +83,33 @@ def main():
80
  # Add voice previews
81
  gr.Markdown("### Voice Previews")
82
  for voice in VOICE_OPTIONS:
83
- gr.Markdown(f"**{voice.capitalize()}**")
84
  audio_url = VOICE_PREVIEWS[voice]
85
  # Fetch the audio data
86
  try:
87
  response = requests.get(audio_url)
88
  response.raise_for_status()
89
  audio_data = response.content
90
- gr.Audio(value=audio_data, label=f"{voice.capitalize()}", autoplay=False)
 
 
 
 
 
 
 
 
 
 
91
  except requests.exceptions.RequestException as e:
92
- gr.Markdown(f"Could not load preview for {voice.capitalize()}: {e}")
 
 
93
 
94
  with gr.Column(scale=2):
95
  input_textbox = gr.Textbox(
96
- label="Input Text",
97
- lines=10,
98
- placeholder="Type your text here..."
99
- )
100
- submit_button = gr.Button(
101
- "Convert Text to Speech",
102
- variant="primary"
103
  )
 
104
  with gr.Column(scale=1):
105
  output_audio = gr.Audio(label="Output Audio")
106
 
@@ -113,11 +122,12 @@ def main():
113
  submit_button.click(
114
  fn=on_submit,
115
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
116
- outputs=output_audio
117
  )
118
 
119
  # Launch the Gradio app with error display enabled
120
  demo.launch(show_error=True)
121
 
 
122
  if __name__ == "__main__":
123
- main()
 
3
  import openai
4
  import requests
5
 
6
+
7
  def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
8
  """
9
  Convert input text to speech using OpenAI's Text-to-Speech API.
 
21
  gr.Error: If input parameters are invalid or API call fails.
22
  """
23
  if not api_key.strip():
24
+ raise gr.Error(
25
+ "API key is required. Get an API key at: https://platform.openai.com/account/api-keys"
26
+ )
27
 
28
  if not input_text.strip():
29
  raise gr.Error("Input text cannot be empty.")
 
31
  openai.api_key = api_key
32
 
33
  try:
34
+ response = openai.Audio.create(text=input_text, voice=voice, model=model)
 
 
 
 
35
  except openai.OpenAIError as e:
36
  # Catch-all for OpenAI exceptions
37
  raise gr.Error(f"An OpenAI error occurred: {e}")
 
39
  # Catch any other exceptions
40
  raise gr.Error(f"An unexpected error occurred: {e}")
41
 
42
+ if not hasattr(response, "audio"):
43
+ raise gr.Error(
44
+ "Invalid response from OpenAI API. The response does not contain audio content."
45
+ )
46
 
47
  with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
48
  temp_file.write(response.audio)
 
50
 
51
  return temp_file_path
52
 
53
+
54
  def main():
55
  """
56
  Main function to create and launch the Gradio interface.
 
60
 
61
  # Predefine voice previews URLs
62
  VOICE_PREVIEWS = {
63
+ voice: f"https://cdn.openai.com/API/docs/audio/{voice}.wav"
64
+ for voice in VOICE_OPTIONS
65
  }
66
 
67
  with gr.Blocks() as demo:
 
83
  # Add voice previews
84
  gr.Markdown("### Voice Previews")
85
  for voice in VOICE_OPTIONS:
 
86
  audio_url = VOICE_PREVIEWS[voice]
87
  # Fetch the audio data
88
  try:
89
  response = requests.get(audio_url)
90
  response.raise_for_status()
91
  audio_data = response.content
92
+ gr.Audio(
93
+ value=audio_data,
94
+ waveform_options=gr.WaveformOptions(
95
+ waveform_color="#01C6FF",
96
+ waveform_progress_color="#0066B4",
97
+ skip_length=2,
98
+ show_controls=False,
99
+ ),
100
+ label=f"{voice.capitalize()}",
101
+ autoplay=False,
102
+ )
103
  except requests.exceptions.RequestException as e:
104
+ gr.Markdown(
105
+ f"Could not load preview for {voice.capitalize()}: {e}"
106
+ )
107
 
108
  with gr.Column(scale=2):
109
  input_textbox = gr.Textbox(
110
+ label="Input Text", lines=10, placeholder="Type your text here..."
 
 
 
 
 
 
111
  )
112
+ submit_button = gr.Button("Convert Text to Speech", variant="primary")
113
  with gr.Column(scale=1):
114
  output_audio = gr.Audio(label="Output Audio")
115
 
 
122
  submit_button.click(
123
  fn=on_submit,
124
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
125
+ outputs=output_audio,
126
  )
127
 
128
  # Launch the Gradio app with error display enabled
129
  demo.launch(show_error=True)
130
 
131
+
132
  if __name__ == "__main__":
133
+ main()