Решение по TD-STYLE-ANALYZERS: LF — инструменты проекта (Python/Node) пишут LF, CRLF-.sh не работают на Linux CI (sh scripts/ci.sh), большинство файлов уже были LF. Добавлен .gitattributes (* text=auto eol=lf, бинарные исключения), .editorconfig переведён на lf, 1029 файлов конвертированы, git add --renormalize. Из индекса убраны закравшиеся archive/**/__pycache__/*.pyc.
71 lines
1.5 KiB
Python
71 lines
1.5 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()
|