Spaces:
Running
Running
jonathanagustin
commited on
Commit
•
dac7224
1
Parent(s):
9c9ccd9
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -1,20 +1,3 @@
|
|
1 |
-
"""
|
2 |
-
This script implements a Gradio interface for text-to-speech conversion using OpenAI's API.
|
3 |
-
Users can input text, select a model and voice, and receive an audio output of the synthesized speech.
|
4 |
-
|
5 |
-
Dependencies:
|
6 |
-
- gradio
|
7 |
-
- openai
|
8 |
-
|
9 |
-
Usage:
|
10 |
-
Run the script to launch a web interface for text-to-speech conversion.
|
11 |
-
|
12 |
-
Note:
|
13 |
-
- Ensure that you have installed the required packages:
|
14 |
-
pip install gradio openai
|
15 |
-
- Obtain a valid OpenAI API key with access to the necessary services.
|
16 |
-
"""
|
17 |
-
|
18 |
import gradio as gr
|
19 |
import tempfile
|
20 |
import openai
|
@@ -49,8 +32,24 @@ def tts(input_text: str, model: str, voice: str, api_key: str) -> str:
|
|
49 |
voice=voice,
|
50 |
model=model
|
51 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
except openai.error.OpenAIError as e:
|
53 |
-
raise gr.Error(f"OpenAI API Error: {
|
|
|
|
|
54 |
|
55 |
if not hasattr(response, 'audio'):
|
56 |
raise gr.Error("Invalid response from OpenAI API. The response does not contain audio content.")
|
@@ -74,7 +73,7 @@ def main():
|
|
74 |
with gr.Column(scale=1):
|
75 |
api_key_input = gr.Textbox(
|
76 |
label="API Key",
|
77 |
-
info="Get API
|
78 |
type="password",
|
79 |
placeholder="Enter your OpenAI API Key",
|
80 |
value="",
|
@@ -94,33 +93,60 @@ def main():
|
|
94 |
submit_button = gr.Button(
|
95 |
"Convert Text to Speech",
|
96 |
variant="primary",
|
97 |
-
interactive=
|
98 |
)
|
99 |
with gr.Column(scale=1):
|
100 |
output_audio = gr.Audio(label="Output Audio")
|
101 |
|
102 |
# Define the event handler for the submit button with error handling
|
103 |
def on_submit(input_text, model, voice, api_key):
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
-
|
|
|
108 |
fn=on_submit,
|
109 |
inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
|
110 |
outputs=output_audio,
|
111 |
-
api_name="
|
112 |
)
|
113 |
|
114 |
-
#
|
115 |
-
|
116 |
fn=on_submit,
|
117 |
inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
|
118 |
outputs=output_audio,
|
119 |
-
api_name="
|
120 |
)
|
121 |
|
122 |
-
# Launch the Gradio app with
|
123 |
-
demo.launch(share=True)
|
124 |
|
125 |
if __name__ == "__main__":
|
126 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import tempfile
|
3 |
import openai
|
|
|
32 |
voice=voice,
|
33 |
model=model
|
34 |
)
|
35 |
+
except openai.error.Timeout as e:
|
36 |
+
raise gr.Error(f"OpenAI API request timed out: {e}")
|
37 |
+
except openai.error.APIError as e:
|
38 |
+
raise gr.Error(f"OpenAI API returned an API Error: {e}")
|
39 |
+
except openai.error.APIConnectionError as e:
|
40 |
+
raise gr.Error(f"OpenAI API request failed to connect: {e}")
|
41 |
+
except openai.error.InvalidRequestError as e:
|
42 |
+
raise gr.Error(f"OpenAI API request was invalid: {e}")
|
43 |
+
except openai.error.AuthenticationError as e:
|
44 |
+
raise gr.Error(f"OpenAI API request was not authorized: {e}")
|
45 |
+
except openai.error.PermissionError as e:
|
46 |
+
raise gr.Error(f"OpenAI API request was not permitted: {e}")
|
47 |
+
except openai.error.RateLimitError as e:
|
48 |
+
raise gr.Error(f"OpenAI API request exceeded rate limit: {e}")
|
49 |
except openai.error.OpenAIError as e:
|
50 |
+
raise gr.Error(f"OpenAI API Error: {e}")
|
51 |
+
except Exception as e:
|
52 |
+
raise gr.Error(f"An unexpected error occurred: {e}")
|
53 |
|
54 |
if not hasattr(response, 'audio'):
|
55 |
raise gr.Error("Invalid response from OpenAI API. The response does not contain audio content.")
|
|
|
73 |
with gr.Column(scale=1):
|
74 |
api_key_input = gr.Textbox(
|
75 |
label="API Key",
|
76 |
+
info="Get API key at: [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys)",
|
77 |
type="password",
|
78 |
placeholder="Enter your OpenAI API Key",
|
79 |
value="",
|
|
|
93 |
submit_button = gr.Button(
|
94 |
"Convert Text to Speech",
|
95 |
variant="primary",
|
96 |
+
interactive=False # Initially disabled
|
97 |
)
|
98 |
with gr.Column(scale=1):
|
99 |
output_audio = gr.Audio(label="Output Audio")
|
100 |
|
101 |
# Define the event handler for the submit button with error handling
|
102 |
def on_submit(input_text, model, voice, api_key):
|
103 |
+
try:
|
104 |
+
audio_file = tts(input_text, model, voice, api_key)
|
105 |
+
return audio_file
|
106 |
+
except gr.Error as err:
|
107 |
+
# Re-raise gr.Error exceptions to display message without traceback
|
108 |
+
raise err
|
109 |
+
except Exception as e:
|
110 |
+
# Handle any other exceptions and display error message
|
111 |
+
raise gr.Error(f"An unexpected error occurred: {e}")
|
112 |
+
|
113 |
+
# Function to update the submit button state
|
114 |
+
def update_submit_button_state(api_key, input_text):
|
115 |
+
if api_key.strip() and input_text.strip():
|
116 |
+
return gr.update(interactive=True)
|
117 |
+
else:
|
118 |
+
return gr.update(interactive=False)
|
119 |
+
|
120 |
+
# Update the submit button state when the API key or input text changes
|
121 |
+
api_key_input.change(
|
122 |
+
fn=update_submit_button_state,
|
123 |
+
inputs=[api_key_input, input_textbox],
|
124 |
+
outputs=submit_button
|
125 |
+
)
|
126 |
+
input_textbox.change(
|
127 |
+
fn=update_submit_button_state,
|
128 |
+
inputs=[api_key_input, input_textbox],
|
129 |
+
outputs=submit_button
|
130 |
+
)
|
131 |
|
132 |
+
# Allow pressing Enter in the input textbox to trigger the conversion
|
133 |
+
input_textbox.submit(
|
134 |
fn=on_submit,
|
135 |
inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
|
136 |
outputs=output_audio,
|
137 |
+
api_name="tts",
|
138 |
)
|
139 |
|
140 |
+
# Trigger the conversion when the submit button is clicked
|
141 |
+
submit_button.click(
|
142 |
fn=on_submit,
|
143 |
inputs=[input_textbox, model_dropdown, voice_dropdown, api_key_input],
|
144 |
outputs=output_audio,
|
145 |
+
api_name="tts",
|
146 |
)
|
147 |
|
148 |
+
# Launch the Gradio app with error display enabled
|
149 |
+
demo.launch(share=True, show_error=True)
|
150 |
|
151 |
if __name__ == "__main__":
|
152 |
main()
|