at-engine / main.py
Godswill-IoT's picture
Upload 6 files
bb8a136 verified
"""
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()
@app.on_event("startup")
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")
@app.on_event("shutdown")
async def shutdown_event():
"""Shutdown event handler"""
logger.info("Shutting down engine")
@app.get("/")
async def root():
"""Root endpoint - health check"""
return {
"engine": config.ENGINE_NAME,
"version": config.ENGINE_VERSION,
"status": "running",
"supported_actions": ["create_avatar_tutor"]
}
@app.get("/health")
async def health():
"""Health check endpoint"""
return {"status": "healthy"}
@app.post("/run", response_model=EngineResponse)
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
)