Spaces:
Sleeping
Sleeping
capradeepgujaran
commited on
Commit
•
f3d0867
1
Parent(s):
e9c72ea
Update openai_tts_tool.py
Browse files- openai_tts_tool.py +104 -74
openai_tts_tool.py
CHANGED
@@ -1,28 +1,34 @@
|
|
1 |
# openai_tts_tool.py
|
2 |
|
3 |
-
from openai import OpenAI
|
4 |
import os
|
5 |
from langdetect import detect, DetectorFactory
|
6 |
import logging
|
7 |
|
|
|
|
|
|
|
8 |
# Set up logging configuration
|
9 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s | %(levelname)s | %(message)s')
|
10 |
|
11 |
-
#
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
# Simple in-memory cache for translations
|
15 |
translation_cache = {}
|
16 |
|
17 |
-
def translate_text(api_key, text, target_language):
|
18 |
"""
|
19 |
-
Translate text to the target language using OpenAI's API with gpt-4o-mini model.
|
20 |
|
21 |
Args:
|
22 |
api_key (str): OpenAI API key
|
23 |
text (str): Text to translate
|
24 |
target_language (str): Target language code (e.g., 'en' for English)
|
25 |
-
|
|
|
26 |
Returns:
|
27 |
str: Translated text or error message
|
28 |
"""
|
@@ -33,15 +39,24 @@ def translate_text(api_key, text, target_language):
|
|
33 |
|
34 |
try:
|
35 |
logging.info("Starting translation process.")
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
prompt = f"Translate the following text to {target_language}:\n\n{text}"
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
43 |
)
|
44 |
-
translated_text =
|
45 |
logging.info("Translation successful.")
|
46 |
|
47 |
# Cache the translation
|
@@ -52,105 +67,120 @@ def translate_text(api_key, text, target_language):
|
|
52 |
logging.error(f"Error in translation: {str(e)}")
|
53 |
return f"Error in translation: {str(e)}"
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_speed, language, output_option):
|
56 |
"""
|
57 |
-
Generate audio and text
|
58 |
|
59 |
Args:
|
60 |
api_key (str): OpenAI API key
|
61 |
-
input_text (str): Text to convert to speech
|
62 |
-
model_name (str): OpenAI model name
|
63 |
voice_type (str): Voice type for TTS
|
64 |
voice_speed (float): Speed of speech
|
65 |
-
language (str):
|
66 |
output_option (str): Output type ('audio', 'script_text', or 'both')
|
67 |
|
68 |
Returns:
|
69 |
-
tuple: (audio_file_path, script_file_path, status_message)
|
70 |
"""
|
71 |
if not input_text:
|
72 |
logging.warning("No input text provided.")
|
73 |
-
return None, None, "No input text provided"
|
74 |
|
75 |
if not api_key:
|
76 |
logging.warning("No API key provided.")
|
77 |
-
return None, None, "No API key provided"
|
78 |
|
79 |
try:
|
80 |
-
logging.info("
|
81 |
-
client = OpenAI(api_key=api_key)
|
82 |
-
|
83 |
-
# Create temp directory if it doesn't exist
|
84 |
-
temp_dir = os.path.join(os.getcwd(), 'temp')
|
85 |
-
if not os.path.exists(temp_dir):
|
86 |
-
os.makedirs(temp_dir)
|
87 |
-
logging.info(f"Created temporary directory at {temp_dir}.")
|
88 |
|
89 |
-
#
|
90 |
-
|
91 |
-
|
92 |
-
logging.info(f"Detected input language: {detected_language}")
|
93 |
-
except Exception as e:
|
94 |
-
logging.error(f"Error detecting language: {str(e)}")
|
95 |
-
return None, None, f"Error detecting language: {str(e)}"
|
96 |
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
# If detected language is different from target, translate
|
101 |
-
if detected_language != target_language:
|
102 |
-
logging.info("Input language differs from target language. Proceeding to translate.")
|
103 |
-
translated_text = translate_text(api_key, input_text, target_language)
|
104 |
if translated_text.startswith("Error in translation:"):
|
105 |
return None, None, translated_text
|
106 |
else:
|
107 |
-
logging.info("
|
108 |
translated_text = input_text
|
109 |
|
110 |
-
# Generate audio file
|
111 |
audio_file = None
|
|
|
|
|
|
|
|
|
112 |
if output_option in ["audio", "both"]:
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
# Save the audio to a temporary file
|
123 |
-
audio_filename = f"output_{hash(translated_text)}_{target_language}.mp3"
|
124 |
-
audio_path = os.path.join(temp_dir, audio_filename)
|
125 |
-
with open(audio_path, "wb") as f:
|
126 |
-
for chunk in speech_response.iter_bytes():
|
127 |
-
f.write(chunk)
|
128 |
-
logging.info(f"Audio file saved at {audio_path}.")
|
129 |
-
audio_file = audio_path
|
130 |
-
except Exception as e:
|
131 |
-
logging.error(f"Error during audio generation: {str(e)}")
|
132 |
-
return None, None, f"Error during audio generation: {str(e)}"
|
133 |
|
134 |
-
#
|
135 |
-
script_file = None
|
136 |
if output_option in ["script_text", "both"]:
|
137 |
try:
|
138 |
-
|
139 |
-
|
140 |
-
script_filename = f"script_{hash(script_text)}_{target_language}.txt"
|
141 |
script_path = os.path.join(temp_dir, script_filename)
|
142 |
with open(script_path, "w", encoding='utf-8') as f:
|
143 |
-
f.write(
|
144 |
-
logging.info(f"Script file saved at {script_path}.")
|
145 |
script_file = script_path
|
|
|
146 |
except Exception as e:
|
147 |
logging.error(f"Error during script text generation: {str(e)}")
|
148 |
-
return
|
149 |
|
150 |
-
status_message =
|
151 |
logging.info(status_message)
|
152 |
return audio_file, script_file, status_message
|
153 |
-
|
154 |
except Exception as e:
|
155 |
logging.error(f"Unexpected error: {str(e)}")
|
156 |
return None, None, f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# openai_tts_tool.py
|
2 |
|
|
|
3 |
import os
|
4 |
from langdetect import detect, DetectorFactory
|
5 |
import logging
|
6 |
|
7 |
+
# Ensure consistent results from langdetect
|
8 |
+
DetectorFactory.seed = 0
|
9 |
+
|
10 |
# Set up logging configuration
|
11 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s | %(levelname)s | %(message)s')
|
12 |
|
13 |
+
# Initialize your custom OpenAI client here
|
14 |
+
# Replace the following line with your actual client initialization
|
15 |
+
# For example:
|
16 |
+
# from your_custom_client_module import client
|
17 |
+
client = None # Placeholder: Initialize your client appropriately
|
18 |
|
19 |
# Simple in-memory cache for translations
|
20 |
translation_cache = {}
|
21 |
|
22 |
+
def translate_text(api_key, text, target_language, length=1000):
|
23 |
"""
|
24 |
+
Translate text to the target language using OpenAI's Chat Completion API with gpt-4o-mini model.
|
25 |
|
26 |
Args:
|
27 |
api_key (str): OpenAI API key
|
28 |
text (str): Text to translate
|
29 |
target_language (str): Target language code (e.g., 'en' for English)
|
30 |
+
length (int): Maximum number of tokens for the response
|
31 |
+
|
32 |
Returns:
|
33 |
str: Translated text or error message
|
34 |
"""
|
|
|
39 |
|
40 |
try:
|
41 |
logging.info("Starting translation process.")
|
42 |
+
|
43 |
+
# Ensure the client is initialized
|
44 |
+
if client is None:
|
45 |
+
logging.error("OpenAI client is not initialized.")
|
46 |
+
return "Error: OpenAI client is not initialized."
|
47 |
+
|
48 |
prompt = f"Translate the following text to {target_language}:\n\n{text}"
|
49 |
+
|
50 |
+
# Using your provided chat completion code snippet
|
51 |
+
completion = client.chat.completions.create(
|
52 |
+
model="gpt-4o-mini",
|
53 |
+
messages=[
|
54 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
55 |
+
{"role": "user", "content": prompt}
|
56 |
+
],
|
57 |
+
max_tokens=length
|
58 |
)
|
59 |
+
translated_text = completion.choices[0].message.content.strip()
|
60 |
logging.info("Translation successful.")
|
61 |
|
62 |
# Cache the translation
|
|
|
67 |
logging.error(f"Error in translation: {str(e)}")
|
68 |
return f"Error in translation: {str(e)}"
|
69 |
|
70 |
+
def text_to_speech_openai(text, audio_path, voice, speed):
|
71 |
+
"""
|
72 |
+
Convert text to speech using OpenAI's TTS API and save the audio to a file.
|
73 |
+
|
74 |
+
Args:
|
75 |
+
text (str): Text to convert to speech
|
76 |
+
audio_path (str): Path to save the generated audio file
|
77 |
+
voice (str): Voice type for TTS
|
78 |
+
speed (float): Speed of speech
|
79 |
+
|
80 |
+
Returns:
|
81 |
+
str: Status message indicating success or error
|
82 |
+
"""
|
83 |
+
try:
|
84 |
+
logging.info("Starting text-to-speech generation.")
|
85 |
+
|
86 |
+
# Ensure the client is initialized
|
87 |
+
if client is None:
|
88 |
+
logging.error("OpenAI client is not initialized.")
|
89 |
+
return "Error: OpenAI client is not initialized."
|
90 |
+
|
91 |
+
response = client.audio.speech.create(
|
92 |
+
model="tts-1-hd",
|
93 |
+
voice=voice,
|
94 |
+
input=text,
|
95 |
+
speed=speed
|
96 |
+
)
|
97 |
+
response.stream_to_file(audio_path)
|
98 |
+
logging.info(f"Audio file saved at {audio_path}.")
|
99 |
+
|
100 |
+
return f"Audio generated and saved to {audio_path}."
|
101 |
+
except Exception as e:
|
102 |
+
logging.error(f"Error during audio generation: {str(e)}")
|
103 |
+
return f"Error during audio generation: {str(e)}"
|
104 |
+
|
105 |
def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_speed, language, output_option):
|
106 |
"""
|
107 |
+
Generate audio and/or script text from input text using translation and TTS.
|
108 |
|
109 |
Args:
|
110 |
api_key (str): OpenAI API key
|
111 |
+
input_text (str): Text to translate and convert to speech
|
112 |
+
model_name (str): OpenAI model name (unused in current implementation)
|
113 |
voice_type (str): Voice type for TTS
|
114 |
voice_speed (float): Speed of speech
|
115 |
+
language (str): Target language code for translation and synthesis
|
116 |
output_option (str): Output type ('audio', 'script_text', or 'both')
|
117 |
|
118 |
Returns:
|
119 |
+
tuple: (audio_file_path or None, script_file_path or None, status_message)
|
120 |
"""
|
121 |
if not input_text:
|
122 |
logging.warning("No input text provided.")
|
123 |
+
return None, None, "No input text provided."
|
124 |
|
125 |
if not api_key:
|
126 |
logging.warning("No API key provided.")
|
127 |
+
return None, None, "No API key provided."
|
128 |
|
129 |
try:
|
130 |
+
logging.info("Processing generation request.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
+
# Translate text if necessary
|
133 |
+
detected_language = detect(input_text)
|
134 |
+
logging.info(f"Detected language: {detected_language}")
|
|
|
|
|
|
|
|
|
135 |
|
136 |
+
if detected_language != language:
|
137 |
+
logging.info("Translation required.")
|
138 |
+
translated_text = translate_text(api_key, input_text, language)
|
|
|
|
|
|
|
|
|
139 |
if translated_text.startswith("Error in translation:"):
|
140 |
return None, None, translated_text
|
141 |
else:
|
142 |
+
logging.info("No translation required.")
|
143 |
translated_text = input_text
|
144 |
|
|
|
145 |
audio_file = None
|
146 |
+
script_file = None
|
147 |
+
status_messages = []
|
148 |
+
|
149 |
+
# Generate audio
|
150 |
if output_option in ["audio", "both"]:
|
151 |
+
temp_dir = create_temp_dir()
|
152 |
+
audio_filename = f"output_{hash(translated_text)}_{language}.mp3"
|
153 |
+
audio_path = os.path.join(temp_dir, audio_filename)
|
154 |
+
audio_status = text_to_speech_openai(translated_text, audio_path, voice_type, voice_speed)
|
155 |
+
if "Error" in audio_status:
|
156 |
+
return None, None, audio_status
|
157 |
+
audio_file = audio_path
|
158 |
+
status_messages.append("Audio generated successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
+
# Generate script text
|
|
|
161 |
if output_option in ["script_text", "both"]:
|
162 |
try:
|
163 |
+
temp_dir = create_temp_dir()
|
164 |
+
script_filename = f"script_{hash(translated_text)}_{language}.txt"
|
|
|
165 |
script_path = os.path.join(temp_dir, script_filename)
|
166 |
with open(script_path, "w", encoding='utf-8') as f:
|
167 |
+
f.write(translated_text)
|
|
|
168 |
script_file = script_path
|
169 |
+
status_messages.append("Script text generated successfully.")
|
170 |
except Exception as e:
|
171 |
logging.error(f"Error during script text generation: {str(e)}")
|
172 |
+
return audio_file, None, f"Error during script text generation: {str(e)}"
|
173 |
|
174 |
+
status_message = " ".join(status_messages)
|
175 |
logging.info(status_message)
|
176 |
return audio_file, script_file, status_message
|
|
|
177 |
except Exception as e:
|
178 |
logging.error(f"Unexpected error: {str(e)}")
|
179 |
return None, None, f"Error: {str(e)}"
|
180 |
+
|
181 |
+
def create_temp_dir():
|
182 |
+
"""Create temporary directory if it doesn't exist"""
|
183 |
+
temp_dir = os.path.join(os.getcwd(), 'temp')
|
184 |
+
if not os.path.exists(temp_dir):
|
185 |
+
os.makedirs(temp_dir)
|
186 |
+
return temp_dir
|