Spaces:
Runtime error
Runtime error
added error handling for incorrect keys
Browse files
app.py
CHANGED
|
@@ -5,11 +5,26 @@ from openai import OpenAI
|
|
| 5 |
|
| 6 |
|
| 7 |
def tts(text, model, voice, api_key):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
client = OpenAI(api_key=api_key)
|
| 10 |
response = client.audio.speech.create(
|
| 11 |
-
model=model, #"tts-1","tts-1-hd"
|
| 12 |
-
voice=voice, #'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
|
| 13 |
input=text,
|
| 14 |
)
|
| 15 |
|
|
@@ -35,7 +50,7 @@ with gr.Blocks() as demo:
|
|
| 35 |
btn = gr.Button("Text-To-Speech")
|
| 36 |
output_audio = gr.Audio(label="Speech Output")
|
| 37 |
|
| 38 |
-
text.submit(fn=tts, inputs=[text, model, voice, api_key], outputs=output_audio, api_name="
|
| 39 |
-
btn.click(fn=tts, inputs=[text, model, voice, api_key], outputs=output_audio, api_name="
|
| 40 |
|
| 41 |
-
demo.launch()
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
def tts(text, model, voice, api_key):
|
| 8 |
+
if api_key == '':
|
| 9 |
+
raise gr.Error('Please enter your OpenAI API Key')
|
| 10 |
+
else:
|
| 11 |
+
try:
|
| 12 |
+
client = openai.OpenAI(api_key=api_key)
|
| 13 |
+
|
| 14 |
+
response = client.audio.speech.create(
|
| 15 |
+
model='tts-1', # "tts-1","tts-1-hd"
|
| 16 |
+
voice='alloy', # 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
|
| 17 |
+
input='input text',
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
except Exception as error:
|
| 21 |
+
# Handle any exception that occurs
|
| 22 |
+
raise gr.Error("An error occurred while generating speech. Please check your API key and try again.")
|
| 23 |
+
print(str(error))
|
| 24 |
|
|
|
|
| 25 |
response = client.audio.speech.create(
|
| 26 |
+
model=model, # expected values: "tts-1","tts-1-hd"
|
| 27 |
+
voice=voice, # expected values: 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'
|
| 28 |
input=text,
|
| 29 |
)
|
| 30 |
|
|
|
|
| 50 |
btn = gr.Button("Text-To-Speech")
|
| 51 |
output_audio = gr.Audio(label="Speech Output")
|
| 52 |
|
| 53 |
+
text.submit(fn=tts, inputs=[text, model, voice, api_key], outputs=output_audio, api_name="tts_enter_key", concurrency_limit=None)
|
| 54 |
+
btn.click(fn=tts, inputs=[text, model, voice, api_key], outputs=output_audio, api_name="tts_button", concurrency_limit=None)
|
| 55 |
|
| 56 |
+
demo.launch()
|