Spaces:
Sleeping
Sleeping
Added fit/forecast file individual
Browse files
main.py
CHANGED
|
@@ -161,6 +161,27 @@ async def _forward_stream(path: str, files=None, data=None, user_token: str | No
|
|
| 161 |
return response
|
| 162 |
|
| 163 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
# for demo
|
| 165 |
async def _forward_demo_stream(path: str, *, files: dict | None, data: dict | None, method: str = "POST"):
|
| 166 |
if not DEMO_FORWARD_URL or not HF_TOKEN or not DATFID_DEMO_TOKEN:
|
|
@@ -373,4 +394,58 @@ async def modelforecast_ind(req: Request):
|
|
| 373 |
if not user_token:
|
| 374 |
raise HTTPException(status_code=401, detail="Missing Authorization Bearer token (dt+...)")
|
| 375 |
body = await req.json()
|
| 376 |
-
return await _forward("/modelforecast_ind/", "POST", json_body=body, user_token=user_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
return response
|
| 162 |
|
| 163 |
|
| 164 |
+
async def _forward_multipart_json(path: str, files=None, data=None, user_token: str | None = None, method: str = "POST"):
|
| 165 |
+
"""POST multipart (files + data) to upstream and return JSON response."""
|
| 166 |
+
url = f"{UPSTREAM_URL}{path}"
|
| 167 |
+
headers = {
|
| 168 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 169 |
+
"Accept": "application/json",
|
| 170 |
+
}
|
| 171 |
+
if user_token:
|
| 172 |
+
headers["X-API-Key"] = user_token
|
| 173 |
+
timeout = httpx.Timeout(600.0)
|
| 174 |
+
async with httpx.AsyncClient(timeout=timeout) as client:
|
| 175 |
+
r = await client.request(method, url, headers=headers, files=files, data=data)
|
| 176 |
+
ct = r.headers.get("content-type", "")
|
| 177 |
+
if "application/json" in ct:
|
| 178 |
+
try:
|
| 179 |
+
return JSONResponse(status_code=r.status_code, content=r.json())
|
| 180 |
+
except Exception:
|
| 181 |
+
return JSONResponse(status_code=r.status_code, content={"error": r.text[:500]})
|
| 182 |
+
return JSONResponse(status_code=r.status_code, content={"text": r.text[:1000]})
|
| 183 |
+
|
| 184 |
+
|
| 185 |
# for demo
|
| 186 |
async def _forward_demo_stream(path: str, *, files: dict | None, data: dict | None, method: str = "POST"):
|
| 187 |
if not DEMO_FORWARD_URL or not HF_TOKEN or not DATFID_DEMO_TOKEN:
|
|
|
|
| 394 |
if not user_token:
|
| 395 |
raise HTTPException(status_code=401, detail="Missing Authorization Bearer token (dt+...)")
|
| 396 |
body = await req.json()
|
| 397 |
+
return await _forward("/modelforecast_ind/", "POST", json_body=body, user_token=user_token)
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
@app.post("/modelfit-file_ind/")
|
| 401 |
+
async def modelfit_file_ind(
|
| 402 |
+
req: Request,
|
| 403 |
+
file: UploadFile = File(...),
|
| 404 |
+
id_col: str = Form(...),
|
| 405 |
+
time_col: str = Form(...),
|
| 406 |
+
y: str = Form(...),
|
| 407 |
+
lag_y: str = Form(""),
|
| 408 |
+
lagged_features: str = Form(""),
|
| 409 |
+
current_features: str = Form(""),
|
| 410 |
+
filter_by_significance: str = Form("false"),
|
| 411 |
+
meanvar_test: str = Form("false"),
|
| 412 |
+
signif: str = Form("0.05"),
|
| 413 |
+
):
|
| 414 |
+
user_token = _extract_user_token(req)
|
| 415 |
+
if not user_token:
|
| 416 |
+
raise HTTPException(status_code=401, detail="Missing Authorization Bearer token (dt+...)")
|
| 417 |
+
raw = await file.read()
|
| 418 |
+
if len(raw) > SDK_MAX_BODY_BYTES_extended:
|
| 419 |
+
raise HTTPException(status_code=413, detail="Payload too large.")
|
| 420 |
+
files = {
|
| 421 |
+
"file": (file.filename, raw, file.content_type or "application/octet-stream"),
|
| 422 |
+
}
|
| 423 |
+
data = {
|
| 424 |
+
"id_col": id_col,
|
| 425 |
+
"time_col": time_col,
|
| 426 |
+
"y": y,
|
| 427 |
+
"lag_y": lag_y,
|
| 428 |
+
"lagged_features": lagged_features,
|
| 429 |
+
"current_features": current_features,
|
| 430 |
+
"filter_by_significance": filter_by_significance,
|
| 431 |
+
"meanvar_test": meanvar_test,
|
| 432 |
+
"signif": signif,
|
| 433 |
+
}
|
| 434 |
+
return await _forward_multipart_json("/modelfit-file_ind/", files=files, data=data, user_token=user_token, method="POST")
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
@app.post("/modelforecast-file_ind/")
|
| 438 |
+
async def modelforecast_file_ind(
|
| 439 |
+
req: Request,
|
| 440 |
+
df_forecast: UploadFile = File(...),
|
| 441 |
+
):
|
| 442 |
+
user_token = _extract_user_token(req)
|
| 443 |
+
if not user_token:
|
| 444 |
+
raise HTTPException(status_code=401, detail="Missing Authorization Bearer token (dt+...)")
|
| 445 |
+
raw = await df_forecast.read()
|
| 446 |
+
if len(raw) > SDK_MAX_BODY_BYTES_extended:
|
| 447 |
+
raise HTTPException(status_code=413, detail="Payload too large.")
|
| 448 |
+
files = {
|
| 449 |
+
"df_forecast": (df_forecast.filename, raw, df_forecast.content_type or "application/octet-stream"),
|
| 450 |
+
}
|
| 451 |
+
return await _forward_stream("/modelforecast-file_ind/", files=files, data=None, user_token=user_token, method="POST")
|