Files
Deal/archive/leadradar-legacy/mlservice/server.py
T
Rustam Khalimov 9e07568ddd Инициализировать репозиторий «Дейл»
Первый коммит: модульный монолит ядра (.NET 10) и gRPC-сервисы
ai/ml/telegram, фронтенд Vue 3/Vite/Tailwind, документация (ТЗ,
инструкция пользователя, техдокументация, код-стайл), бэклог,
скрипты развёртывания и архив прототипа LeadRadar.
2026-09-11 02:50:17 +03:00

71 lines
1.6 KiB
Python

"""HTTP-API автономного ML-сервиса LeadRadar.
Запуск: uvicorn server:app --host 0.0.0.0 --port 8100
(в compose сервис `ml`, наружу порт не публикуется).
"""
from __future__ import annotations
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import model as m
app = FastAPI(title="LeadRadar ML", version="1.0.0")
class LearnItem(BaseModel):
text: str
label: str
delta: float = 1.0
class PredictBody(BaseModel):
text: str
class BatchBody(BaseModel):
items: list[LearnItem]
@app.get("/health")
def health() -> dict:
return {"ok": True, "service": "leadradar-ml"}
@app.get("/status")
def status() -> dict:
return m.status()
@app.post("/learn")
def learn(body: LearnItem) -> dict:
if not body.text.strip() or not body.label.strip():
raise HTTPException(400, "text и label обязательны")
m.learn(body.label, body.text, body.delta)
return {"ok": True}
@app.post("/learn-batch")
def learn_batch(body: BatchBody) -> dict:
m.learn_batch([it.model_dump() for it in body.items])
return {"ok": True, "learned": len(body.items)}
@app.post("/predict")
def predict(body: PredictBody) -> dict:
if not body.text.strip():
raise HTTPException(400, "text обязателен")
return m.predict(body.text)
@app.post("/reset")
def reset() -> dict:
m.reset()
return {"ok": True}
@app.on_event("startup")
def _startup() -> None:
# прогреваем соединение с БД модели
m.status()