capradeepgujaran commited on
Commit
f9ae432
1 Parent(s): 9d47d09

Update openai_tts_tool.py

Browse files
Files changed (1) hide show
  1. openai_tts_tool.py +55 -27
openai_tts_tool.py CHANGED
@@ -1,29 +1,57 @@
1
- import openai
 
 
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
- # Set API key dynamically
5
- openai.api_key = api_key
6
- client = openai.OpenAI(api_key=api_key)
7
-
8
- # Assuming text-to-speech and summarization logic goes here
9
- if output_option in ["summary_text", "both"]:
10
- text_summary = f"Generated summary for: {input_text[:100]}..." # Replace with real summary generation logic
11
-
12
- if output_option in ["audio", "both"]:
13
- response = client.audio.speech.create(
14
- text=input_text,
15
- model=model_name,
16
- voice=voice_type,
17
- language=language,
18
- speed=voice_speed
19
- )
20
- audio_output = response['audio_file'] # Placeholder for the actual audio file output
21
-
22
- if output_option == "summary_text":
23
- return text_summary, None
24
- elif output_option == "audio":
25
- return None, audio_output
26
- elif output_option == "both":
27
- return text_summary, audio_output
28
-
29
- return None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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