Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -6,20 +6,20 @@ import pandas as pd
|
|
6 |
import tempfile
|
7 |
from database import engine, initialize_database
|
8 |
|
9 |
-
# Ensure the database initializes
|
10 |
initialize_database()
|
11 |
|
12 |
# SQL Execution Tool (FIXED - Defined BEFORE Use)
|
13 |
@tool
|
14 |
def sql_engine(query: str) -> str:
|
15 |
"""
|
16 |
-
Executes an SQL SELECT query
|
17 |
|
18 |
Args:
|
19 |
-
query (str): The SQL query
|
20 |
|
21 |
Returns:
|
22 |
-
str: A formatted string
|
23 |
"""
|
24 |
try:
|
25 |
with engine.connect() as con:
|
@@ -30,7 +30,7 @@ def sql_engine(query: str) -> str:
|
|
30 |
except Exception as e:
|
31 |
return f"Error: {str(e)}"
|
32 |
|
33 |
-
# Initialize CodeAgent for SQL query generation
|
34 |
agent = CodeAgent(
|
35 |
tools=[sql_engine],
|
36 |
model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
|
@@ -132,49 +132,6 @@ def handle_file_upload(file):
|
|
132 |
|
133 |
return result, table_data
|
134 |
|
135 |
-
# Function to handle natural language to SQL conversion
|
136 |
-
def query_sql(user_query: str) -> str:
|
137 |
-
"""
|
138 |
-
Converts a user's natural language query into an SQL query.
|
139 |
-
|
140 |
-
Args:
|
141 |
-
user_query (str): The question asked by the user.
|
142 |
-
|
143 |
-
Returns:
|
144 |
-
str: The results of the executed SQL query.
|
145 |
-
"""
|
146 |
-
tables = get_table_names()
|
147 |
-
if not tables:
|
148 |
-
return "Error: No tables found. Please upload an SQL file first."
|
149 |
-
|
150 |
-
schema_info = "Available tables and columns:\n"
|
151 |
-
|
152 |
-
for table in tables:
|
153 |
-
columns = get_table_schema(table)
|
154 |
-
schema_info += f"Table '{table}' has columns: {', '.join(columns)}.\n"
|
155 |
-
|
156 |
-
schema_info += "Generate a valid SQL SELECT query using ONLY these column names. DO NOT return anything other than the SQL query itself."
|
157 |
-
|
158 |
-
generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
|
159 |
-
|
160 |
-
if not isinstance(generated_sql, str) or not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
|
161 |
-
return "Error: Only SELECT queries are allowed."
|
162 |
-
|
163 |
-
return sql_engine(generated_sql)
|
164 |
-
|
165 |
-
# Function to handle query input
|
166 |
-
def handle_query(user_input: str) -> str:
|
167 |
-
"""
|
168 |
-
Handles user input and returns the SQL query result.
|
169 |
-
|
170 |
-
Args:
|
171 |
-
user_input (str): User's natural language query.
|
172 |
-
|
173 |
-
Returns:
|
174 |
-
str: The query result or error message.
|
175 |
-
"""
|
176 |
-
return query_sql(user_input)
|
177 |
-
|
178 |
# Gradio UI
|
179 |
with gr.Blocks() as demo:
|
180 |
gr.Markdown("## SQL Query Interface")
|
@@ -183,7 +140,7 @@ with gr.Blocks() as demo:
|
|
183 |
user_input = gr.Textbox(label="Ask a question about the data")
|
184 |
query_output = gr.Textbox(label="Result")
|
185 |
|
186 |
-
user_input.change(fn=
|
187 |
|
188 |
gr.Markdown("## Upload SQL File to Execute")
|
189 |
file_upload = gr.File(label="Upload SQL File")
|
|
|
6 |
import tempfile
|
7 |
from database import engine, initialize_database
|
8 |
|
9 |
+
# Ensure the database initializes with placeholder data
|
10 |
initialize_database()
|
11 |
|
12 |
# SQL Execution Tool (FIXED - Defined BEFORE Use)
|
13 |
@tool
|
14 |
def sql_engine(query: str) -> str:
|
15 |
"""
|
16 |
+
Executes an SQL SELECT query on the database.
|
17 |
|
18 |
Args:
|
19 |
+
query (str): The SQL query to be executed. Only SELECT queries are allowed.
|
20 |
|
21 |
Returns:
|
22 |
+
str: A formatted string with query results, or an error message if the query fails.
|
23 |
"""
|
24 |
try:
|
25 |
with engine.connect() as con:
|
|
|
30 |
except Exception as e:
|
31 |
return f"Error: {str(e)}"
|
32 |
|
33 |
+
# Initialize CodeAgent for SQL query generation
|
34 |
agent = CodeAgent(
|
35 |
tools=[sql_engine],
|
36 |
model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
|
|
|
132 |
|
133 |
return result, table_data
|
134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
# Gradio UI
|
136 |
with gr.Blocks() as demo:
|
137 |
gr.Markdown("## SQL Query Interface")
|
|
|
140 |
user_input = gr.Textbox(label="Ask a question about the data")
|
141 |
query_output = gr.Textbox(label="Result")
|
142 |
|
143 |
+
user_input.change(fn=query_sql, inputs=user_input, outputs=query_output)
|
144 |
|
145 |
gr.Markdown("## Upload SQL File to Execute")
|
146 |
file_upload = gr.File(label="Upload SQL File")
|