Spaces:
Runtime error
Runtime error
jonathanagustin
commited on
Commit
•
cc4f7fd
1
Parent(s):
858b946
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import requests
|
|
5 |
import os
|
6 |
from functools import partial
|
7 |
|
|
|
8 |
def tts(
|
9 |
input_text: str,
|
10 |
model: str,
|
@@ -50,6 +51,16 @@ def tts(
|
|
50 |
response_format=response_format,
|
51 |
speed=speed,
|
52 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
except openai.error.OpenAIError as e:
|
54 |
# Catch OpenAI exceptions
|
55 |
raise gr.Error(f"An OpenAI error occurred: {e}")
|
@@ -57,12 +68,6 @@ def tts(
|
|
57 |
# Catch any other exceptions
|
58 |
raise gr.Error(f"An unexpected error occurred: {e}")
|
59 |
|
60 |
-
# Save the audio content to a temporary file
|
61 |
-
file_extension = f".{response_format}"
|
62 |
-
with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as temp_file:
|
63 |
-
response.stream_to_file(temp_file.name)
|
64 |
-
temp_file_path = temp_file.name
|
65 |
-
|
66 |
return temp_file_path
|
67 |
|
68 |
|
@@ -80,7 +85,7 @@ def main():
|
|
80 |
for voice in VOICE_OPTIONS
|
81 |
}
|
82 |
|
83 |
-
# Download audio previews
|
84 |
PREVIEW_DIR = "voice_previews"
|
85 |
os.makedirs(PREVIEW_DIR, exist_ok=True)
|
86 |
|
@@ -103,6 +108,7 @@ def main():
|
|
103 |
with gr.Blocks(title="OpenAI - Text to Speech") as demo:
|
104 |
with gr.Row():
|
105 |
with gr.Column(scale=1):
|
|
|
106 |
def play_voice_sample(voice: str):
|
107 |
"""
|
108 |
Play the preview audio sample for the selected voice.
|
@@ -116,12 +122,13 @@ def main():
|
|
116 |
value=VOICE_PREVIEW_FILES[voice],
|
117 |
label=f"Preview Voice: {voice.capitalize()}",
|
118 |
)
|
|
|
119 |
with gr.Group():
|
120 |
|
121 |
preview_audio = gr.Audio(
|
122 |
interactive=False,
|
123 |
label="Preview Voice: Echo",
|
124 |
-
value=VOICE_PREVIEW_FILES[
|
125 |
visible=True,
|
126 |
show_download_button=False,
|
127 |
show_share_button=False,
|
@@ -196,7 +203,7 @@ def main():
|
|
196 |
fn=update_label,
|
197 |
inputs=input_textbox,
|
198 |
outputs=input_textbox,
|
199 |
-
show_progress=
|
200 |
)
|
201 |
|
202 |
# Initialize the submit button as non-interactive
|
@@ -218,10 +225,14 @@ def main():
|
|
218 |
"""
|
219 |
if api_key.strip():
|
220 |
# There is an API key, enable the submit button
|
221 |
-
return gr.update(
|
|
|
|
|
222 |
else:
|
223 |
# No API key, disable the submit button
|
224 |
-
return gr.update(
|
|
|
|
|
225 |
|
226 |
# Update the submit button whenever the API Key input changes
|
227 |
api_key_input.input(
|
@@ -231,10 +242,19 @@ def main():
|
|
231 |
)
|
232 |
|
233 |
with gr.Column(scale=1):
|
234 |
-
output_audio = gr.Audio(
|
|
|
|
|
|
|
|
|
235 |
|
236 |
def on_submit(
|
237 |
-
input_text: str,
|
|
|
|
|
|
|
|
|
|
|
238 |
) -> str:
|
239 |
"""
|
240 |
Event handler for the submit button; converts text to speech using the tts function.
|
@@ -254,9 +274,7 @@ def main():
|
|
254 |
:return: File path to the generated audio file.
|
255 |
:rtype: str
|
256 |
"""
|
257 |
-
audio_file = tts(
|
258 |
-
input_text, model, voice, api_key, response_format, speed
|
259 |
-
)
|
260 |
return audio_file
|
261 |
|
262 |
# Trigger the conversion when the submit button is clicked
|
@@ -276,5 +294,6 @@ def main():
|
|
276 |
# Launch the Gradio app with error display enabled
|
277 |
demo.launch(show_error=True)
|
278 |
|
|
|
279 |
if __name__ == "__main__":
|
280 |
main()
|
|
|
5 |
import os
|
6 |
from functools import partial
|
7 |
|
8 |
+
|
9 |
def tts(
|
10 |
input_text: str,
|
11 |
model: str,
|
|
|
51 |
response_format=response_format,
|
52 |
speed=speed,
|
53 |
)
|
54 |
+
|
55 |
+
# Save the audio content to a temporary file
|
56 |
+
file_extension = f".{response_format}"
|
57 |
+
with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as temp_file:
|
58 |
+
temp_file_path = temp_file.name
|
59 |
+
|
60 |
+
with response.with_streaming_response():
|
61 |
+
for chunk in response:
|
62 |
+
temp_file.write(chunk)
|
63 |
+
|
64 |
except openai.error.OpenAIError as e:
|
65 |
# Catch OpenAI exceptions
|
66 |
raise gr.Error(f"An OpenAI error occurred: {e}")
|
|
|
68 |
# Catch any other exceptions
|
69 |
raise gr.Error(f"An unexpected error occurred: {e}")
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
return temp_file_path
|
72 |
|
73 |
|
|
|
85 |
for voice in VOICE_OPTIONS
|
86 |
}
|
87 |
|
88 |
+
# Download audio previews before initiating the interface
|
89 |
PREVIEW_DIR = "voice_previews"
|
90 |
os.makedirs(PREVIEW_DIR, exist_ok=True)
|
91 |
|
|
|
108 |
with gr.Blocks(title="OpenAI - Text to Speech") as demo:
|
109 |
with gr.Row():
|
110 |
with gr.Column(scale=1):
|
111 |
+
|
112 |
def play_voice_sample(voice: str):
|
113 |
"""
|
114 |
Play the preview audio sample for the selected voice.
|
|
|
122 |
value=VOICE_PREVIEW_FILES[voice],
|
123 |
label=f"Preview Voice: {voice.capitalize()}",
|
124 |
)
|
125 |
+
|
126 |
with gr.Group():
|
127 |
|
128 |
preview_audio = gr.Audio(
|
129 |
interactive=False,
|
130 |
label="Preview Voice: Echo",
|
131 |
+
value=VOICE_PREVIEW_FILES["echo"],
|
132 |
visible=True,
|
133 |
show_download_button=False,
|
134 |
show_share_button=False,
|
|
|
203 |
fn=update_label,
|
204 |
inputs=input_textbox,
|
205 |
outputs=input_textbox,
|
206 |
+
show_progress="hidden", # Hide the progress indicator
|
207 |
)
|
208 |
|
209 |
# Initialize the submit button as non-interactive
|
|
|
225 |
"""
|
226 |
if api_key.strip():
|
227 |
# There is an API key, enable the submit button
|
228 |
+
return gr.update(
|
229 |
+
value="Convert Text to Speech", interactive=True
|
230 |
+
)
|
231 |
else:
|
232 |
# No API key, disable the submit button
|
233 |
+
return gr.update(
|
234 |
+
value="Enter OpenAI API Key", interactive=False
|
235 |
+
)
|
236 |
|
237 |
# Update the submit button whenever the API Key input changes
|
238 |
api_key_input.input(
|
|
|
242 |
)
|
243 |
|
244 |
with gr.Column(scale=1):
|
245 |
+
output_audio = gr.Audio(
|
246 |
+
label="Output Audio",
|
247 |
+
show_download_button=False,
|
248 |
+
show_share_button=False,
|
249 |
+
)
|
250 |
|
251 |
def on_submit(
|
252 |
+
input_text: str,
|
253 |
+
model: str,
|
254 |
+
voice: str,
|
255 |
+
api_key: str,
|
256 |
+
response_format: str,
|
257 |
+
speed: float,
|
258 |
) -> str:
|
259 |
"""
|
260 |
Event handler for the submit button; converts text to speech using the tts function.
|
|
|
274 |
:return: File path to the generated audio file.
|
275 |
:rtype: str
|
276 |
"""
|
277 |
+
audio_file = tts(input_text, model, voice, api_key, response_format, speed)
|
|
|
|
|
278 |
return audio_file
|
279 |
|
280 |
# Trigger the conversion when the submit button is clicked
|
|
|
294 |
# Launch the Gradio app with error display enabled
|
295 |
demo.launch(show_error=True)
|
296 |
|
297 |
+
|
298 |
if __name__ == "__main__":
|
299 |
main()
|