jonathanagustin commited on
Commit
97e46d3
1 Parent(s): 81c4ffe

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +22 -81
app.py CHANGED
@@ -6,24 +6,24 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
6
  """
7
  Convert input text to speech using OpenAI's Text-to-Speech API.
8
 
9
- :param input_text: The text to be converted to speech.
10
- :type input_text: str
11
- :param model: The model to use for synthesis (e.g., 'tts-1', 'tts-1-hd').
12
- :type model: str
13
- :param voice: The voice profile to use (e.g., 'alloy', 'echo', 'fable', etc.).
14
- :type voice: str
15
- :param api_key: OpenAI API key.
16
- :type api_key: str
17
- :return: File path to the generated audio file.
18
- :rtype: str
19
- :raises gr.Error: If input parameters are invalid or API call fails.
20
  """
 
 
 
21
  if not input_text.strip():
22
  raise gr.Error("Input text cannot be empty.")
23
 
24
- if not api_key.strip():
25
- raise gr.Error("API key is required.")
26
-
27
  openai.api_key = api_key
28
 
29
  try:
@@ -32,30 +32,8 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
32
  voice=voice,
33
  model=model
34
  )
35
-
36
- except openai.APITimeoutError as e:
37
- raise gr.Error(f"OpenAI API request timed out: {e}")
38
- except openai.APIConnectionError as e:
39
- raise gr.Error(f"OpenAI API request failed to connect: {e}")
40
- except openai.AuthenticationError as e:
41
- raise gr.Error(f"OpenAI API request was not authorized: {e}")
42
- except openai.PermissionDeniedError as e:
43
- raise gr.Error(f"OpenAI API request was not permitted: {e}")
44
- except openai.RateLimitError as e:
45
- raise gr.Error(f"OpenAI API request exceeded rate limit: {e}")
46
- except openai.BadRequestError as e:
47
- raise gr.Error(f"OpenAI API bad request: {e}")
48
- except openai.UnprocessableEntityError as e:
49
- raise gr.Error(f"OpenAI API request could not be processed: {e}")
50
- except openai.NotFoundError as e:
51
- raise gr.Error(f"OpenAI API resource not found: {e}")
52
- except openai.InternalServerError as e:
53
- raise gr.Error(f"OpenAI API internal server error: {e}")
54
- except openai.APIError as e:
55
- # This will catch other API-related errors not captured above
56
- raise gr.Error(f"OpenAI API Error: {e}")
57
  except openai.OpenAIError as e:
58
- # Catch-all for any other OpenAI exceptions
59
  raise gr.Error(f"An OpenAI error occurred: {e}")
60
  except Exception as e:
61
  # Catch any other exceptions
@@ -72,7 +50,7 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
72
 
73
  def main():
74
  """
75
- Main function to create and launch the Gradio interface with input validation and error handling.
76
  """
77
  MODEL_OPTIONS = ["tts-1", "tts-1-hd"]
78
  VOICE_OPTIONS = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
@@ -82,10 +60,9 @@ def main():
82
  with gr.Column(scale=1):
83
  api_key_input = gr.Textbox(
84
  label="OpenAI API Key",
85
- info="Get OpenAI key at: https://platform.openai.com/account/api-keys",
86
  type="password",
87
  placeholder="Enter your OpenAI API Key",
88
- value="",
89
  )
90
  model_dropdown = gr.Dropdown(
91
  choices=MODEL_OPTIONS, label="Model", value="tts-1"
@@ -101,61 +78,25 @@ def main():
101
  )
102
  submit_button = gr.Button(
103
  "Convert Text to Speech",
104
- variant="primary",
105
- interactive=False # Initially disabled
106
  )
107
  with gr.Column(scale=1):
108
  output_audio = gr.Audio(label="Output Audio")
109
 
110
  # Define the event handler for the submit button with error handling
111
  def on_submit(input_text, model, voice, api_key):
112
- try:
113
- audio_file = tts(input_text, model, voice, api_key)
114
- return audio_file
115
- except gr.Error as err:
116
- # Re-raise gr.Error exceptions to display message without traceback
117
- raise err
118
- except Exception as e:
119
- # Handle any other exceptions and display error message
120
- raise gr.Error(f"An unexpected error occurred: {e}")
121
-
122
- # Function to update the submit button state
123
- def update_submit_button_state(api_key, input_text):
124
- if api_key.strip() and input_text.strip():
125
- return gr.update(interactive=True)
126
- else:
127
- return gr.update(interactive=False)
128
-
129
- # Update the submit button state when the API key or input text changes
130
- api_key_input.change(
131
- fn=update_submit_button_state,
132
- inputs=[api_key_input, input_textbox],
133
- outputs=submit_button
134
- )
135
- input_textbox.change(
136
- fn=update_submit_button_state,
137
- inputs=[api_key_input, input_textbox],
138
- outputs=submit_button
139
- )
140
-
141
- # Allow pressing Enter in the input textbox to trigger the conversion
142
- input_textbox.submit(
143
- fn=on_submit,
144
- inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
145
- outputs=output_audio,
146
- api_name="tts",
147
- )
148
 
149
  # Trigger the conversion when the submit button is clicked
150
  submit_button.click(
151
  fn=on_submit,
152
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
153
- outputs=output_audio,
154
- api_name="tts",
155
  )
156
 
157
  # Launch the Gradio app with error display enabled
158
  demo.launch(show_error=True)
159
 
160
  if __name__ == "__main__":
161
- main()
 
6
  """
7
  Convert input text to speech using OpenAI's Text-to-Speech API.
8
 
9
+ Parameters:
10
+ input_text (str): The text to be converted to speech.
11
+ model (str): The model to use for synthesis (e.g., 'tts-1', 'tts-1-hd').
12
+ voice (str): The voice profile to use (e.g., 'alloy', 'echo', 'fable', etc.).
13
+ api_key (str): OpenAI API key.
14
+
15
+ Returns:
16
+ str: File path to the generated audio file.
17
+
18
+ Raises:
19
+ gr.Error: If input parameters are invalid or API call fails.
20
  """
21
+ if not api_key.strip():
22
+ raise gr.Error("API key is required. Get an API key at: https://platform.openai.com/account/api-keys")
23
+
24
  if not input_text.strip():
25
  raise gr.Error("Input text cannot be empty.")
26
 
 
 
 
27
  openai.api_key = api_key
28
 
29
  try:
 
32
  voice=voice,
33
  model=model
34
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  except openai.OpenAIError as e:
36
+ # Catch-all for OpenAI exceptions
37
  raise gr.Error(f"An OpenAI error occurred: {e}")
38
  except Exception as e:
39
  # Catch any other exceptions
 
50
 
51
  def main():
52
  """
53
+ Main function to create and launch the Gradio interface.
54
  """
55
  MODEL_OPTIONS = ["tts-1", "tts-1-hd"]
56
  VOICE_OPTIONS = ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
 
60
  with gr.Column(scale=1):
61
  api_key_input = gr.Textbox(
62
  label="OpenAI API Key",
63
+ info="Get OpenAI API Key: https://platform.openai.com/account/api-keys",
64
  type="password",
65
  placeholder="Enter your OpenAI API Key",
 
66
  )
67
  model_dropdown = gr.Dropdown(
68
  choices=MODEL_OPTIONS, label="Model", value="tts-1"
 
78
  )
79
  submit_button = gr.Button(
80
  "Convert Text to Speech",
81
+ variant="primary"
 
82
  )
83
  with gr.Column(scale=1):
84
  output_audio = gr.Audio(label="Output Audio")
85
 
86
  # Define the event handler for the submit button with error handling
87
  def on_submit(input_text, model, voice, api_key):
88
+ audio_file = tts(input_text, model, voice, api_key)
89
+ return audio_file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  # Trigger the conversion when the submit button is clicked
92
  submit_button.click(
93
  fn=on_submit,
94
  inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
95
+ outputs=output_audio
 
96
  )
97
 
98
  # Launch the Gradio app with error display enabled
99
  demo.launch(show_error=True)
100
 
101
  if __name__ == "__main__":
102
+ main()