ZennyKenny commited on
Commit
215368b
·
verified ·
1 Parent(s): 61d9b40

add handle_query

Browse files
Files changed (1) hide show
  1. app.py +14 -21
app.py CHANGED
@@ -11,7 +11,7 @@ from database import engine, receipts
11
  def sql_engine(query: str) -> str:
12
  """
13
  Executes an SQL query on the 'receipts' table and returns formatted results.
14
-
15
  Args:
16
  query: The SQL query to execute.
17
 
@@ -25,7 +25,7 @@ def sql_engine(query: str) -> str:
25
  if not rows:
26
  return "No results found."
27
 
28
- # Convert results into a readable string format
29
  return "\n".join([", ".join(map(str, row)) for row in rows])
30
 
31
  except Exception as e:
@@ -70,26 +70,19 @@ def query_sql(user_query: str) -> str:
70
  # Log the SQL query result
71
  print(f"SQL Query Result: {result}")
72
 
73
- return result # Return the final result, NOT the generated SQL
74
-
75
-
76
- # Generate SQL query using the provided schema
77
- generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
78
-
79
- # Log the generated SQL for debugging
80
- print(f"Generated SQL: {generated_sql}")
81
-
82
- # Ensure we only execute valid SELECT queries
83
- if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
84
- return "Error: Only SELECT queries are allowed."
85
 
86
- # Execute the SQL query and return the result
87
- result = sql_engine(generated_sql)
 
88
 
89
- # Log the SQL query result
90
- print(f"SQL Query Result: {result}")
91
 
92
- return result
 
 
 
93
 
94
  # Initialize CodeAgent to generate SQL queries from natural language
95
  agent = CodeAgent(
@@ -97,9 +90,9 @@ agent = CodeAgent(
97
  model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
98
  )
99
 
100
- # Define Gradio interface
101
  demo = gr.Interface(
102
- fn=query_sql,
103
  inputs=gr.Textbox(label="Enter your query in plain English"),
104
  outputs=gr.Textbox(label="Query Result"),
105
  title="Natural Language to SQL Executor",
 
11
  def sql_engine(query: str) -> str:
12
  """
13
  Executes an SQL query on the 'receipts' table and returns formatted results.
14
+
15
  Args:
16
  query: The SQL query to execute.
17
 
 
25
  if not rows:
26
  return "No results found."
27
 
28
+ # Convert query results into a clean, readable format
29
  return "\n".join([", ".join(map(str, row)) for row in rows])
30
 
31
  except Exception as e:
 
70
  # Log the SQL query result
71
  print(f"SQL Query Result: {result}")
72
 
73
+ return result # Return only the final query result, NOT the generated SQL
 
 
 
 
 
 
 
 
 
 
 
74
 
75
+ def handle_query(user_input: str) -> str:
76
+ """
77
+ Calls query_sql, captures the output, and directly returns it to the UI.
78
 
79
+ Args:
80
+ user_input: The user's natural language question.
81
 
82
+ Returns:
83
+ The SQL query result as a plain string to be displayed in the UI.
84
+ """
85
+ return query_sql(user_input) # Directly return the processed result
86
 
87
  # Initialize CodeAgent to generate SQL queries from natural language
88
  agent = CodeAgent(
 
90
  model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
91
  )
92
 
93
+ # Define Gradio interface using handle_query instead of query_sql
94
  demo = gr.Interface(
95
+ fn=handle_query, # Call handle_query to return the final SQL output
96
  inputs=gr.Textbox(label="Enter your query in plain English"),
97
  outputs=gr.Textbox(label="Query Result"),
98
  title="Natural Language to SQL Executor",