Spaces:
Runtime error
Runtime error
Update openai_tts_tool.py
Browse files- openai_tts_tool.py +21 -7
openai_tts_tool.py
CHANGED
|
@@ -1,6 +1,17 @@
|
|
| 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, language, output_option):
|
| 5 |
"""
|
| 6 |
Generate audio and text files from input text using OpenAI's TTS API.
|
|
@@ -8,7 +19,7 @@ def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_s
|
|
| 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 |
language (str): Language code for synthesis
|
|
@@ -26,6 +37,9 @@ def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_s
|
|
| 26 |
try:
|
| 27 |
client = OpenAI(api_key=api_key)
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
# Create temp directory if it doesn't exist
|
| 30 |
temp_dir = os.path.join(os.getcwd(), 'temp')
|
| 31 |
if not os.path.exists(temp_dir):
|
|
@@ -35,26 +49,26 @@ def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_s
|
|
| 35 |
audio_file = None
|
| 36 |
if output_option in ["audio", "both"]:
|
| 37 |
speech_response = client.audio.speech.create(
|
| 38 |
-
model="tts-1",
|
| 39 |
voice=voice_type,
|
| 40 |
-
input=
|
| 41 |
speed=float(voice_speed)
|
| 42 |
)
|
| 43 |
|
| 44 |
# Save the audio to a temporary file
|
| 45 |
-
audio_path = os.path.join(temp_dir, f"output_{hash(
|
| 46 |
with open(audio_path, "wb") as f:
|
| 47 |
for chunk in speech_response.iter_bytes():
|
| 48 |
f.write(chunk)
|
| 49 |
|
| 50 |
audio_file = audio_path
|
| 51 |
|
| 52 |
-
# Save the
|
| 53 |
script_file = None
|
| 54 |
if output_option in ["script_text", "both"]:
|
| 55 |
-
script_path = os.path.join(temp_dir, f"script_{hash(
|
| 56 |
with open(script_path, "w", encoding='utf-8') as f:
|
| 57 |
-
f.write(
|
| 58 |
script_file = script_path
|
| 59 |
|
| 60 |
status_message = f"Generation completed successfully in {language}!"
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
def translate_text(client, text, target_language, model_name):
|
| 5 |
+
"""
|
| 6 |
+
Translate the input text to the target language using specified OpenAI GPT model.
|
| 7 |
+
"""
|
| 8 |
+
prompt = f"Translate the following text to {target_language}:\n\n{text}\n\nTranslation:"
|
| 9 |
+
response = client.chat.completions.create(
|
| 10 |
+
model=model_name,
|
| 11 |
+
messages=[{"role": "user", "content": prompt}]
|
| 12 |
+
)
|
| 13 |
+
return response.choices[0].message.content.strip()
|
| 14 |
+
|
| 15 |
def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_speed, language, output_option):
|
| 16 |
"""
|
| 17 |
Generate audio and text files from input text using OpenAI's TTS API.
|
|
|
|
| 19 |
Args:
|
| 20 |
api_key (str): OpenAI API key
|
| 21 |
input_text (str): Text to convert to speech
|
| 22 |
+
model_name (str): OpenAI model name for translation
|
| 23 |
voice_type (str): Voice type for TTS
|
| 24 |
voice_speed (float): Speed of speech
|
| 25 |
language (str): Language code for synthesis
|
|
|
|
| 37 |
try:
|
| 38 |
client = OpenAI(api_key=api_key)
|
| 39 |
|
| 40 |
+
# Translate the text if the target language is not the same as the input text language
|
| 41 |
+
translated_text = translate_text(client, input_text, language, model_name)
|
| 42 |
+
|
| 43 |
# Create temp directory if it doesn't exist
|
| 44 |
temp_dir = os.path.join(os.getcwd(), 'temp')
|
| 45 |
if not os.path.exists(temp_dir):
|
|
|
|
| 49 |
audio_file = None
|
| 50 |
if output_option in ["audio", "both"]:
|
| 51 |
speech_response = client.audio.speech.create(
|
| 52 |
+
model="tts-1-hd",
|
| 53 |
voice=voice_type,
|
| 54 |
+
input=translated_text,
|
| 55 |
speed=float(voice_speed)
|
| 56 |
)
|
| 57 |
|
| 58 |
# Save the audio to a temporary file
|
| 59 |
+
audio_path = os.path.join(temp_dir, f"output_{hash(translated_text)}_{language}.mp3")
|
| 60 |
with open(audio_path, "wb") as f:
|
| 61 |
for chunk in speech_response.iter_bytes():
|
| 62 |
f.write(chunk)
|
| 63 |
|
| 64 |
audio_file = audio_path
|
| 65 |
|
| 66 |
+
# Save the translated text as a script file
|
| 67 |
script_file = None
|
| 68 |
if output_option in ["script_text", "both"]:
|
| 69 |
+
script_path = os.path.join(temp_dir, f"script_{hash(translated_text)}_{language}.txt")
|
| 70 |
with open(script_path, "w", encoding='utf-8') as f:
|
| 71 |
+
f.write(translated_text)
|
| 72 |
script_file = script_path
|
| 73 |
|
| 74 |
status_message = f"Generation completed successfully in {language}!"
|