Spaces:
Sleeping
Sleeping
Optimized output for modelfit_chat
Browse files
main.py
CHANGED
|
@@ -366,21 +366,57 @@ async def _forward_and_get_body(path: str, method: str = "GET", json_body=None,
|
|
| 366 |
return (r.status_code, {"text": r.text[:1000]})
|
| 367 |
|
| 368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
def _build_modelfit_chat_minimal(body: dict) -> dict:
|
| 370 |
"""Extract formula, alpha, beta, and Performance subset (col 2, rows 3,4,5) for chat response."""
|
| 371 |
out = {"ok": True, "formula": body.get("formula")}
|
| 372 |
-
|
| 373 |
-
|
|
|
|
|
|
|
| 374 |
perf = body.get("Performance")
|
| 375 |
if isinstance(perf, list) and len(perf) >= 5:
|
| 376 |
keys = list(perf[0].keys()) if perf[0] else []
|
| 377 |
-
# Col 2 (1-based) = index 1; if only one column use index 0. Rows 3,4,5 = indices 2,3,4.
|
| 378 |
col_idx = 1 if len(keys) > 1 else 0
|
| 379 |
col2_name = keys[col_idx] if keys else None
|
| 380 |
if col2_name is not None:
|
| 381 |
row_names = ["R2 overall", "MSE", "MAE"]
|
| 382 |
-
values = [perf[i].get(col2_name) for i in [2, 3, 4]]
|
| 383 |
-
out["Performance_subset"] = {"metric_names": row_names, "
|
| 384 |
else:
|
| 385 |
out["Performance_subset"] = None
|
| 386 |
else:
|
|
|
|
| 366 |
return (r.status_code, {"text": r.text[:1000]})
|
| 367 |
|
| 368 |
|
| 369 |
+
def _round_val(x, ndigits: int = 4):
|
| 370 |
+
"""Round numbers to ndigits; leave non-numbers as-is."""
|
| 371 |
+
if isinstance(x, (int, float)) and not isinstance(x, bool):
|
| 372 |
+
return round(float(x), ndigits)
|
| 373 |
+
return x
|
| 374 |
+
|
| 375 |
+
|
| 376 |
+
# Row labels for alpha/beta: Estimate, std.er., t_stat, p_val (backend order = _ROW4)
|
| 377 |
+
_FIT_ROW_LABELS = ("Estimate", "std.er.", "t_stat", "p_val")
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
def _format_fit_table(rows: list, ndigits: int = 4) -> list:
|
| 381 |
+
"""
|
| 382 |
+
Transform backend table (list of 4 dicts, keys=columns) into chat-friendly format.
|
| 383 |
+
Row 0: column names -> rounded values (Estimate). Rows 1,2,3: row label -> value(s).
|
| 384 |
+
For single column: [{'col': v0}, {'std.er.': v1}, {'t_stat': v2}, {'p_val': v3}].
|
| 385 |
+
For multi-column: row 0 = {col: v}; rows 1,2,3 = {'std.er.': [v,...]}, etc.
|
| 386 |
+
"""
|
| 387 |
+
if not isinstance(rows, list) or len(rows) != 4:
|
| 388 |
+
return rows
|
| 389 |
+
col_names = list(rows[0].keys()) if rows[0] else []
|
| 390 |
+
out = []
|
| 391 |
+
# Row 0: Estimate (column names as keys)
|
| 392 |
+
out.append({k: _round_val(rows[0].get(k)) for k in col_names})
|
| 393 |
+
# Rows 1,2,3: row name as key
|
| 394 |
+
for r in range(1, 4):
|
| 395 |
+
label = _FIT_ROW_LABELS[r]
|
| 396 |
+
vals = [rows[r].get(k) for k in col_names]
|
| 397 |
+
if len(col_names) == 1:
|
| 398 |
+
out.append({label: _round_val(vals[0])})
|
| 399 |
+
else:
|
| 400 |
+
out.append({label: [_round_val(v) for v in vals]})
|
| 401 |
+
return out
|
| 402 |
+
|
| 403 |
+
|
| 404 |
def _build_modelfit_chat_minimal(body: dict) -> dict:
|
| 405 |
"""Extract formula, alpha, beta, and Performance subset (col 2, rows 3,4,5) for chat response."""
|
| 406 |
out = {"ok": True, "formula": body.get("formula")}
|
| 407 |
+
alpha = body.get("alpha")
|
| 408 |
+
beta = body.get("beta")
|
| 409 |
+
out["alpha"] = _format_fit_table(alpha) if isinstance(alpha, list) else alpha
|
| 410 |
+
out["beta"] = _format_fit_table(beta) if isinstance(beta, list) else beta
|
| 411 |
perf = body.get("Performance")
|
| 412 |
if isinstance(perf, list) and len(perf) >= 5:
|
| 413 |
keys = list(perf[0].keys()) if perf[0] else []
|
|
|
|
| 414 |
col_idx = 1 if len(keys) > 1 else 0
|
| 415 |
col2_name = keys[col_idx] if keys else None
|
| 416 |
if col2_name is not None:
|
| 417 |
row_names = ["R2 overall", "MSE", "MAE"]
|
| 418 |
+
values = [_round_val(perf[i].get(col2_name)) for i in [2, 3, 4]]
|
| 419 |
+
out["Performance_subset"] = {"metric_names": row_names, "values": values}
|
| 420 |
else:
|
| 421 |
out["Performance_subset"] = None
|
| 422 |
else:
|