Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from contextlib import asynccontextmanager | |
| from loguru import logger | |
| import sys | |
| from config import settings | |
| from routes import word, health, study | |
| from services.word_service import WordEmbeddingService | |
| from services.visualization_service import VisualizationService | |
| from services.study_service import StudyService | |
| # Configure logger | |
| logger.remove() | |
| logger.add( | |
| sys.stdout, | |
| format="{time} | {level} | {message}", | |
| level="INFO", | |
| serialize=False | |
| ) | |
| # Initialize services first | |
| word_service = WordEmbeddingService(settings.model_url) | |
| visualization_service = VisualizationService(word_service) | |
| study_service = StudyService(word_service) | |
| async def lifespan(app: FastAPI): | |
| # Startup | |
| logger.info("Starting up...") | |
| try: | |
| yield | |
| except Exception as e: | |
| logger.error(f"Error during startup: {str(e)}") | |
| raise | |
| finally: | |
| # Cleanup | |
| logger.info("Shutting down...") | |
| # Initialize FastAPI app | |
| app = FastAPI( | |
| title=settings.app_name, | |
| version=settings.version, | |
| lifespan=lifespan | |
| ) | |
| # Configure CORS | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Include routers with direct service injection | |
| app.include_router( | |
| word.init_router(word_service), | |
| tags=["word operations"] | |
| ) | |
| app.include_router( | |
| study.init_router(study_service), | |
| tags=["study operations"] | |
| ) | |
| app.include_router( | |
| health.init_router(word_service), | |
| tags=["health"] | |
| ) | |
| async def root(): | |
| """Root endpoint with API information""" | |
| return { | |
| "app_name": settings.app_name, | |
| "version": settings.version, | |
| "status": "running" | |
| } | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "main:app", | |
| host="0.0.0.0", | |
| port=8000, | |
| reload=settings.debug | |
| ) |