Uses circuitforge_core.tasks.scheduler. VRAM detection via cf-orch when available, falling back to unlimited. Adds expiry_llm_fallback task type to background-predict expiry dates for items the LUT doesn't cover.
55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
#!/usr/bin/env python
|
|
# app/main.py
|
|
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.api.routes import api_router
|
|
from app.core.config import settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
logger.info("Starting Kiwi API...")
|
|
settings.ensure_dirs()
|
|
|
|
# Start LLM background task scheduler
|
|
from app.tasks.scheduler import get_scheduler
|
|
get_scheduler(settings.DB_PATH)
|
|
logger.info("Task scheduler started.")
|
|
|
|
yield
|
|
|
|
# Graceful scheduler shutdown
|
|
from app.tasks.scheduler import get_scheduler, reset_scheduler
|
|
get_scheduler(settings.DB_PATH).shutdown(timeout=10.0)
|
|
reset_scheduler()
|
|
logger.info("Kiwi API shutting down.")
|
|
|
|
|
|
app = FastAPI(
|
|
title=settings.PROJECT_NAME,
|
|
description="Pantry tracking + leftover recipe suggestions",
|
|
version="0.1.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(api_router, prefix=settings.API_PREFIX)
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"service": "kiwi-api", "docs": "/docs"}
|