Spaces:
Sleeping
Sleeping
| """ | |
| FastAPI Application - Avatar-based AI Tutor Engine | |
| """ | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.responses import JSONResponse | |
| from app.contracts import EngineRequest, EngineResponse | |
| from app.engine import AvatarTutorEngine | |
| from app.config import config | |
| import logging | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| # Initialize FastAPI app | |
| app = FastAPI( | |
| title="Avatar-based AI Tutor Engine", | |
| description="Intelligence engine for creating personalized AI tutor avatars with dynamic facial animations", | |
| version=config.ENGINE_VERSION | |
| ) | |
| # Initialize engine | |
| engine = AvatarTutorEngine() | |
| async def startup_event(): | |
| """Startup event handler""" | |
| logger.info(f"Starting {config.ENGINE_NAME} v{config.ENGINE_VERSION}") | |
| # Validate configuration | |
| error = config.validate() | |
| if error: | |
| logger.error(f"Configuration error: {error}") | |
| raise RuntimeError(error) | |
| logger.info(f"Using models:") | |
| logger.info(f" Text: {config.HF_TEXT_MODEL}") | |
| logger.info(f" ASR: {config.HF_ASR_MODEL}") | |
| logger.info(f" TTS: {config.HF_TTS_MODEL}") | |
| logger.info(f" Avatar: {config.HF_AVATAR_MODEL}") | |
| logger.info("Engine ready") | |
| async def shutdown_event(): | |
| """Shutdown event handler""" | |
| logger.info("Shutting down engine") | |
| async def root(): | |
| """Root endpoint - health check""" | |
| return { | |
| "engine": config.ENGINE_NAME, | |
| "version": config.ENGINE_VERSION, | |
| "status": "running", | |
| "supported_actions": ["create_avatar_tutor"] | |
| } | |
| async def health(): | |
| """Health check endpoint""" | |
| return {"status": "healthy"} | |
| async def run(request: EngineRequest) -> EngineResponse: | |
| """ | |
| Main engine endpoint - creates avatar tutor | |
| Args: | |
| request: Standard EngineRequest | |
| Returns: | |
| Standard EngineResponse | |
| """ | |
| try: | |
| logger.info(f"Processing request {request.request_id} - action: {request.action}") | |
| # Execute engine logic | |
| response = await engine.run(request) | |
| if response.ok: | |
| logger.info(f"Request {request.request_id} completed successfully") | |
| else: | |
| logger.warning(f"Request {request.request_id} failed: {response.error.detail if response.error else 'Unknown error'}") | |
| return response | |
| except Exception as e: | |
| logger.error(f"Unexpected error processing request {request.request_id}: {str(e)}") | |
| # Return structured error response | |
| return EngineResponse( | |
| request_id=request.request_id, | |
| ok=False, | |
| status="error", | |
| engine=config.ENGINE_NAME, | |
| action=request.action, | |
| error={ | |
| "code": "INTERNAL_ERROR", | |
| "detail": f"An unexpected error occurred: {str(e)}" | |
| } | |
| ) | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "app.main:app", | |
| host=config.HOST, | |
| port=config.PORT, | |
| reload=True | |
| ) | |