improve table retrieval
Browse files
app.py
CHANGED
@@ -8,6 +8,8 @@ import pandas as pd
|
|
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.
|
@@ -25,13 +27,11 @@ def get_receipts_table():
|
|
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,26 +122,23 @@ agent = CodeAgent(
|
|
122 |
model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
|
123 |
)
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
],
|
143 |
-
tab_names=["SQL Query", "Receipts Table"],
|
144 |
-
)
|
145 |
|
146 |
if __name__ == "__main__":
|
147 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
8 |
# Import the persistent database
|
9 |
from database import engine, receipts
|
10 |
|
11 |
+
import pandas as pd
|
12 |
+
|
13 |
def get_receipts_table():
|
14 |
"""
|
15 |
Fetches all data from the 'receipts' table and returns it as a Pandas DataFrame.
|
|
|
27 |
|
28 |
# Convert rows into a DataFrame
|
29 |
df = pd.DataFrame(rows, columns=["receipt_id", "customer_name", "price", "tip"])
|
|
|
30 |
return df
|
31 |
|
32 |
except Exception as e:
|
33 |
return pd.DataFrame({"Error": [str(e)]}) # Return error message in DataFrame format
|
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 |
+
with gr.Blocks() as demo:
|
126 |
+
gr.Markdown("## Natural Language to SQL Executor with Live Data")
|
127 |
+
|
128 |
+
with gr.Row():
|
129 |
+
with gr.Column(scale=1): # Left: Query Interface
|
130 |
+
user_input = gr.Textbox(label="Enter your query in plain English")
|
131 |
+
query_output = gr.Textbox(label="Query Result")
|
132 |
+
|
133 |
+
with gr.Column(scale=2): # Right: Live Database Table
|
134 |
+
gr.Markdown("### Receipts Table (Live View)")
|
135 |
+
receipts_table = gr.Dataframe(value=get_receipts_table(), label="Receipts Table")
|
136 |
+
|
137 |
+
# Query handling function
|
138 |
+
user_input.change(fn=handle_query, inputs=user_input, outputs=query_output)
|
139 |
+
|
140 |
+
# Auto-refresh table every 5 seconds
|
141 |
+
demo.load(fn=get_receipts_table, outputs=receipts_table, every=5)
|
|
|
|
|
|
|
142 |
|
143 |
if __name__ == "__main__":
|
144 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|