Spaces:
Running
Running
capradeepgujaran
commited on
Commit
•
f9ae432
1
Parent(s):
9d47d09
Update openai_tts_tool.py
Browse files- openai_tts_tool.py +55 -27
openai_tts_tool.py
CHANGED
@@ -1,29 +1,57 @@
|
|
1 |
-
import
|
|
|
|
|
2 |
|
3 |
def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_speed, language, output_option, summary_length, additional_prompt):
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
import tempfile
|
3 |
+
import os
|
4 |
|
5 |
def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_speed, language, output_option, summary_length, additional_prompt):
|
6 |
+
if not input_text:
|
7 |
+
return None, "No input text provided"
|
8 |
+
|
9 |
+
try:
|
10 |
+
client = OpenAI(api_key=api_key)
|
11 |
+
|
12 |
+
# Generate summary if requested
|
13 |
+
summary_text = None
|
14 |
+
if output_option in ["summary_text", "both"]:
|
15 |
+
summary_prompt = f"Summarize the following text in approximately {summary_length} words. {additional_prompt or ''}\n\nText: {input_text}"
|
16 |
+
|
17 |
+
summary_response = client.chat.completions.create(
|
18 |
+
model=model_name,
|
19 |
+
messages=[{"role": "user", "content": summary_prompt}]
|
20 |
+
)
|
21 |
+
summary_text = summary_response.choices[0].message.content
|
22 |
+
|
23 |
+
# Generate audio if requested
|
24 |
+
audio_file = None
|
25 |
+
if output_option in ["audio", "both"]:
|
26 |
+
speech_response = client.audio.speech.create(
|
27 |
+
model="tts-1", # or "tts-1-hd" for higher quality
|
28 |
+
voice=voice_type,
|
29 |
+
input=input_text,
|
30 |
+
speed=float(voice_speed)
|
31 |
+
)
|
32 |
+
|
33 |
+
# Create temp directory if it doesn't exist
|
34 |
+
temp_dir = os.path.join(os.getcwd(), 'temp')
|
35 |
+
if not os.path.exists(temp_dir):
|
36 |
+
os.makedirs(temp_dir)
|
37 |
+
|
38 |
+
# Save the audio to a temporary file
|
39 |
+
audio_path = os.path.join(temp_dir, f"output_{hash(input_text)}.mp3")
|
40 |
+
with open(audio_path, "wb") as f:
|
41 |
+
for chunk in speech_response.iter_bytes():
|
42 |
+
f.write(chunk)
|
43 |
+
|
44 |
+
audio_file = audio_path
|
45 |
+
|
46 |
+
# Return based on output option
|
47 |
+
if output_option == "summary_text":
|
48 |
+
return None, summary_text
|
49 |
+
elif output_option == "audio":
|
50 |
+
return audio_file, None
|
51 |
+
elif output_option == "both":
|
52 |
+
return audio_file, summary_text
|
53 |
+
|
54 |
+
except Exception as e:
|
55 |
+
return None, f"Error: {str(e)}"
|
56 |
+
|
57 |
+
return None, None
|