File size: 2,175 Bytes
f9ae432
 
 
78decde
c08083d
f9ae432
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c3644eb
f9ae432
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from openai import OpenAI
import tempfile
import os

def generate_audio_and_text(api_key, input_text, model_name, voice_type, voice_speed, language, output_option, summary_length, additional_prompt):
    if not input_text:
        return None, "No input text provided"
    
    try:
        client = OpenAI(api_key=api_key)
        
        # Generate summary if requested
        summary_text = None
        if output_option in ["summary_text", "both"]:
            summary_prompt = f"Summarize the following text in approximately {summary_length} words. {additional_prompt or ''}\n\nText: {input_text}"
            
            summary_response = client.chat.completions.create(
                model=model_name,
                messages=[{"role": "user", "content": summary_prompt}]
            )
            summary_text = summary_response.choices[0].message.content
        
        # Generate audio if requested
        audio_file = None
        if output_option in ["audio", "both"]:
            speech_response = client.audio.speech.create(
                model="tts-1",  # or "tts-1-hd" for higher quality
                voice=voice_type,
                input=input_text,
                speed=float(voice_speed)
            )
            
            # Create temp directory if it doesn't exist
            temp_dir = os.path.join(os.getcwd(), 'temp')
            if not os.path.exists(temp_dir):
                os.makedirs(temp_dir)
            
            # Save the audio to a temporary file
            audio_path = os.path.join(temp_dir, f"output_{hash(input_text)}.mp3")
            with open(audio_path, "wb") as f:
                for chunk in speech_response.iter_bytes():
                    f.write(chunk)
            
            audio_file = audio_path
        
        # Return based on output option
        if output_option == "summary_text":
            return None, summary_text
        elif output_option == "audio":
            return audio_file, None
        elif output_option == "both":
            return audio_file, summary_text
            
    except Exception as e:
        return None, f"Error: {str(e)}"
    
    return None, None