add UI support for underlying flat file table
Browse files
app.py
CHANGED
@@ -3,10 +3,35 @@ import gradio as gr
|
|
3 |
from sqlalchemy import text
|
4 |
from smolagents import tool, CodeAgent, HfApiModel
|
5 |
import spaces
|
|
|
6 |
|
7 |
# Import the persistent database
|
8 |
from database import engine, receipts
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
@tool
|
11 |
def sql_engine(query: str) -> str:
|
12 |
"""
|
@@ -97,14 +122,25 @@ agent = CodeAgent(
|
|
97 |
model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
|
98 |
)
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
)
|
109 |
|
110 |
if __name__ == "__main__":
|
|
|
3 |
from sqlalchemy import text
|
4 |
from smolagents import tool, CodeAgent, HfApiModel
|
5 |
import spaces
|
6 |
+
import pandas as pd
|
7 |
|
8 |
# Import the persistent database
|
9 |
from database import engine, receipts
|
10 |
|
11 |
+
def get_receipts_table():
|
12 |
+
"""
|
13 |
+
Fetches all data from the 'receipts' table and returns it as a Pandas DataFrame.
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
A Pandas DataFrame containing all receipt data.
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
with engine.connect() as con:
|
20 |
+
result = con.execute(text("SELECT * FROM receipts"))
|
21 |
+
rows = result.fetchall()
|
22 |
+
|
23 |
+
if not rows:
|
24 |
+
return pd.DataFrame(columns=["receipt_id", "customer_name", "price", "tip"])
|
25 |
+
|
26 |
+
# Convert rows into a DataFrame
|
27 |
+
df = pd.DataFrame(rows, columns=["receipt_id", "customer_name", "price", "tip"])
|
28 |
+
|
29 |
+
return df
|
30 |
+
|
31 |
+
except Exception as e:
|
32 |
+
return pd.DataFrame({"Error": [str(e)]}) # Return error message in DataFrame format
|
33 |
+
|
34 |
+
|
35 |
@tool
|
36 |
def sql_engine(query: str) -> str:
|
37 |
"""
|
|
|
122 |
model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
|
123 |
)
|
124 |
|
125 |
+
demo = gr.TabbedInterface(
|
126 |
+
[
|
127 |
+
gr.Interface(
|
128 |
+
fn=handle_query,
|
129 |
+
inputs=gr.Textbox(label="Enter your query in plain English"),
|
130 |
+
outputs=gr.Textbox(label="Query Result"),
|
131 |
+
title="Natural Language to SQL Executor",
|
132 |
+
description="Enter a plain English request, and the AI will generate an SQL query and return the results.",
|
133 |
+
flagging_mode="never",
|
134 |
+
),
|
135 |
+
gr.Interface(
|
136 |
+
fn=get_receipts_table,
|
137 |
+
inputs=[],
|
138 |
+
outputs=gr.Dataframe(label="Receipts Table"),
|
139 |
+
title="Database Viewer",
|
140 |
+
description="This table shows all receipts stored in the database.",
|
141 |
+
),
|
142 |
+
],
|
143 |
+
tab_names=["SQL Query", "Receipts Table"],
|
144 |
)
|
145 |
|
146 |
if __name__ == "__main__":
|