File size: 1,553 Bytes
a73ec05
 
6b438f3
 
 
 
a73ec05
6b438f3
 
 
 
a73ec05
6b438f3
 
 
 
 
a73ec05
6b438f3
 
a73ec05
6b438f3
 
 
 
a73ec05
6b438f3
 
 
a73ec05
6b438f3
 
a73ec05
6b438f3
a73ec05
6b438f3
 
 
 
a73ec05
6b438f3
 
 
 
 
 
 
 
 
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
# tts.py
import os
from pathlib import Path
import openai
import logging
from gtts import gTTS  # Ensure gTTS is installed (pip install gTTS)

# Set OpenAI API key from the environment variable
openai.api_key = os.getenv("api_key_oai")

def text_to_speech(text: str, voice: str = "coral", model: str = "tts-1") -> str:
    """
    Convert input text to speech using OpenAI's TTS API.
    Falls back to gTTS if the OpenAI API fails.
    
    Returns:
        The file path to the generated audio file.
    """
    # Generate a unique filename using a hash of the text
    output_file = Path(__file__).parent / f"speech_{abs(hash(text))}.pus"
    try:
        response = openai.Audio.speech.create(
            model=model,
            voice=voice,
            input=text,
        )
        response.stream_to_file(str(output_file))
        logging.info("OpenAI TTS succeeded.")
        return str(output_file)
    except Exception as e:
        logging.error("OpenAI TTS failed, falling back to gTTS. Error: %s", e)
        return text_to_speech_gtts(text)

def text_to_speech_gtts(text: str) -> str:
    """
    Convert input text to speech using gTTS.
    
    Returns:
        The file path to the generated audio file.
    """
    output_file = Path(__file__).parent / f"speech_{abs(hash(text))}.mp3"
    try:
        tts = gTTS(text=text, lang='en')
        tts.save(str(output_file))
        logging.info("gTTS succeeded.")
        return str(output_file)
    except Exception as e:
        logging.error("gTTS failed. Error: %s", e)
        raise