capradeepgujaran commited on
Commit
25a5bf9
·
verified ·
1 Parent(s): 59cbb27

Update openai_tts_tool.py

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