arpy8's picture
update server download and play logic
051a0cd
Raw History Blame Contribute Delete
8.12 kB
import os
import logging
import httpx
from contextlib import asynccontextmanager
from fastapi.responses import StreamingResponse
from fastapi import FastAPI, UploadFile, File, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from dotenv import load_dotenv
from slowapi import Limiter
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Rate limiting
limiter = Limiter(key_func=get_remote_address)
# API Keys
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
SYSTEM_PROMPT = """Act as a playful, caring voice friend for young kids.
Mirror the user's language (default English) and match tone/complexity to the question.
Reply under 50 words, always gentle, kindergarten-safe.
Refuse inappropriate topics politely.
Never mention being AI or a robot - you're just a friendly voice.
Refuse adult topics which kids may ask politely.
Be encouraging, curious, and fun!
IMPORTANT: Always respond in English only, regardless of input language."""
if not ELEVENLABS_API_KEY or not OPENAI_API_KEY:
raise RuntimeError("Missing ELEVENLABS_API_KEY or OPENAI_API_KEY")
# Connection pool for HTTP clients
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup
app.state.http_client = httpx.AsyncClient(
timeout=httpx.Timeout(30.0, connect=10.0),
limits=httpx.Limits(max_connections=100, max_keepalive_connections=20)
)
logger.info("Application startup: HTTP client pool initialized")
yield
# Shutdown
await app.state.http_client.aclose()
logger.info("Application shutdown: HTTP client pool closed")
app = FastAPI(title="Kids Voice Chat API", version="1.0.0", lifespan=lifespan, docs_url=None, redoc_url=None)
# Add middleware
app.add_middleware(GZipMiddleware, minimum_size=1000)
app.add_middleware(
CORSMiddleware,
allow_origins=os.getenv("CORS_ORIGINS", "*").split(","),
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Rate limit exception handler
@app.exception_handler(RateLimitExceeded)
async def rate_limit_handler(request: Request, exc: RateLimitExceeded):
return HTTPException(status_code=429, detail="Rate limit exceeded")
async def stt_elevenlabs(audio_bytes: bytes, client: httpx.AsyncClient) -> str:
"""Speech-to-text using ElevenLabs API"""
try:
url = "https://api.elevenlabs.io/v1/speech-to-text"
headers = {"xi-api-key": ELEVENLABS_API_KEY}
files = {
"model_id": (None, "scribe_v1"),
"file": ("audio.wav", audio_bytes, "audio/wav"),
}
resp = await client.post(url, headers=headers, files=files)
if resp.status_code != 200:
logger.error(f"STT API error: {resp.status_code} - {resp.text}")
raise HTTPException(status_code=500, detail="Speech-to-text failed")
data = resp.json()
text = data.get("text", "").strip()
if not text:
logger.warning("STT returned empty text")
raise HTTPException(status_code=400, detail="No speech detected")
logger.info(f"STT success: {text[:50]}...")
return text
except httpx.TimeoutException:
logger.error("STT timeout")
raise HTTPException(status_code=504, detail="Speech-to-text timeout")
except HTTPException:
raise
except Exception as e:
logger.error(f"STT error: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
async def chat_openai(prompt: str, client: httpx.AsyncClient) -> str:
"""LLM response using OpenAI API"""
try:
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Authorization": f"Bearer {OPENAI_API_KEY}",
"Content-Type": "application/json",
}
payload = {
"model": "gpt-4o-mini",
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": prompt},
],
"max_tokens": 120,
"temperature": 0.6,
}
resp = await client.post(url, headers=headers, json=payload)
if resp.status_code != 200:
logger.error(f"LLM API error: {resp.status_code} - {resp.text}")
raise HTTPException(status_code=500, detail="LLM processing failed")
data = resp.json()
reply = data["choices"][0]["message"]["content"].strip()
logger.info(f"LLM success: {reply[:50]}...")
return reply
except httpx.TimeoutException:
logger.error("LLM timeout")
raise HTTPException(status_code=504, detail="LLM timeout")
except HTTPException:
raise
except Exception as e:
logger.error(f"LLM error: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
async def tts_openai(text: str, client: httpx.AsyncClient) -> bytes:
"""Text-to-speech using OpenAI API - returns complete audio"""
try:
url = "https://api.openai.com/v1/audio/speech"
headers = {
"Authorization": f"Bearer {OPENAI_API_KEY}",
"Content-Type": "application/json",
}
payload = {
"model": "tts-1",
"input": text,
"voice": "nova",
"response_format": "pcm",
"language": "en",
}
resp = await client.post(url, headers=headers, json=payload)
if resp.status_code != 200:
logger.error(f"TTS API error: {resp.status_code} - {resp.text}")
raise HTTPException(status_code=500, detail="Text-to-speech failed")
audio_data = resp.content
logger.info(f"TTS complete: {len(audio_data)} bytes")
return audio_data
except httpx.TimeoutException:
logger.error("TTS timeout")
raise HTTPException(status_code=504, detail="TTS timeout")
except HTTPException:
raise
except Exception as e:
logger.error(f"TTS error: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
@app.get("/health")
async def health_check():
"""Health check endpoint for load balancers"""
return {"status": "healthy", "version": "1.0.0"}
@app.post("/chat")
@limiter.limit("30/minute")
async def chat(request: Request, audio: UploadFile = File(...)):
"""Process audio input through STT → LLM → TTS pipeline"""
try:
if audio.size > 5 * 1024 * 1024: # 5MB limit
raise HTTPException(status_code=413, detail="Audio file too large (max 5MB)")
logger.info(f"Chat request received: {audio.filename}")
audio_bytes = await audio.read()
client = app.state.http_client
transcript = await stt_elevenlabs(audio_bytes, client)
reply = await chat_openai(transcript, client)
audio_response = await tts_openai(reply, client)
logger.info("Chat pipeline completed successfully")
from fastapi.responses import Response
return Response(
content=audio_response,
media_type="audio/raw",
headers={
"X-Sample-Rate": "24000",
"X-Format": "PCM16",
"Content-Length": str(len(audio_response))
},
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Chat endpoint error: {str(e)}")
raise HTTPException(status_code=500, detail="Internal server error")
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app",
host="0.0.0.0",
port=int(os.getenv("PORT", 7860)),
workers=int(os.getenv("WORKERS", 4)),
log_level="info",
access_log=True,
reload=True
)