Spaces:
Running
Running
jonathanagustin
commited on
Commit
•
5814747
1
Parent(s):
4e85e92
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -16,10 +16,58 @@ def tts(
|
|
16 |
"""
|
17 |
Convert input text to speech using OpenAI's Text-to-Speech API.
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
"""
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def main():
|
25 |
"""
|
@@ -62,7 +110,8 @@ def main():
|
|
62 |
value=VOICE_PREVIEW_FILES['echo'],
|
63 |
visible=True,
|
64 |
show_download_button=False,
|
65 |
-
show_share_button=False
|
|
|
66 |
)
|
67 |
|
68 |
with gr.Blocks(title="OpenAI - Text to Speech") as demo:
|
@@ -125,24 +174,24 @@ def main():
|
|
125 |
)
|
126 |
|
127 |
with gr.Column(scale=2):
|
|
|
128 |
input_textbox = gr.Textbox(
|
129 |
-
label="Input Text",
|
130 |
lines=10,
|
131 |
placeholder="Type your text here...",
|
132 |
)
|
133 |
-
# Add a character counter below the input textbox
|
134 |
-
char_count_text = gr.Markdown("0 / 4096")
|
135 |
|
136 |
-
# Function to update the character count
|
137 |
-
def
|
138 |
char_count = len(input_text)
|
139 |
-
|
|
|
140 |
|
141 |
-
# Update
|
142 |
input_textbox.change(
|
143 |
-
fn=
|
144 |
inputs=input_textbox,
|
145 |
-
outputs=
|
146 |
)
|
147 |
|
148 |
submit_button = gr.Button(
|
@@ -178,6 +227,5 @@ def main():
|
|
178 |
# Launch the Gradio app with error display enabled
|
179 |
demo.launch(show_error=True)
|
180 |
|
181 |
-
|
182 |
if __name__ == "__main__":
|
183 |
-
main()
|
|
|
16 |
"""
|
17 |
Convert input text to speech using OpenAI's Text-to-Speech API.
|
18 |
|
19 |
+
Parameters:
|
20 |
+
input_text (str): The text to be converted to speech.
|
21 |
+
model (str): The model to use for synthesis (e.g., 'tts-1', 'tts-1-hd').
|
22 |
+
voice (str): The voice profile to use (e.g., 'alloy', 'echo', 'fable', etc.).
|
23 |
+
api_key (str): OpenAI API key.
|
24 |
+
response_format (str): The audio format of the output file (default is 'mp3').
|
25 |
+
speed (float): The speed of the synthesized speech.
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
str: File path to the generated audio file.
|
29 |
+
|
30 |
+
Raises:
|
31 |
+
gr.Error: If input parameters are invalid or API call fails.
|
32 |
"""
|
33 |
+
if not api_key.strip():
|
34 |
+
raise gr.Error(
|
35 |
+
"API key is required. Get an API key at: https://platform.openai.com/account/api-keys"
|
36 |
+
)
|
37 |
+
|
38 |
+
if not input_text.strip():
|
39 |
+
raise gr.Error("Input text cannot be empty.")
|
40 |
+
|
41 |
+
openai.api_key = api_key
|
42 |
+
|
43 |
+
try:
|
44 |
+
response = openai.Audio.create(
|
45 |
+
text=input_text,
|
46 |
+
voice=voice,
|
47 |
+
model=model,
|
48 |
+
response_format=response_format,
|
49 |
+
speed=speed,
|
50 |
+
)
|
51 |
+
except openai.OpenAIError as e:
|
52 |
+
# Catch-all for OpenAI exceptions
|
53 |
+
raise gr.Error(f"An OpenAI error occurred: {e}")
|
54 |
+
except Exception as e:
|
55 |
+
# Catch any other exceptions
|
56 |
+
raise gr.Error(f"An unexpected error occurred: {e}")
|
57 |
+
|
58 |
+
if not hasattr(response, "audio"):
|
59 |
+
raise gr.Error(
|
60 |
+
"Invalid response from OpenAI API. The response does not contain audio content."
|
61 |
+
)
|
62 |
+
|
63 |
+
# Save the audio content to a temporary file
|
64 |
+
audio_content = response.audio
|
65 |
+
file_extension = f".{response_format}"
|
66 |
+
with tempfile.NamedTemporaryFile(suffix=file_extension, delete=False) as temp_file:
|
67 |
+
temp_file.write(audio_content)
|
68 |
+
temp_file_path = temp_file.name
|
69 |
+
|
70 |
+
return temp_file_path
|
71 |
|
72 |
def main():
|
73 |
"""
|
|
|
110 |
value=VOICE_PREVIEW_FILES['echo'],
|
111 |
visible=True,
|
112 |
show_download_button=False,
|
113 |
+
show_share_button=False,
|
114 |
+
autoplay=True,
|
115 |
)
|
116 |
|
117 |
with gr.Blocks(title="OpenAI - Text to Speech") as demo:
|
|
|
174 |
)
|
175 |
|
176 |
with gr.Column(scale=2):
|
177 |
+
# Initialize the input textbox with the desired label
|
178 |
input_textbox = gr.Textbox(
|
179 |
+
label="Input Text (0000 / 4096 chars)",
|
180 |
lines=10,
|
181 |
placeholder="Type your text here...",
|
182 |
)
|
|
|
|
|
183 |
|
184 |
+
# Function to update the label with the character count
|
185 |
+
def update_label(input_text):
|
186 |
char_count = len(input_text)
|
187 |
+
new_label = f"Input Text ({char_count:04d} / 4096 chars)"
|
188 |
+
return gr.update(label=new_label)
|
189 |
|
190 |
+
# Update the label when the text changes
|
191 |
input_textbox.change(
|
192 |
+
fn=update_label,
|
193 |
inputs=input_textbox,
|
194 |
+
outputs=input_textbox,
|
195 |
)
|
196 |
|
197 |
submit_button = gr.Button(
|
|
|
227 |
# Launch the Gradio app with error display enabled
|
228 |
demo.launch(show_error=True)
|
229 |
|
|
|
230 |
if __name__ == "__main__":
|
231 |
+
main()
|