ZennyKenny commited on
Commit
1df3c5d
·
verified ·
1 Parent(s): 8be3748

aesthetics

Browse files
Files changed (1) hide show
  1. app.py +5 -13
app.py CHANGED
@@ -5,7 +5,6 @@ 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
  import pandas as pd
@@ -69,7 +68,7 @@ def query_sql(user_query: str) -> str:
69
  Returns:
70
  The query result from the database as a formatted string.
71
  """
72
- # Provide the AI with the correct schema and strict instructions
73
  schema_info = (
74
  "The database has a table named 'receipts' with the following schema:\n"
75
  "- receipt_id (INTEGER, primary key)\n"
@@ -80,14 +79,11 @@ def query_sql(user_query: str) -> str:
80
  "DO NOT explain your reasoning, and DO NOT return anything other than the SQL query itself."
81
  )
82
 
83
- # Generate SQL query using the provided schema
84
  generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
85
 
86
- # Ensure generated_sql is always a string
87
  if not isinstance(generated_sql, str):
88
  return f"Unexpected result: {generated_sql}" # Handle unexpected numerical result
89
 
90
- # Log the generated SQL for debugging
91
  print(f"Generated SQL: {generated_sql}")
92
 
93
  if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
@@ -95,7 +91,6 @@ def query_sql(user_query: str) -> str:
95
 
96
  result = sql_engine(generated_sql)
97
 
98
- #print(f"SQL Query Result: {result}")
99
  print(f"{result}")
100
 
101
  try:
@@ -114,11 +109,10 @@ def handle_query(user_input: str) -> str:
114
  Returns:
115
  The SQL query result as a plain string to be displayed in the UI.
116
  """
117
- return query_sql(user_input) # Directly return the processed result
118
 
119
- # Initialize CodeAgent to generate SQL queries from natural language
120
  agent = CodeAgent(
121
- tools=[sql_engine], # Ensure sql_engine is properly registered
122
  model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
123
  )
124
 
@@ -126,18 +120,16 @@ 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)
142
 
143
  if __name__ == "__main__":
 
5
  import spaces
6
  import pandas as pd
7
 
 
8
  from database import engine, receipts
9
 
10
  import pandas as pd
 
68
  Returns:
69
  The query result from the database as a formatted string.
70
  """
71
+
72
  schema_info = (
73
  "The database has a table named 'receipts' with the following schema:\n"
74
  "- receipt_id (INTEGER, primary key)\n"
 
79
  "DO NOT explain your reasoning, and DO NOT return anything other than the SQL query itself."
80
  )
81
 
 
82
  generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
83
 
 
84
  if not isinstance(generated_sql, str):
85
  return f"Unexpected result: {generated_sql}" # Handle unexpected numerical result
86
 
 
87
  print(f"Generated SQL: {generated_sql}")
88
 
89
  if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
 
91
 
92
  result = sql_engine(generated_sql)
93
 
 
94
  print(f"{result}")
95
 
96
  try:
 
109
  Returns:
110
  The SQL query result as a plain string to be displayed in the UI.
111
  """
112
+ return query_sql(user_input)
113
 
 
114
  agent = CodeAgent(
115
+ tools=[sql_engine],
116
  model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
117
  )
118
 
 
120
  gr.Markdown("## Natural Language to SQL Executor with Live Data")
121
 
122
  with gr.Row():
123
+ with gr.Column(scale=1):
124
  user_input = gr.Textbox(label="Enter your query in plain English")
125
  query_output = gr.Textbox(label="Query Result")
126
 
127
+ with gr.Column(scale=2):
128
  gr.Markdown("### Receipts Table (Live View)")
129
  receipts_table = gr.Dataframe(value=get_receipts_table(), label="Receipts Table")
130
 
 
131
  user_input.change(fn=handle_query, inputs=user_input, outputs=query_output)
132
 
 
133
  demo.load(fn=get_receipts_table, outputs=receipts_table)
134
 
135
  if __name__ == "__main__":