95 lines
2.6 KiB
Python
95 lines
2.6 KiB
Python
"""
|
|
LLM Monitor - Dashboard per controllare i modelli caricati in Ollama
|
|
Entry point dell'applicazione FastAPI
|
|
"""
|
|
|
|
import logging
|
|
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.responses import FileResponse
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from pathlib import Path
|
|
import os
|
|
|
|
# Configurazione logging
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Importare le rotte
|
|
from app.api.health import router as health_router
|
|
from app.api.models import router as models_router
|
|
from app.config import settings
|
|
|
|
# Creare l'app FastAPI
|
|
app = FastAPI(
|
|
title="LLM Monitor API",
|
|
description="Dashboard per il monitoraggio dei modelli LLM in Ollama",
|
|
version="1.0.0",
|
|
docs_url="/docs",
|
|
redoc_url="/redoc",
|
|
openapi_url="/openapi.json"
|
|
)
|
|
|
|
# Configurare CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.CORS_ORIGINS.split(","),
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Registrare le rotte API
|
|
app.include_router(health_router, prefix="/api/v1", tags=["health"])
|
|
app.include_router(models_router, prefix="/api/v1", tags=["models"])
|
|
|
|
# Servire i file statici
|
|
static_path = Path(__file__).parent / "app" / "web" / "static"
|
|
if static_path.exists():
|
|
app.mount("/static", StaticFiles(directory=static_path), name="static")
|
|
|
|
# Servire la dashboard web
|
|
templates_path = Path(__file__).parent / "app" / "web" / "templates"
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Redirect alla dashboard"""
|
|
return FileResponse(templates_path / "index.html")
|
|
|
|
@app.get("/dashboard")
|
|
async def dashboard():
|
|
"""Dashboard principale"""
|
|
return FileResponse(templates_path / "index.html")
|
|
|
|
|
|
@app.get("/models-running")
|
|
async def models_running_page():
|
|
"""Pagina dedicata ai modelli residenti in memoria (ollama ps)."""
|
|
return FileResponse(templates_path / "models_running.html")
|
|
|
|
|
|
@app.get("/favicon.ico", include_in_schema=False)
|
|
async def favicon():
|
|
"""Favicon dell'applicazione."""
|
|
return FileResponse(static_path / "favicon.ico")
|
|
|
|
# Event hooks
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
logger.info("🚀 LLM Monitor avviato")
|
|
logger.info(f"📊 Ollama host: {settings.OLLAMA_HOST}")
|
|
|
|
@app.on_event("shutdown")
|
|
async def shutdown_event():
|
|
logger.info("🛑 LLM Monitor arrestato")
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"main:app",
|
|
host=settings.API_HOST,
|
|
port=settings.API_PORT,
|
|
reload=settings.ENVIRONMENT == "development",
|
|
log_level=settings.LOG_LEVEL.lower()
|
|
)
|