Spaces:
Running
Running
handle numerical output
Browse files
app.py
CHANGED
|
@@ -60,25 +60,25 @@ def query_sql(user_query: str) -> str:
|
|
| 60 |
# Generate SQL query using the provided schema
|
| 61 |
generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# Log the generated SQL for debugging
|
| 64 |
print(f"Generated SQL: {generated_sql}")
|
| 65 |
|
| 66 |
-
# Ensure we only execute valid SELECT queries
|
| 67 |
if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
|
| 68 |
return "Error: Only SELECT queries are allowed."
|
| 69 |
|
| 70 |
-
# Execute the SQL query and return the result
|
| 71 |
result = sql_engine(generated_sql)
|
| 72 |
|
| 73 |
-
# Log the SQL query result
|
| 74 |
print(f"SQL Query Result: {result}")
|
| 75 |
-
|
| 76 |
-
# Ensure proper formatting based on content
|
| 77 |
try:
|
| 78 |
float_result = float(result)
|
| 79 |
-
return f"{float_result:.2f}"
|
| 80 |
except ValueError:
|
| 81 |
-
return result
|
| 82 |
|
| 83 |
def handle_query(user_input: str) -> str:
|
| 84 |
"""
|
|
|
|
| 60 |
# Generate SQL query using the provided schema
|
| 61 |
generated_sql = agent.run(f"{schema_info} Convert this request into SQL: {user_query}")
|
| 62 |
|
| 63 |
+
# Ensure generated_sql is always a string
|
| 64 |
+
if not isinstance(generated_sql, str):
|
| 65 |
+
return f"Unexpected result: {generated_sql}" # Handle unexpected numerical result
|
| 66 |
+
|
| 67 |
# Log the generated SQL for debugging
|
| 68 |
print(f"Generated SQL: {generated_sql}")
|
| 69 |
|
|
|
|
| 70 |
if not generated_sql.strip().lower().startswith(("select", "show", "pragma")):
|
| 71 |
return "Error: Only SELECT queries are allowed."
|
| 72 |
|
|
|
|
| 73 |
result = sql_engine(generated_sql)
|
| 74 |
|
|
|
|
| 75 |
print(f"SQL Query Result: {result}")
|
| 76 |
+
|
|
|
|
| 77 |
try:
|
| 78 |
float_result = float(result)
|
| 79 |
+
return f"{float_result:.2f}"
|
| 80 |
except ValueError:
|
| 81 |
+
return result
|
| 82 |
|
| 83 |
def handle_query(user_input: str) -> str:
|
| 84 |
"""
|