Spaces:
Sleeping
Sleeping
# tts.py | |
import os | |
import tempfile | |
import requests | |
from playsound import playsound | |
def text_to_speech_openai(text, language="en"): | |
""" | |
Convert text to speech using a hypothetical OpenAI TTS API. | |
Note: OpenAI Whisper is for speech recognition. | |
Replace the endpoint and parameters with actual API details when available. | |
""" | |
import openai | |
api_key = os.getenv("api_key_oai") | |
if not api_key: | |
raise ValueError("API key for OpenAI TTS not found in environment variable 'api_key_oai'") | |
openai.api_key = api_key | |
try: | |
# Hypothetical API call -- adjust the engine name and parameters as per actual API documentation. | |
response = openai.Audio.synthesize( | |
engine="tts", # Hypothetical engine name for TTS | |
text=text, | |
language=language | |
) | |
audio_url = response["audio_url"] | |
except Exception as e: | |
raise RuntimeError(f"OpenAI TTS synthesis failed: {e}") | |
# Download and play the audio | |
audio_data = requests.get(audio_url).content | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: | |
tmp_file.write(audio_data) | |
tmp_file_path = tmp_file.name | |
playsound(tmp_file_path) | |
def text_to_speech_gtts(text, language="en"): | |
""" | |
Fallback text-to-speech using the gTTS library. | |
""" | |
from gtts import gTTS | |
tts = gTTS(text=text, lang=language) | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: | |
tts.save(tmp_file.name) | |
tmp_file_path = tmp_file.name | |
playsound(tmp_file_path) | |