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:
72
ml/core/api_client.py
Normal file
72
ml/core/api_client.py
Normal file
@@ -0,0 +1,72 @@
|
||||
"""Client HTTP verso l'api-service (service-to-service via x-api-key).
|
||||
|
||||
Espone accesso a:
|
||||
/jobs ciclo di vita job
|
||||
/queue stato coda
|
||||
/pageconnections registro sessioni di pagina (enforcement /test max 2)
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from core.config import settings
|
||||
|
||||
|
||||
def _headers() -> dict:
|
||||
return {"x-api-key": settings.internal_api_key, "Content-Type": "application/json"}
|
||||
|
||||
|
||||
async def _req(method: str, path: str, json: Optional[dict] = None, params: Optional[dict] = None) -> Any:
|
||||
url = f"{settings.api_url}{path}"
|
||||
async with httpx.AsyncClient(timeout=10.0) as c:
|
||||
r = await c.request(method, url, json=json, params=params, headers=_headers())
|
||||
r.raise_for_status()
|
||||
if r.status_code == 204 or not r.content:
|
||||
return None
|
||||
return r.json()
|
||||
|
||||
|
||||
# ── jobs ────────────────────────────────────────────────────────────────────
|
||||
async def create_job(type_: str, created_by: str, payload: dict) -> dict:
|
||||
return await _req("POST", "/jobs", json={"type": type_, "created_by": created_by, "payload": payload})
|
||||
|
||||
|
||||
async def update_job(job_id: str, **fields) -> dict:
|
||||
return await _req("PATCH", f"/jobs/{job_id}", json=fields)
|
||||
|
||||
|
||||
async def get_job(job_id: str) -> dict:
|
||||
return await _req("GET", f"/jobs/{job_id}")
|
||||
|
||||
|
||||
async def list_jobs(type_: Optional[str] = None, status: Optional[str] = None, limit: int = 50) -> list:
|
||||
params = {"limit": str(limit)}
|
||||
if type_:
|
||||
params["type"] = type_
|
||||
if status:
|
||||
params["status"] = status
|
||||
return await _req("GET", "/jobs", params=params) or []
|
||||
|
||||
|
||||
# ── queue ───────────────────────────────────────────────────────────────────
|
||||
async def queue_status(type_: str = "train") -> dict:
|
||||
return await _req("GET", "/queue", params={"type": type_})
|
||||
|
||||
|
||||
# ── page connections ───────────────────────────────────────────────────────
|
||||
async def page_connect(page: str, user_id: str, session_id: str) -> dict:
|
||||
return await _req("POST", "/pageconnections", json={"page": page, "user_id": user_id, "session_id": session_id})
|
||||
|
||||
|
||||
async def page_ping(session_id: str) -> dict:
|
||||
return await _req("POST", f"/pageconnections/{session_id}/ping")
|
||||
|
||||
|
||||
async def page_disconnect(session_id: str) -> None:
|
||||
await _req("DELETE", f"/pageconnections/{session_id}")
|
||||
|
||||
|
||||
async def page_count(page: str) -> dict:
|
||||
return await _req("GET", f"/pageconnections/{page}")
|
||||
Reference in New Issue
Block a user