Spaces:
Running
Running
jonathanagustin
commited on
Commit
•
055f404
1
Parent(s):
7b9da97
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -16,75 +16,10 @@ def tts(
|
|
16 |
"""
|
17 |
Convert input text to speech using OpenAI's Text-to-Speech API.
|
18 |
|
19 |
-
|
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 to use when generating the audio.
|
23 |
-
api_key (str): OpenAI API key.
|
24 |
-
response_format (str): Format of the output audio. Defaults to 'mp3'.
|
25 |
-
speed (float): Speed of the generated audio. Defaults to 1.0.
|
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 |
-
|
34 |
-
|
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 |
-
if len(input_text) > 4096:
|
42 |
-
raise gr.Error("Input text exceeds the maximum length of 4096 characters.")
|
43 |
-
|
44 |
-
if speed < 0.25 or speed > 4.0:
|
45 |
-
raise gr.Error("Speed must be between 0.25 and 4.0.")
|
46 |
-
|
47 |
-
headers = {
|
48 |
-
"Authorization": f"Bearer {api_key}",
|
49 |
-
"Content-Type": "application/json",
|
50 |
-
}
|
51 |
-
|
52 |
-
data = {
|
53 |
-
"model": model,
|
54 |
-
"input": input_text,
|
55 |
-
"voice": voice,
|
56 |
-
"response_format": response_format,
|
57 |
-
"speed": speed,
|
58 |
-
}
|
59 |
-
|
60 |
-
try:
|
61 |
-
response = requests.post(
|
62 |
-
"https://api.openai.com/v1/audio/speech",
|
63 |
-
headers=headers,
|
64 |
-
json=data,
|
65 |
-
)
|
66 |
-
response.raise_for_status()
|
67 |
-
except requests.exceptions.HTTPError as http_err:
|
68 |
-
raise gr.Error(f"HTTP error occurred: {http_err} - {response.text}")
|
69 |
-
except Exception as err:
|
70 |
-
raise gr.Error(f"An error occurred: {err}")
|
71 |
-
|
72 |
-
# The content will be the audio file content
|
73 |
-
audio_content = response.content
|
74 |
-
|
75 |
-
file_extension = response_format.lower()
|
76 |
-
# PCM is raw data, so it does not have a standard file extension
|
77 |
-
if file_extension == "pcm":
|
78 |
-
file_extension = "raw"
|
79 |
-
|
80 |
-
with tempfile.NamedTemporaryFile(
|
81 |
-
suffix=f".{file_extension}", delete=False
|
82 |
-
) as temp_file:
|
83 |
-
temp_file.write(audio_content)
|
84 |
-
temp_file_path = temp_file.name
|
85 |
-
|
86 |
-
return temp_file_path
|
87 |
-
|
88 |
|
89 |
def main():
|
90 |
"""
|
@@ -121,20 +56,26 @@ def main():
|
|
121 |
gr.set_static_paths(paths=[PREVIEW_DIR])
|
122 |
|
123 |
with gr.Blocks(title="OpenAI - Text to Speech") as demo:
|
|
|
124 |
with gr.Row():
|
125 |
with gr.Column(scale=1):
|
126 |
with gr.Group():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
preview_audio = gr.Audio(
|
128 |
interactive=False,
|
129 |
-
label="
|
130 |
-
value=
|
131 |
visible=True,
|
132 |
)
|
133 |
|
134 |
-
# Function to play the selected voice sample
|
135 |
-
def play_voice_sample(voice):
|
136 |
-
return gr.update(value=VOICE_PREVIEW_FILES[voice])
|
137 |
-
|
138 |
# Create buttons for each voice
|
139 |
for voice in VOICE_OPTIONS:
|
140 |
voice_button = gr.Button(
|
@@ -146,6 +87,10 @@ def main():
|
|
146 |
fn=partial(play_voice_sample, voice=voice),
|
147 |
outputs=preview_audio,
|
148 |
)
|
|
|
|
|
|
|
|
|
149 |
with gr.Column(scale=1):
|
150 |
api_key_input = gr.Textbox(
|
151 |
label="OpenAI API Key",
|
@@ -178,33 +123,33 @@ def main():
|
|
178 |
value=1.0,
|
179 |
)
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
|
209 |
# Define the event handler for the submit button with error handling
|
210 |
def on_submit(
|
|
|
16 |
"""
|
17 |
Convert input text to speech using OpenAI's Text-to-Speech API.
|
18 |
|
19 |
+
(Function definition remains the same)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
"""
|
21 |
+
# (Function body remains the same)
|
22 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def main():
|
25 |
"""
|
|
|
56 |
gr.set_static_paths(paths=[PREVIEW_DIR])
|
57 |
|
58 |
with gr.Blocks(title="OpenAI - Text to Speech") as demo:
|
59 |
+
gr.Markdown("# OpenAI Text-to-Speech Demo")
|
60 |
with gr.Row():
|
61 |
with gr.Column(scale=1):
|
62 |
with gr.Group():
|
63 |
+
gr.Markdown("### Voice Preview")
|
64 |
+
# Function to play the selected voice sample
|
65 |
+
def play_voice_sample(voice):
|
66 |
+
return gr.Audio.update(
|
67 |
+
value=VOICE_PREVIEW_FILES[voice],
|
68 |
+
label=voice.capitalize(),
|
69 |
+
)
|
70 |
+
|
71 |
+
# Create the 'preview_audio' component
|
72 |
preview_audio = gr.Audio(
|
73 |
interactive=False,
|
74 |
+
label="Echo",
|
75 |
+
value=VOICE_PREVIEW_FILES['echo'],
|
76 |
visible=True,
|
77 |
)
|
78 |
|
|
|
|
|
|
|
|
|
79 |
# Create buttons for each voice
|
80 |
for voice in VOICE_OPTIONS:
|
81 |
voice_button = gr.Button(
|
|
|
87 |
fn=partial(play_voice_sample, voice=voice),
|
88 |
outputs=preview_audio,
|
89 |
)
|
90 |
+
|
91 |
+
# Place the audio player below the buttons
|
92 |
+
preview_audio.render()
|
93 |
+
|
94 |
with gr.Column(scale=1):
|
95 |
api_key_input = gr.Textbox(
|
96 |
label="OpenAI API Key",
|
|
|
123 |
value=1.0,
|
124 |
)
|
125 |
|
126 |
+
with gr.Column(scale=2):
|
127 |
+
input_textbox = gr.Textbox(
|
128 |
+
label="Input Text",
|
129 |
+
lines=10,
|
130 |
+
placeholder="Type your text here...",
|
131 |
+
)
|
132 |
+
# Add a character counter below the input textbox
|
133 |
+
char_count_text = gr.Markdown("0 / 4096")
|
134 |
+
|
135 |
+
# Function to update the character count
|
136 |
+
def update_char_count(input_text):
|
137 |
+
char_count = len(input_text)
|
138 |
+
return f"**{char_count} / 4096**"
|
139 |
+
|
140 |
+
# Update character count when the user stops typing
|
141 |
+
input_textbox.change(
|
142 |
+
fn=update_char_count,
|
143 |
+
inputs=input_textbox,
|
144 |
+
outputs=char_count_text,
|
145 |
+
)
|
146 |
|
147 |
+
submit_button = gr.Button(
|
148 |
+
"Convert Text to Speech",
|
149 |
+
variant="primary",
|
150 |
+
)
|
151 |
+
with gr.Column(scale=1):
|
152 |
+
output_audio = gr.Audio(label="Output Audio")
|
153 |
|
154 |
# Define the event handler for the submit button with error handling
|
155 |
def on_submit(
|