Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import gradio as gr
|
|
| 6 |
from tools.sql_tool import SQLTool
|
| 7 |
from tools.ts_preprocess import build_timeseries
|
| 8 |
|
| 9 |
-
#
|
| 10 |
DUCKDB_PATH = os.getenv("DUCKDB_PATH", "alm.duckdb")
|
| 11 |
DEFAULT_SCHEMA = os.getenv("SQL_DEFAULT_SCHEMA", "main")
|
| 12 |
DEFAULT_TABLE = os.getenv("SQL_DEFAULT_TABLE", "masterdataset_v")
|
|
@@ -14,36 +14,43 @@ DEFAULT_TABLE = os.getenv("SQL_DEFAULT_TABLE", "masterdataset_v")
|
|
| 14 |
sql_tool = SQLTool(DUCKDB_PATH)
|
| 15 |
|
| 16 |
INTRO = f"""
|
| 17 |
-
### ALM LLM — Demo
|
|
|
|
| 18 |
Connected to **DuckDB** at `{DUCKDB_PATH}` using table **{DEFAULT_SCHEMA}.{DEFAULT_TABLE}**.
|
| 19 |
|
| 20 |
-
**Try:**
|
| 21 |
-
- *"show me the top 10 fds by portfolio value"*
|
| 22 |
-
- *"top 10 assets by portfolio value"*
|
| 23 |
-
- *"sum portfolio value by currency"*
|
| 24 |
"""
|
| 25 |
|
|
|
|
| 26 |
def run_nl(nl_query: str):
|
| 27 |
if not nl_query or not nl_query.strip():
|
| 28 |
-
return
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
df, sql, why = sql_tool.query_from_nl(nl_query)
|
| 32 |
except Exception as e:
|
| 33 |
return pd.DataFrame(), "", f"Error: {e}", pd.DataFrame(), pd.DataFrame()
|
| 34 |
|
| 35 |
-
# Try
|
| 36 |
try:
|
| 37 |
cf, gap = build_timeseries(df)
|
| 38 |
-
except Exception
|
| 39 |
cf, gap = pd.DataFrame(), pd.DataFrame()
|
| 40 |
|
| 41 |
return df, sql.strip(), why, cf, gap
|
| 42 |
|
|
|
|
| 43 |
def run_sql(sql_text: str):
|
| 44 |
if not sql_text or not sql_text.strip():
|
| 45 |
return pd.DataFrame(), "Please paste a SQL statement.", pd.DataFrame(), pd.DataFrame()
|
| 46 |
-
|
| 47 |
try:
|
| 48 |
df = sql_tool.run_sql(sql_text)
|
| 49 |
except Exception as e:
|
|
@@ -53,29 +60,52 @@ def run_sql(sql_text: str):
|
|
| 53 |
cf, gap = build_timeseries(df)
|
| 54 |
except Exception:
|
| 55 |
cf, gap = pd.DataFrame(), pd.DataFrame()
|
|
|
|
| 56 |
return df, "OK", cf, gap
|
| 57 |
|
| 58 |
-
with gr.Tab("Ask in Natural Language"):
|
| 59 |
-
nl = gr.Textbox(label="Ask a question", placeholder="e.g., show me the top 10 fds by portfolio value")
|
| 60 |
-
btn = gr.Button("Run")
|
| 61 |
-
sql_out = gr.Textbox(label="Generated SQL", interactive=False)
|
| 62 |
-
why_out = gr.Textbox(label="Reasoning", interactive=False)
|
| 63 |
-
df_out = gr.Dataframe(label="Query Result", interactive=True)
|
| 64 |
-
cf_out = gr.Dataframe(label="Projected Cash-Flows (if applicable)", interactive=True)
|
| 65 |
-
gap_out = gr.Dataframe(label="Liquidity Gap (monthly)", interactive=True)
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
with gr.Tab("Run Raw SQL"):
|
| 70 |
-
sql_in = gr.Code(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
btn2 = gr.Button("Execute")
|
| 72 |
-
df2
|
| 73 |
status = gr.Textbox(label="Status", interactive=False)
|
| 74 |
-
cf2
|
| 75 |
-
gap2
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
|
|
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
# Spaces set PORT automatically; otherwise, Gradio defaults are fine.
|
| 81 |
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 |
+
# --- Environment & defaults ---
|
| 10 |
DUCKDB_PATH = os.getenv("DUCKDB_PATH", "alm.duckdb")
|
| 11 |
DEFAULT_SCHEMA = os.getenv("SQL_DEFAULT_SCHEMA", "main")
|
| 12 |
DEFAULT_TABLE = os.getenv("SQL_DEFAULT_TABLE", "masterdataset_v")
|
|
|
|
| 14 |
sql_tool = SQLTool(DUCKDB_PATH)
|
| 15 |
|
| 16 |
INTRO = f"""
|
| 17 |
+
### ALM LLM — Demo
|
| 18 |
+
|
| 19 |
Connected to **DuckDB** at `{DUCKDB_PATH}` using table **{DEFAULT_SCHEMA}.{DEFAULT_TABLE}**.
|
| 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 |
+
# --- Handlers ---
|
| 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 |
+
|
| 51 |
def run_sql(sql_text: str):
|
| 52 |
if not sql_text or not sql_text.strip():
|
| 53 |
return pd.DataFrame(), "Please paste a SQL statement.", pd.DataFrame(), pd.DataFrame()
|
|
|
|
| 54 |
try:
|
| 55 |
df = sql_tool.run_sql(sql_text)
|
| 56 |
except Exception as e:
|
|
|
|
| 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 {DEFAULT_SCHEMA}.{DEFAULT_TABLE} LIMIT 20;",
|
| 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)))
|