Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -191,13 +191,11 @@ def query_sql(user_query: str) -> str:
|
|
191 |
"""
|
192 |
Converts natural language input to an SQL query using CodeAgent.
|
193 |
"""
|
194 |
-
# Get current table information
|
195 |
table_name, column_names, column_info = get_table_info()
|
196 |
|
197 |
if not table_name:
|
198 |
return "Error: No data table exists. Please upload a file first."
|
199 |
|
200 |
-
# Create schema information with actual column names
|
201 |
schema_info = (
|
202 |
f"The database has a table named '{table_name}' with the following columns:\n"
|
203 |
+ "\n".join([
|
@@ -211,12 +209,15 @@ def query_sql(user_query: str) -> str:
|
|
211 |
"DO NOT explain your reasoning, and DO NOT return anything other than the SQL query itself."
|
212 |
)
|
213 |
|
214 |
-
# Get
|
215 |
generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
|
216 |
|
217 |
-
# Clean up and validate the SQL
|
218 |
if not isinstance(generated_sql, str):
|
219 |
-
return
|
|
|
|
|
|
|
|
|
220 |
|
221 |
# Extract just the SQL query if there's additional text
|
222 |
sql_lines = [line for line in generated_sql.split('\n') if 'select' in line.lower()]
|
@@ -247,7 +248,11 @@ def query_sql(user_query: str) -> str:
|
|
247 |
return f"{float_result:,.0f}" # Format with commas, no decimals
|
248 |
except ValueError:
|
249 |
return result
|
|
|
250 |
except Exception as e:
|
|
|
|
|
|
|
251 |
return f"Error executing query: {str(e)}"
|
252 |
|
253 |
# Create the Gradio interface
|
|
|
191 |
"""
|
192 |
Converts natural language input to an SQL query using CodeAgent.
|
193 |
"""
|
|
|
194 |
table_name, column_names, column_info = get_table_info()
|
195 |
|
196 |
if not table_name:
|
197 |
return "Error: No data table exists. Please upload a file first."
|
198 |
|
|
|
199 |
schema_info = (
|
200 |
f"The database has a table named '{table_name}' with the following columns:\n"
|
201 |
+ "\n".join([
|
|
|
209 |
"DO NOT explain your reasoning, and DO NOT return anything other than the SQL query itself."
|
210 |
)
|
211 |
|
212 |
+
# Get SQL from the agent
|
213 |
generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
|
214 |
|
|
|
215 |
if not isinstance(generated_sql, str):
|
216 |
+
return "Error: Invalid query generated"
|
217 |
+
|
218 |
+
# Clean up the SQL
|
219 |
+
if generated_sql.isnumeric(): # If the agent returned just a number
|
220 |
+
return generated_sql
|
221 |
|
222 |
# Extract just the SQL query if there's additional text
|
223 |
sql_lines = [line for line in generated_sql.split('\n') if 'select' in line.lower()]
|
|
|
248 |
return f"{float_result:,.0f}" # Format with commas, no decimals
|
249 |
except ValueError:
|
250 |
return result
|
251 |
+
|
252 |
except Exception as e:
|
253 |
+
if str(e).startswith("(sqlite3.OperationalError) near"):
|
254 |
+
# If it's a SQL syntax error, return the raw result
|
255 |
+
return generated_sql
|
256 |
return f"Error executing query: {str(e)}"
|
257 |
|
258 |
# Create the Gradio interface
|