Spaces:
Sleeping
Sleeping
Moved service initialization outside of the lifespan context manager
Browse files
main.py
CHANGED
|
@@ -19,29 +19,23 @@ logger.add(
|
|
| 19 |
serialize=False
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@asynccontextmanager
|
| 23 |
async def lifespan(app: FastAPI):
|
| 24 |
# Startup
|
| 25 |
-
logger.info("
|
| 26 |
try:
|
| 27 |
-
# Initialize core services
|
| 28 |
-
word_service = WordEmbeddingService(settings.model_url)
|
| 29 |
-
visualization_service = VisualizationService(word_service)
|
| 30 |
-
study_service = StudyService(word_service)
|
| 31 |
-
|
| 32 |
-
# Add services to app state
|
| 33 |
-
app.state.word_service = word_service
|
| 34 |
-
app.state.visualization_service = visualization_service
|
| 35 |
-
app.state.study_service = study_service
|
| 36 |
-
|
| 37 |
-
logger.info("Services initialized successfully")
|
| 38 |
yield
|
| 39 |
except Exception as e:
|
| 40 |
logger.error(f"Error during startup: {str(e)}")
|
| 41 |
raise
|
| 42 |
finally:
|
| 43 |
# Cleanup
|
| 44 |
-
logger.info("Shutting down
|
| 45 |
|
| 46 |
# Initialize FastAPI app
|
| 47 |
app = FastAPI(
|
|
@@ -59,17 +53,17 @@ app.add_middleware(
|
|
| 59 |
allow_headers=["*"],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
# Include routers
|
| 63 |
app.include_router(
|
| 64 |
-
word.init_router(
|
| 65 |
tags=["word operations"]
|
| 66 |
)
|
| 67 |
app.include_router(
|
| 68 |
-
study.init_router(
|
| 69 |
tags=["study operations"]
|
| 70 |
)
|
| 71 |
app.include_router(
|
| 72 |
-
health.init_router(
|
| 73 |
tags=["health"]
|
| 74 |
)
|
| 75 |
|
|
|
|
| 19 |
serialize=False
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Initialize services first
|
| 23 |
+
word_service = WordEmbeddingService(settings.model_url)
|
| 24 |
+
visualization_service = VisualizationService(word_service)
|
| 25 |
+
study_service = StudyService(word_service)
|
| 26 |
+
|
| 27 |
@asynccontextmanager
|
| 28 |
async def lifespan(app: FastAPI):
|
| 29 |
# Startup
|
| 30 |
+
logger.info("Starting up...")
|
| 31 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
yield
|
| 33 |
except Exception as e:
|
| 34 |
logger.error(f"Error during startup: {str(e)}")
|
| 35 |
raise
|
| 36 |
finally:
|
| 37 |
# Cleanup
|
| 38 |
+
logger.info("Shutting down...")
|
| 39 |
|
| 40 |
# Initialize FastAPI app
|
| 41 |
app = FastAPI(
|
|
|
|
| 53 |
allow_headers=["*"],
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Include routers with direct service injection
|
| 57 |
app.include_router(
|
| 58 |
+
word.init_router(word_service),
|
| 59 |
tags=["word operations"]
|
| 60 |
)
|
| 61 |
app.include_router(
|
| 62 |
+
study.init_router(study_service),
|
| 63 |
tags=["study operations"]
|
| 64 |
)
|
| 65 |
app.include_router(
|
| 66 |
+
health.init_router(word_service),
|
| 67 |
tags=["health"]
|
| 68 |
)
|
| 69 |
|