Skip to content

Commit

Permalink
fix empty input
Browse files Browse the repository at this point in the history
  • Loading branch information
leokwsw committed Dec 28, 2023
1 parent 365dc8c commit 99ed665
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
Binary file added 1-second-of-silence.mp3
Binary file not shown.
51 changes: 27 additions & 24 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import sys
from typing import Union
from typing_extensions import Literal
from typing import Union, Literal

import gradio as gr
import tempfile
Expand All @@ -24,30 +23,34 @@ def tts(
text: str,
model: Union[str, Literal["tts-1", "tts-1-hd"]],
voice: Literal["alloy", "echo", "fable", "onyx", "nova", "shimmer"],
output_file_format: Literal["mp3", "opus", "aac", "flac", ""] = "",
output_file_format: Literal["mp3", "opus", "aac", "flac"] = "mp3",
speed: float = 1.0
):
try:
client = OpenAI(api_key=openai_key)

response = client.audio.speech.create(
model=model,
voice=voice,
input=text,
response_format=output_file_format,
speed=speed
)

except Exception as error:
print(str(error))
raise gr.Error("An error occurred while generating speech. Please check your API key and come back try again.")

with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
temp_file.write(response.content)

temp_file_path = temp_file.name

return temp_file_path
if len(text) > 0:
try:
client = OpenAI(api_key=openai_key)

response = client.audio.speech.create(
model=model,
voice=voice,
input=text,
response_format=output_file_format,
speed=speed
)

except Exception as error:
print(str(error))
raise gr.Error(
"An error occurred while generating speech. Please check your API key and come back try again.")

with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file:
temp_file.write(response.content)

temp_file_path = temp_file.name

return temp_file_path
else:
return "1-second-of-silence.mp3"


with gr.Blocks() as demo:
Expand Down

0 comments on commit 99ed665

Please sign in to comment.