ZennyKenny commited on
Commit
1767e22
ยท
verified ยท
1 Parent(s): 20e319d

improve table retrieval

Browse files
Files changed (1) hide show
  1. app.py +19 -22
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
- 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__":
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)