File size: 847 Bytes
efe586f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import openai
from openai import OpenAI
from config import OPENAI_API_KEY
# import shutil

def generate_speech(path, outfile, voice, text, speed):

    # Initialize OpenAI TTS API
    openai.api_key = OPENAI_API_KEY
    
    # Generate speech using OpenAI's TTS
    try:
        client = OpenAI()
        response = client.audio.speech.create(
            model='tts-1-hd',
            voice=voice,
            input=text,
            speed=speed
            # Add other parameters as needed
        )
        print(f"the response content: {response}")

    except Exception as e:
        print(f"Error during speech generation: {e}")
        return

    # Stream the audio to a file
    response.stream_to_file('voices\output.mp3')
    # shutil.copy('voices\output.mp3', os.path.abspath(path))

    print("Speech generation completed.")