feat: Add new API endpoints and HTML pages for ML model management
- Implemented HTML pages for datasets, models, training, testing, and results. - Created API endpoints for managing repositories, results, tests, and training sessions. - Added functionality for streaming training progress via Server-Sent Events (SSE). - Introduced a Dockerfile for the ML runner with necessary dependencies. - Developed an SDK for user code execution within the runner container. - Enhanced CSS styles for improved UI layout and navigation. - Established a layout template for consistent HTML structure across pages. - Added JavaScript for dynamic interactions on the models page. - Implemented WebSocket handling for real-time communication with kiosk devices and controllers. - Implemented model registration and management API at /api/models - Added Gitea proxy API for repository interactions at /api/repos - Created results API for listing and comparing training results at /api/results - Developed training management API for enqueueing and retrieving training jobs at /api/trainings - Introduced SSE endpoint for live training progress updates - Added HTML pages for models, datasets, and training management - Created a Dockerfile for the ML runner with necessary dependencies - Developed SDK for user code execution within the runner container - Enhanced CSS styles for improved UI/UX - Implemented WebSocket communication for real-time device and controller interactions in the kiosk system
This commit is contained in:
95
ml/main.py
95
ml/main.py
@@ -1,19 +1,90 @@
|
||||
from fastapi import FastAPI, Request, Response, Header
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
import time
|
||||
"""ml-service — FastAPI entrypoint.
|
||||
|
||||
Monta:
|
||||
/ → RedirectResponse
|
||||
/datasets /models /train /test /results → pagine Jinja
|
||||
/api/datasets /api/models /api/repos /api/trainings /api/tests /api/results → JSON
|
||||
/api/trainings/{id}/events → SSE
|
||||
/health → check
|
||||
/static/* → file statici
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
from core import db, minio_client, redis_client, worker
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
STATIC_DIR = Path(__file__).resolve().parent / "static"
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
log.info("ml-service starting")
|
||||
await db.init_pool()
|
||||
try:
|
||||
minio_client.ensure_bucket()
|
||||
except Exception as e:
|
||||
log.warning("minio bucket ensure failed: %s", e)
|
||||
worker.start_workers()
|
||||
yield
|
||||
log.info("ml-service stopping")
|
||||
await worker.stop_workers()
|
||||
await db.close_pool()
|
||||
await redis_client.close()
|
||||
|
||||
|
||||
app = FastAPI(title="MEB ML Service", lifespan=lifespan)
|
||||
|
||||
# static
|
||||
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/health")
|
||||
def health():
|
||||
async def health():
|
||||
pg_ok = True
|
||||
try:
|
||||
await db.fetchrow("SELECT 1")
|
||||
except Exception:
|
||||
pg_ok = False
|
||||
redis_ok = True
|
||||
try:
|
||||
await redis_client.client().ping()
|
||||
except Exception:
|
||||
redis_ok = False
|
||||
return {
|
||||
"status": "ok",
|
||||
"status": "ok" if (pg_ok and redis_ok) else "degraded",
|
||||
"service": "ml",
|
||||
"version": "1.0.0",
|
||||
"build_number": "1",
|
||||
"version_state": "dev"
|
||||
"postgres": "connected" if pg_ok else "disconnected",
|
||||
"redis": "connected" if redis_ok else "disconnected",
|
||||
"minio": "connected" if minio_client.check() else "disconnected",
|
||||
"version": "2.0.0",
|
||||
}
|
||||
|
||||
@app.get("/")
|
||||
def root():
|
||||
return {"message": "ML Service"}
|
||||
|
||||
from routers import ( # noqa: E402
|
||||
datasets,
|
||||
models,
|
||||
pages,
|
||||
repos,
|
||||
results,
|
||||
tests,
|
||||
trainings,
|
||||
trainings_stream,
|
||||
)
|
||||
|
||||
app.include_router(pages.router)
|
||||
app.include_router(datasets.router)
|
||||
app.include_router(models.router)
|
||||
app.include_router(repos.router)
|
||||
app.include_router(trainings.router)
|
||||
app.include_router(trainings_stream.router)
|
||||
app.include_router(tests.router)
|
||||
app.include_router(results.router)
|
||||
|
||||
Reference in New Issue
Block a user