Spaces:
Sleeping

File size: 8,118 Bytes
1cada75
 
 
 
 
051a0cd
1cada75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
051a0cd
1cada75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
051a0cd
1cada75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
051a0cd
 
4d4a172
 
 
 
 
 
 
 
 
 
 
 
 
 
051a0cd
4d4a172
 
 
051a0cd
 
 
 
4d4a172
 
 
 
 
 
 
 
 
1cada75
 
 
 
 
 
 
 
051a0cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cada75
 
 
 
 
 
 
 
 
 
4d4a172
1cada75
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
    )