Spaces:
Sleeping
Sleeping
Commit
路
cb11654
1
Parent(s):
fc33134
Add libsql dependency
Browse files
server.py
CHANGED
|
@@ -1897,13 +1897,49 @@ def forecast_train(alpha: float = 3.0, lags: int = 6):
|
|
| 1897 |
"metrics": metrics,
|
| 1898 |
}
|
| 1899 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1900 |
|
| 1901 |
@app.get("/forecast", response_model=ForecastOut)
|
| 1902 |
-
def forecast_get(horizon: int = 12):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1903 |
with db() as conn:
|
| 1904 |
payload = load_model(conn, "forecast_competencia_v1")
|
|
|
|
|
|
|
| 1905 |
if not payload:
|
| 1906 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1907 |
|
| 1908 |
preds = forecast_next_months(payload, horizon=int(horizon))
|
| 1909 |
|
|
|
|
| 1897 |
"metrics": metrics,
|
| 1898 |
}
|
| 1899 |
|
| 1900 |
+
@app.get("/forecast/status")
|
| 1901 |
+
def forecast_status(min_months: int = 12, lags: int = 6):
|
| 1902 |
+
"""
|
| 1903 |
+
Retorna se existe modelo treinado e se h谩 hist贸rico suficiente para treinar.
|
| 1904 |
+
脷til para o frontend decidir habilitar bot玫es e/ou auto-treinar.
|
| 1905 |
+
"""
|
| 1906 |
+
with db() as conn:
|
| 1907 |
+
payload = load_model(conn, "forecast_competencia_v1")
|
| 1908 |
+
series = build_monthly_series_competencia(conn)
|
| 1909 |
+
n_months = len(series)
|
| 1910 |
+
|
| 1911 |
+
# Regra do seu treino: len(series) >= (lags + 6)
|
| 1912 |
+
required = int(lags) + 6
|
| 1913 |
+
can_train = n_months >= required and n_months >= int(min_months)
|
| 1914 |
+
|
| 1915 |
+
return {
|
| 1916 |
+
"trained": bool(payload),
|
| 1917 |
+
"trained_at": (payload or {}).get("trained_at"),
|
| 1918 |
+
"n_months": n_months,
|
| 1919 |
+
"required_months": required,
|
| 1920 |
+
"can_train": can_train,
|
| 1921 |
+
"note": "O treino exige hist贸rico suficiente (>= lags + 6). Recomenda-se >= 12 meses."
|
| 1922 |
+
}
|
| 1923 |
+
|
| 1924 |
|
| 1925 |
@app.get("/forecast", response_model=ForecastOut)
|
| 1926 |
+
def forecast_get(horizon: int = 12, auto_train: bool = True, alpha: float = 3.0, lags: int = 6):
|
| 1927 |
+
"""
|
| 1928 |
+
Se n茫o houver modelo treinado:
|
| 1929 |
+
- auto_train=True => tenta treinar e salvar automaticamente (se houver hist贸rico suficiente)
|
| 1930 |
+
- auto_train=False => retorna 404 "Modelo n茫o treinado..."
|
| 1931 |
+
"""
|
| 1932 |
with db() as conn:
|
| 1933 |
payload = load_model(conn, "forecast_competencia_v1")
|
| 1934 |
+
|
| 1935 |
+
# Se n茫o existe modelo e auto_train ligado, tenta treinar
|
| 1936 |
if not payload:
|
| 1937 |
+
if not auto_train:
|
| 1938 |
+
raise HTTPException(status_code=404, detail="Modelo n茫o treinado. Rode POST /forecast/train.")
|
| 1939 |
+
|
| 1940 |
+
# Tenta treinar agora (pode falhar por hist贸rico insuficiente)
|
| 1941 |
+
payload = train_models_competencia(conn, alpha=float(alpha), lags=int(lags))
|
| 1942 |
+
save_model(conn, "forecast_competencia_v1", payload)
|
| 1943 |
|
| 1944 |
preds = forecast_next_months(payload, horizon=int(horizon))
|
| 1945 |
|