Spaces:
Running
Running
jonathanagustin
commited on
Commit
•
2c9d387
1
Parent(s):
0bb3772
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -44,23 +44,25 @@ def tts(
|
|
44 |
openai.api_key = api_key
|
45 |
|
46 |
try:
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
for chunk in response:
|
62 |
temp_file.write(chunk)
|
63 |
-
|
64 |
except openai.OpenAIError as e:
|
65 |
# Catch OpenAI exceptions
|
66 |
raise gr.Error(f"An OpenAI error occurred: {e}")
|
|
|
44 |
openai.api_key = api_key
|
45 |
|
46 |
try:
|
47 |
+
# Save the audio content to a temporary file
|
48 |
+
file_extension = f".{response_format}"
|
49 |
+
with tempfile.NamedTemporaryFile(
|
50 |
+
suffix=file_extension, delete=False, mode="wb"
|
51 |
+
) as temp_file:
|
52 |
+
temp_file_path = temp_file.name
|
53 |
+
|
54 |
+
# Use a context manager to handle the streaming response
|
55 |
+
with openai.audio.speech.create(
|
56 |
+
model=model,
|
57 |
+
voice=voice,
|
58 |
+
input=input_text,
|
59 |
+
response_format=response_format,
|
60 |
+
speed=speed,
|
61 |
+
stream=True,
|
62 |
+
) as response:
|
63 |
+
# Stream the response directly to the temp file
|
64 |
for chunk in response:
|
65 |
temp_file.write(chunk)
|
|
|
66 |
except openai.OpenAIError as e:
|
67 |
# Catch OpenAI exceptions
|
68 |
raise gr.Error(f"An OpenAI error occurred: {e}")
|