Update app.py
Browse files
app.py
CHANGED
|
@@ -6,45 +6,30 @@ import gradio as gr
|
|
| 6 |
from tools.sql_tool import SQLTool
|
| 7 |
from tools.ts_preprocess import build_timeseries
|
| 8 |
|
| 9 |
-
# --- Environment & defaults ---
|
| 10 |
DUCKDB_PATH = os.getenv("DUCKDB_PATH", "alm.duckdb")
|
| 11 |
-
|
| 12 |
-
DEFAULT_TABLE = os.getenv("SQL_DEFAULT_TABLE", "masterdataset_v")
|
| 13 |
|
| 14 |
sql_tool = SQLTool(DUCKDB_PATH)
|
| 15 |
|
| 16 |
INTRO = f"""
|
| 17 |
### ALM LLM — Demo
|
| 18 |
|
| 19 |
-
Connected to **DuckDB** at `{DUCKDB_PATH}`
|
| 20 |
-
|
| 21 |
-
**Try:**
|
| 22 |
-
- *"show me the top 10 fds by portfolio value"*
|
| 23 |
-
- *"top 10 assets by portfolio value"*
|
| 24 |
-
- *"sum portfolio value by currency"*
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
#
|
| 28 |
def run_nl(nl_query: str):
|
| 29 |
if not nl_query or not nl_query.strip():
|
| 30 |
-
return (
|
| 31 |
-
pd.DataFrame(),
|
| 32 |
-
"",
|
| 33 |
-
"Please enter a query.",
|
| 34 |
-
pd.DataFrame(),
|
| 35 |
-
pd.DataFrame(),
|
| 36 |
-
)
|
| 37 |
try:
|
| 38 |
df, sql, why = sql_tool.query_from_nl(nl_query)
|
| 39 |
except Exception as e:
|
| 40 |
return pd.DataFrame(), "", f"Error: {e}", pd.DataFrame(), pd.DataFrame()
|
| 41 |
-
|
| 42 |
-
# Try building projections and liquidity gap
|
| 43 |
try:
|
| 44 |
cf, gap = build_timeseries(df)
|
| 45 |
except Exception:
|
| 46 |
cf, gap = pd.DataFrame(), pd.DataFrame()
|
| 47 |
-
|
| 48 |
return df, sql.strip(), why, cf, gap
|
| 49 |
|
| 50 |
|
|
@@ -55,57 +40,38 @@ def run_sql(sql_text: str):
|
|
| 55 |
df = sql_tool.run_sql(sql_text)
|
| 56 |
except Exception as e:
|
| 57 |
return pd.DataFrame(), f"Error: {e}", pd.DataFrame(), pd.DataFrame()
|
| 58 |
-
|
| 59 |
try:
|
| 60 |
cf, gap = build_timeseries(df)
|
| 61 |
except Exception:
|
| 62 |
cf, gap = pd.DataFrame(), pd.DataFrame()
|
| 63 |
-
|
| 64 |
return df, "OK", cf, gap
|
| 65 |
|
| 66 |
-
|
| 67 |
-
# --- UI ---
|
| 68 |
with gr.Blocks(title="ALM LLM") as demo:
|
| 69 |
gr.Markdown(INTRO)
|
| 70 |
|
| 71 |
with gr.Tab("Ask in Natural Language"):
|
| 72 |
-
nl = gr.Textbox(
|
| 73 |
-
label="Ask a question",
|
| 74 |
-
placeholder="e.g., show me the top 10 fds by portfolio value",
|
| 75 |
-
lines=2,
|
| 76 |
-
)
|
| 77 |
btn = gr.Button("Run")
|
| 78 |
sql_out = gr.Textbox(label="Generated SQL", interactive=False)
|
| 79 |
why_out = gr.Textbox(label="Reasoning", interactive=False)
|
| 80 |
df_out = gr.Dataframe(label="Query Result", interactive=True)
|
| 81 |
cf_out = gr.Dataframe(label="Projected Cash-Flows (if applicable)", interactive=True)
|
| 82 |
gap_out = gr.Dataframe(label="Liquidity Gap (monthly)", interactive=True)
|
| 83 |
-
|
| 84 |
-
# IMPORTANT: keep event binding INSIDE the Blocks context
|
| 85 |
-
btn.click(
|
| 86 |
-
fn=run_nl,
|
| 87 |
-
inputs=[nl],
|
| 88 |
-
outputs=[df_out, sql_out, why_out, cf_out, gap_out],
|
| 89 |
-
)
|
| 90 |
|
| 91 |
with gr.Tab("Run Raw SQL"):
|
| 92 |
sql_in = gr.Code(
|
| 93 |
label="SQL",
|
| 94 |
language="sql",
|
| 95 |
-
value=f"SELECT * FROM {
|
| 96 |
)
|
| 97 |
btn2 = gr.Button("Execute")
|
| 98 |
df2 = gr.Dataframe(label="Result", interactive=True)
|
| 99 |
status = gr.Textbox(label="Status", interactive=False)
|
| 100 |
cf2 = gr.Dataframe(label="Projected Cash-Flows (if applicable)", interactive=True)
|
| 101 |
gap2 = gr.Dataframe(label="Liquidity Gap (monthly)", interactive=True)
|
|
|
|
| 102 |
|
| 103 |
-
btn2.click(
|
| 104 |
-
fn=run_sql,
|
| 105 |
-
inputs=[sql_in],
|
| 106 |
-
outputs=[df2, status, cf2, gap2],
|
| 107 |
-
)
|
| 108 |
-
|
| 109 |
-
# --- Launch ---
|
| 110 |
if __name__ == "__main__":
|
| 111 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 6 |
from tools.sql_tool import SQLTool
|
| 7 |
from tools.ts_preprocess import build_timeseries
|
| 8 |
|
|
|
|
| 9 |
DUCKDB_PATH = os.getenv("DUCKDB_PATH", "alm.duckdb")
|
| 10 |
+
DEFAULT_PATH = "my_db.main.masterdataset_v"
|
|
|
|
| 11 |
|
| 12 |
sql_tool = SQLTool(DUCKDB_PATH)
|
| 13 |
|
| 14 |
INTRO = f"""
|
| 15 |
### ALM LLM — Demo
|
| 16 |
|
| 17 |
+
Connected to **DuckDB** at `{DUCKDB_PATH}`
|
| 18 |
+
Using table **{DEFAULT_PATH}**.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
|
| 21 |
+
# ---------------------- Handlers ----------------------
|
| 22 |
def run_nl(nl_query: str):
|
| 23 |
if not nl_query or not nl_query.strip():
|
| 24 |
+
return pd.DataFrame(), "", "Please enter a query.", pd.DataFrame(), pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
df, sql, why = sql_tool.query_from_nl(nl_query)
|
| 27 |
except Exception as e:
|
| 28 |
return pd.DataFrame(), "", f"Error: {e}", pd.DataFrame(), pd.DataFrame()
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
cf, gap = build_timeseries(df)
|
| 31 |
except Exception:
|
| 32 |
cf, gap = pd.DataFrame(), pd.DataFrame()
|
|
|
|
| 33 |
return df, sql.strip(), why, cf, gap
|
| 34 |
|
| 35 |
|
|
|
|
| 40 |
df = sql_tool.run_sql(sql_text)
|
| 41 |
except Exception as e:
|
| 42 |
return pd.DataFrame(), f"Error: {e}", pd.DataFrame(), pd.DataFrame()
|
|
|
|
| 43 |
try:
|
| 44 |
cf, gap = build_timeseries(df)
|
| 45 |
except Exception:
|
| 46 |
cf, gap = pd.DataFrame(), pd.DataFrame()
|
|
|
|
| 47 |
return df, "OK", cf, gap
|
| 48 |
|
| 49 |
+
# ---------------------- UI ----------------------
|
|
|
|
| 50 |
with gr.Blocks(title="ALM LLM") as demo:
|
| 51 |
gr.Markdown(INTRO)
|
| 52 |
|
| 53 |
with gr.Tab("Ask in Natural Language"):
|
| 54 |
+
nl = gr.Textbox(label="Ask a question", placeholder="e.g., show me the top 10 fds by portfolio value", lines=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
btn = gr.Button("Run")
|
| 56 |
sql_out = gr.Textbox(label="Generated SQL", interactive=False)
|
| 57 |
why_out = gr.Textbox(label="Reasoning", interactive=False)
|
| 58 |
df_out = gr.Dataframe(label="Query Result", interactive=True)
|
| 59 |
cf_out = gr.Dataframe(label="Projected Cash-Flows (if applicable)", interactive=True)
|
| 60 |
gap_out = gr.Dataframe(label="Liquidity Gap (monthly)", interactive=True)
|
| 61 |
+
btn.click(fn=run_nl, inputs=[nl], outputs=[df_out, sql_out, why_out, cf_out, gap_out])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
with gr.Tab("Run Raw SQL"):
|
| 64 |
sql_in = gr.Code(
|
| 65 |
label="SQL",
|
| 66 |
language="sql",
|
| 67 |
+
value=f"SELECT * FROM {DEFAULT_PATH} LIMIT 20;",
|
| 68 |
)
|
| 69 |
btn2 = gr.Button("Execute")
|
| 70 |
df2 = gr.Dataframe(label="Result", interactive=True)
|
| 71 |
status = gr.Textbox(label="Status", interactive=False)
|
| 72 |
cf2 = gr.Dataframe(label="Projected Cash-Flows (if applicable)", interactive=True)
|
| 73 |
gap2 = gr.Dataframe(label="Liquidity Gap (monthly)", interactive=True)
|
| 74 |
+
btn2.click(fn=run_sql, inputs=[sql_in], outputs=[df2, status, cf2, gap2])
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if __name__ == "__main__":
|
| 77 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|