Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import gradio as gr
|
|
@@ -7,10 +8,11 @@ from tools.sql_tool import SQLTool
|
|
| 7 |
from tools.predict_tool import PredictTool
|
| 8 |
from tools.explain_tool import ExplainTool
|
| 9 |
from tools.report_tool import ReportTool
|
|
|
|
|
|
|
|
|
|
| 10 |
from utils.tracing import Tracer
|
| 11 |
from utils.config import AppConfig
|
| 12 |
-
from tools.ts_forecast_tool import TimeseriesForecastTool
|
| 13 |
-
from tools.ts_preprocess import build_timeseries
|
| 14 |
|
| 15 |
# Optional tiny CPU LLM for planning (can be disabled by not setting ORCHESTRATOR_MODEL)
|
| 16 |
llm = None
|
|
@@ -31,17 +33,12 @@ sql_tool = SQLTool(cfg, tracer)
|
|
| 31 |
predict_tool = PredictTool(cfg, tracer)
|
| 32 |
explain_tool = ExplainTool(cfg, tracer)
|
| 33 |
report_tool = ReportTool(cfg, tracer)
|
| 34 |
-
|
| 35 |
-
ts_tool = TimeseriesForecastTool(cfg, tracer,
|
| 36 |
-
context_length=512, forecast_length=96,
|
| 37 |
-
target_cols=["portfolio_value"], # set after preprocessing
|
| 38 |
-
control_cols=["rate_deposit", "rate_asset"] # optional exogenous
|
| 39 |
-
)
|
| 40 |
|
| 41 |
SYSTEM_PROMPT = (
|
| 42 |
"You are an analytical assistant for tabular data. "
|
| 43 |
"Decide which tools to call in order: "
|
| 44 |
-
"1) SQL (retrieve) 2) Predict (score) 3) Explain (SHAP) 4) Report (document). "
|
| 45 |
"Always disclose the steps taken."
|
| 46 |
)
|
| 47 |
|
|
@@ -49,7 +46,7 @@ def plan_actions(message: str):
|
|
| 49 |
if llm is not None:
|
| 50 |
prompt = (
|
| 51 |
f"{SYSTEM_PROMPT}\nUser: {message}\n"
|
| 52 |
-
"Return JSON with fields: steps (array subset of ['sql','predict','explain','report']), rationale."
|
| 53 |
)
|
| 54 |
try:
|
| 55 |
out = llm(prompt)[0]["generated_text"]
|
|
@@ -66,6 +63,7 @@ def plan_actions(message: str):
|
|
| 66 |
if any(k in m for k in ["predict", "score", "risk", "propensity", "probability"]): steps.append("predict")
|
| 67 |
if any(k in m for k in ["why", "explain", "shap", "feature", "attribution"]): steps.append("explain")
|
| 68 |
if any(k in m for k in ["report", "download", "pdf", "summary"]): steps.append("report")
|
|
|
|
| 69 |
if not steps: steps = ["sql"]
|
| 70 |
return {"steps": steps, "rationale": "Rule-based plan."}
|
| 71 |
|
|
@@ -78,34 +76,43 @@ def run_agent(message: str, hitl_decision: str = "Approve", reviewer_note: str =
|
|
| 78 |
predict_df = None
|
| 79 |
explain_imgs = {}
|
| 80 |
artifacts = {}
|
|
|
|
| 81 |
|
| 82 |
if "sql" in plan["steps"]:
|
| 83 |
sql_df = sql_tool.run(message)
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
try:
|
| 86 |
ts_df = build_timeseries(sql_df)
|
| 87 |
except Exception:
|
| 88 |
ts_df = None
|
| 89 |
-
|
| 90 |
-
if "sql" in plan["steps"]:
|
| 91 |
-
sql_df = sql_tool.run(message)
|
| 92 |
-
artifacts["sql_rows"] = int(len(sql_df)) if isinstance(sql_df, pd.DataFrame) else 0
|
| 93 |
|
| 94 |
-
if "
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
if "explain" in plan["steps"]:
|
| 98 |
explain_imgs = explain_tool.run(predict_df or sql_df)
|
| 99 |
|
| 100 |
-
if "forecast" in plan["steps"] and ts_df is not None:
|
| 101 |
-
fc = ts_tool.zeroshot_forecast(ts_df[["timestamp","portfolio_value","interest_rate"]].dropna())
|
| 102 |
-
|
| 103 |
report_link = None
|
| 104 |
if "report" in plan["steps"]:
|
|
|
|
|
|
|
| 105 |
report_link = report_tool.render_and_save(
|
| 106 |
user_query=message,
|
| 107 |
sql_preview=sql_df.head(50) if isinstance(sql_df, pd.DataFrame) else None,
|
| 108 |
-
predict_preview=predict_df.head(50) if isinstance(predict_df, pd.DataFrame) else
|
| 109 |
explain_images=explain_imgs,
|
| 110 |
plan=plan,
|
| 111 |
)
|
|
@@ -118,13 +125,18 @@ def run_agent(message: str, hitl_decision: str = "Approve", reviewer_note: str =
|
|
| 118 |
"plan": plan,
|
| 119 |
})
|
| 120 |
|
|
|
|
| 121 |
response = f"**Plan:** {plan['steps']}\n**Rationale:** {plan['rationale']}\n"
|
| 122 |
if isinstance(sql_df, pd.DataFrame): response += f"\n**SQL rows:** {len(sql_df)}"
|
| 123 |
if isinstance(predict_df, pd.DataFrame): response += f"\n**Predictions rows:** {len(predict_df)}"
|
|
|
|
|
|
|
| 124 |
if report_link: response += f"\n**Report:** {report_link}"
|
| 125 |
if tracer.trace_url: response += f"\n**Trace:** {tracer.trace_url}"
|
| 126 |
|
| 127 |
-
|
|
|
|
|
|
|
| 128 |
return response, (preview_df if isinstance(preview_df, pd.DataFrame) else pd.DataFrame())
|
| 129 |
|
| 130 |
with gr.Blocks() as demo:
|
|
|
|
| 1 |
+
# space/app.py
|
| 2 |
import os
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
|
|
|
| 8 |
from tools.predict_tool import PredictTool
|
| 9 |
from tools.explain_tool import ExplainTool
|
| 10 |
from tools.report_tool import ReportTool
|
| 11 |
+
from tools.ts_preprocess import build_timeseries
|
| 12 |
+
from tools.ts_forecast_tool import TimeseriesForecastTool
|
| 13 |
+
|
| 14 |
from utils.tracing import Tracer
|
| 15 |
from utils.config import AppConfig
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Optional tiny CPU LLM for planning (can be disabled by not setting ORCHESTRATOR_MODEL)
|
| 18 |
llm = None
|
|
|
|
| 33 |
predict_tool = PredictTool(cfg, tracer)
|
| 34 |
explain_tool = ExplainTool(cfg, tracer)
|
| 35 |
report_tool = ReportTool(cfg, tracer)
|
| 36 |
+
ts_tool = TimeseriesForecastTool(cfg, tracer) # Granite wrapper
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
SYSTEM_PROMPT = (
|
| 39 |
"You are an analytical assistant for tabular data. "
|
| 40 |
"Decide which tools to call in order: "
|
| 41 |
+
"1) SQL (retrieve) 2) Predict (score) 3) Explain (SHAP) 4) Report (document) 5) Forecast (Granite TTM). "
|
| 42 |
"Always disclose the steps taken."
|
| 43 |
)
|
| 44 |
|
|
|
|
| 46 |
if llm is not None:
|
| 47 |
prompt = (
|
| 48 |
f"{SYSTEM_PROMPT}\nUser: {message}\n"
|
| 49 |
+
"Return JSON with fields: steps (array subset of ['sql','predict','explain','report','forecast']), rationale."
|
| 50 |
)
|
| 51 |
try:
|
| 52 |
out = llm(prompt)[0]["generated_text"]
|
|
|
|
| 63 |
if any(k in m for k in ["predict", "score", "risk", "propensity", "probability"]): steps.append("predict")
|
| 64 |
if any(k in m for k in ["why", "explain", "shap", "feature", "attribution"]): steps.append("explain")
|
| 65 |
if any(k in m for k in ["report", "download", "pdf", "summary"]): steps.append("report")
|
| 66 |
+
if any(k in m for k in ["forecast", "next", "horizon", "granite"]): steps.append("forecast")
|
| 67 |
if not steps: steps = ["sql"]
|
| 68 |
return {"steps": steps, "rationale": "Rule-based plan."}
|
| 69 |
|
|
|
|
| 76 |
predict_df = None
|
| 77 |
explain_imgs = {}
|
| 78 |
artifacts = {}
|
| 79 |
+
ts_forecast_df = None
|
| 80 |
|
| 81 |
if "sql" in plan["steps"]:
|
| 82 |
sql_df = sql_tool.run(message)
|
| 83 |
+
artifacts["sql_rows"] = int(len(sql_df)) if isinstance(sql_df, pd.DataFrame) else 0
|
| 84 |
+
|
| 85 |
+
if "predict" in plan["steps"]:
|
| 86 |
+
predict_df = predict_tool.run(sql_df)
|
| 87 |
+
|
| 88 |
+
ts_df = None
|
| 89 |
+
if sql_df is not None:
|
| 90 |
try:
|
| 91 |
ts_df = build_timeseries(sql_df)
|
| 92 |
except Exception:
|
| 93 |
ts_df = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
if "forecast" in plan["steps"] and ts_df is not None:
|
| 96 |
+
# Expect 'portfolio_value' after preprocessing
|
| 97 |
+
# Use the combined series — e.g., sum over instruments by timestamp
|
| 98 |
+
agg = ts_df.groupby("timestamp", as_index=True)["portfolio_value"].sum().sort_index()
|
| 99 |
+
try:
|
| 100 |
+
ts_forecast_df = ts_tool.zeroshot_forecast(agg, horizon=96)
|
| 101 |
+
except Exception as e:
|
| 102 |
+
# Surface a readable error in response
|
| 103 |
+
ts_forecast_df = pd.DataFrame({"error": [str(e)]})
|
| 104 |
|
| 105 |
if "explain" in plan["steps"]:
|
| 106 |
explain_imgs = explain_tool.run(predict_df or sql_df)
|
| 107 |
|
|
|
|
|
|
|
|
|
|
| 108 |
report_link = None
|
| 109 |
if "report" in plan["steps"]:
|
| 110 |
+
# Add forecast preview if available
|
| 111 |
+
forecast_preview = ts_forecast_df.head(50) if isinstance(ts_forecast_df, pd.DataFrame) else None
|
| 112 |
report_link = report_tool.render_and_save(
|
| 113 |
user_query=message,
|
| 114 |
sql_preview=sql_df.head(50) if isinstance(sql_df, pd.DataFrame) else None,
|
| 115 |
+
predict_preview=predict_df.head(50) if isinstance(predict_df, pd.DataFrame) else forecast_preview,
|
| 116 |
explain_images=explain_imgs,
|
| 117 |
plan=plan,
|
| 118 |
)
|
|
|
|
| 125 |
"plan": plan,
|
| 126 |
})
|
| 127 |
|
| 128 |
+
# Compose response
|
| 129 |
response = f"**Plan:** {plan['steps']}\n**Rationale:** {plan['rationale']}\n"
|
| 130 |
if isinstance(sql_df, pd.DataFrame): response += f"\n**SQL rows:** {len(sql_df)}"
|
| 131 |
if isinstance(predict_df, pd.DataFrame): response += f"\n**Predictions rows:** {len(predict_df)}"
|
| 132 |
+
if isinstance(ts_forecast_df, pd.DataFrame) and "forecast" in ts_forecast_df.columns:
|
| 133 |
+
response += f"\n**Forecast horizon:** {len(ts_forecast_df)}"
|
| 134 |
if report_link: response += f"\n**Report:** {report_link}"
|
| 135 |
if tracer.trace_url: response += f"\n**Trace:** {tracer.trace_url}"
|
| 136 |
|
| 137 |
+
# Prefer to show forecast if present, else predictions, else raw query
|
| 138 |
+
preview_df = ts_forecast_df if isinstance(ts_forecast_df, pd.DataFrame) and not ts_forecast_df.empty else \
|
| 139 |
+
(predict_df if isinstance(predict_df, pd.DataFrame) and not predict_df.empty else sql_df)
|
| 140 |
return response, (preview_df if isinstance(preview_df, pd.DataFrame) else pd.DataFrame())
|
| 141 |
|
| 142 |
with gr.Blocks() as demo:
|