ai-interview-caller / src /audio_store.py
ranjithkumar111's picture
Upload 8 files
b253d7e verified
Raw
History Blame Contribute Delete
1.11 kB
# File: src/audio_store.py
# Purpose: Serve synthesised MP3 audio files over HTTP so Twilio can play them.
# Files are stored in /tmp and served via a FastAPI static route.
# For production, swap to S3 presigned URLs.
import uuid
from pathlib import Path
AUDIO_DIR = Path("/tmp/ai_recruiter_audio")
AUDIO_DIR.mkdir(parents=True, exist_ok=True)
def save_tts_audio(audio_bytes: bytes) -> str:
"""
Save MP3 bytes to disk. Returns a unique filename (not full URL).
The FastAPI app will mount AUDIO_DIR as a static route under /audio/.
"""
filename = f"{uuid.uuid4().hex}.mp3"
(AUDIO_DIR / filename).write_bytes(audio_bytes)
return filename
def get_audio_path(filename: str) -> Path:
return AUDIO_DIR / filename
def public_audio_url(base_url: str, filename: str) -> str:
"""Build the public URL Twilio will fetch to play the audio."""
return f"{base_url}/audio/{filename}"
def cleanup_audio(filename: str):
"""Delete audio file after use."""
path = AUDIO_DIR / filename
path.unlink(missing_ok=True)