Spaces:
Running
Running
capradeepgujaran
commited on
Commit
•
9c78509
1
Parent(s):
c2a0c97
Update openai_tts_tool.py
Browse files- openai_tts_tool.py +37 -32
openai_tts_tool.py
CHANGED
@@ -1,41 +1,45 @@
|
|
1 |
from openai import OpenAI
|
2 |
import os
|
3 |
|
4 |
-
def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_speed,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
if not input_text:
|
6 |
-
return None, "No input text provided"
|
|
|
|
|
|
|
7 |
|
8 |
try:
|
9 |
client = OpenAI(api_key=api_key)
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
if
|
14 |
-
|
15 |
-
|
16 |
-
summary_response = client.chat.completions.create(
|
17 |
-
model=model_name,
|
18 |
-
messages=[{"role": "user", "content": summary_prompt}]
|
19 |
-
)
|
20 |
-
summary_text = summary_response.choices[0].message.content
|
21 |
|
22 |
-
# Generate audio
|
23 |
audio_file = None
|
24 |
if output_option in ["audio", "both"]:
|
25 |
-
# Ensure language is passed correctly for TTS generation
|
26 |
speech_response = client.audio.speech.create(
|
27 |
-
model="tts-1",
|
28 |
voice=voice_type,
|
29 |
input=input_text,
|
30 |
-
speed=float(voice_speed)
|
31 |
-
language=language # Pass the selected language here
|
32 |
)
|
33 |
|
34 |
-
# Create temp directory if it doesn't exist
|
35 |
-
temp_dir = os.path.join(os.getcwd(), 'temp')
|
36 |
-
if not os.path.exists(temp_dir):
|
37 |
-
os.makedirs(temp_dir)
|
38 |
-
|
39 |
# Save the audio to a temporary file
|
40 |
audio_path = os.path.join(temp_dir, f"output_{hash(input_text)}.mp3")
|
41 |
with open(audio_path, "wb") as f:
|
@@ -44,15 +48,16 @@ def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_s
|
|
44 |
|
45 |
audio_file = audio_path
|
46 |
|
47 |
-
#
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
54 |
|
55 |
except Exception as e:
|
56 |
-
return None, f"Error: {str(e)}"
|
57 |
-
|
58 |
-
return None, None
|
|
|
1 |
from openai import OpenAI
|
2 |
import os
|
3 |
|
4 |
+
def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_speed, output_option):
|
5 |
+
"""
|
6 |
+
Generate audio and text files from input text using OpenAI's TTS API.
|
7 |
+
|
8 |
+
Args:
|
9 |
+
api_key (str): OpenAI API key
|
10 |
+
input_text (str): Text to convert to speech
|
11 |
+
model_name (str): OpenAI model name
|
12 |
+
voice_type (str): Voice type for TTS
|
13 |
+
voice_speed (float): Speed of speech
|
14 |
+
output_option (str): Output type ('audio', 'script_text', or 'both')
|
15 |
+
|
16 |
+
Returns:
|
17 |
+
tuple: (audio_file_path, script_file_path, status_message)
|
18 |
+
"""
|
19 |
if not input_text:
|
20 |
+
return None, None, "No input text provided"
|
21 |
+
|
22 |
+
if not api_key:
|
23 |
+
return None, None, "No API key provided"
|
24 |
|
25 |
try:
|
26 |
client = OpenAI(api_key=api_key)
|
27 |
|
28 |
+
# Create temp directory if it doesn't exist
|
29 |
+
temp_dir = os.path.join(os.getcwd(), 'temp')
|
30 |
+
if not os.path.exists(temp_dir):
|
31 |
+
os.makedirs(temp_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
# Generate audio file
|
34 |
audio_file = None
|
35 |
if output_option in ["audio", "both"]:
|
|
|
36 |
speech_response = client.audio.speech.create(
|
37 |
+
model="tts-1",
|
38 |
voice=voice_type,
|
39 |
input=input_text,
|
40 |
+
speed=float(voice_speed)
|
|
|
41 |
)
|
42 |
|
|
|
|
|
|
|
|
|
|
|
43 |
# Save the audio to a temporary file
|
44 |
audio_path = os.path.join(temp_dir, f"output_{hash(input_text)}.mp3")
|
45 |
with open(audio_path, "wb") as f:
|
|
|
48 |
|
49 |
audio_file = audio_path
|
50 |
|
51 |
+
# Save the input text as a script file
|
52 |
+
script_file = None
|
53 |
+
if output_option in ["script_text", "both"]:
|
54 |
+
script_path = os.path.join(temp_dir, f"script_{hash(input_text)}.txt")
|
55 |
+
with open(script_path, "w", encoding='utf-8') as f:
|
56 |
+
f.write(input_text)
|
57 |
+
script_file = script_path
|
58 |
+
|
59 |
+
status_message = "Generation completed successfully!"
|
60 |
+
return audio_file, script_file, status_message
|
61 |
|
62 |
except Exception as e:
|
63 |
+
return None, None, f"Error: {str(e)}"
|
|
|
|