File size: 644 Bytes
a38b4f9
 
38822a8
 
a38b4f9
 
38822a8
 
a38b4f9
38822a8
 
a38b4f9
 
38822a8
a38b4f9
38822a8
a38b4f9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# app/services/audio_service.py
from gtts import gTTS
import os, uuid, logging

logger = logging.getLogger(__name__)

def generate_audio_file(text: str, language: str = "en") -> str:
    """Generate an MP3 file in Hugging Face writable /tmp directory."""
    try:
        os.makedirs("/tmp", exist_ok=True)
        file_path = os.path.join("/tmp", f"audio_{uuid.uuid4().hex}.mp3")
        tts = gTTS(text=text, lang=language, slow=False)
        tts.save(file_path)
        logger.info(f"Generated Audio: {file_path}")
        return file_path
    except Exception as e:
        logger.error(f"Audio Generation Failed: {str(e)}")
        raise