Spaces:
Running
Running
update funcs with handling for output
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from database import engine, receipts
|
|
| 10 |
@tool
|
| 11 |
def sql_engine(query: str) -> str:
|
| 12 |
"""
|
| 13 |
-
Executes an SQL query on the 'receipts' table and returns results.
|
| 14 |
|
| 15 |
Table Schema:
|
| 16 |
- receipt_id: INTEGER
|
|
@@ -21,17 +21,20 @@ def sql_engine(query: str) -> str:
|
|
| 21 |
query: The SQL query to execute.
|
| 22 |
|
| 23 |
Returns:
|
| 24 |
-
Query result as a string.
|
| 25 |
"""
|
| 26 |
-
output = ""
|
| 27 |
try:
|
| 28 |
with engine.connect() as con:
|
| 29 |
-
rows = con.execute(text(query))
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
|
| 34 |
-
return output.strip()
|
| 35 |
|
| 36 |
# Initialize CodeAgent to generate SQL queries from natural language
|
| 37 |
agent = CodeAgent(
|
|
@@ -43,18 +46,26 @@ def query_sql(user_query: str) -> str:
|
|
| 43 |
"""
|
| 44 |
Converts natural language input to an SQL query using CodeAgent
|
| 45 |
and returns the execution results.
|
| 46 |
-
|
| 47 |
Args:
|
| 48 |
user_query: The user's request in natural language.
|
| 49 |
-
|
| 50 |
Returns:
|
| 51 |
-
The query result from the database.
|
| 52 |
"""
|
| 53 |
# Generate SQL from natural language
|
| 54 |
generated_sql = agent.run(f"Convert this request into SQL: {user_query}")
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
# Execute the SQL query and return the result
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Define Gradio interface
|
| 60 |
demo = gr.Interface(
|
|
|
|
| 10 |
@tool
|
| 11 |
def sql_engine(query: str) -> str:
|
| 12 |
"""
|
| 13 |
+
Executes an SQL query on the 'receipts' table and returns formatted results.
|
| 14 |
|
| 15 |
Table Schema:
|
| 16 |
- receipt_id: INTEGER
|
|
|
|
| 21 |
query: The SQL query to execute.
|
| 22 |
|
| 23 |
Returns:
|
| 24 |
+
Query result as a formatted string.
|
| 25 |
"""
|
|
|
|
| 26 |
try:
|
| 27 |
with engine.connect() as con:
|
| 28 |
+
rows = con.execute(text(query)).fetchall()
|
| 29 |
+
|
| 30 |
+
if not rows:
|
| 31 |
+
return "No results found."
|
| 32 |
+
|
| 33 |
+
# Convert results to a readable string
|
| 34 |
+
return "\n".join([", ".join(map(str, row)) for row in rows])
|
| 35 |
+
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"Error: {str(e)}"
|
|
|
|
| 38 |
|
| 39 |
# Initialize CodeAgent to generate SQL queries from natural language
|
| 40 |
agent = CodeAgent(
|
|
|
|
| 46 |
"""
|
| 47 |
Converts natural language input to an SQL query using CodeAgent
|
| 48 |
and returns the execution results.
|
| 49 |
+
|
| 50 |
Args:
|
| 51 |
user_query: The user's request in natural language.
|
| 52 |
+
|
| 53 |
Returns:
|
| 54 |
+
The query result from the database as a formatted string.
|
| 55 |
"""
|
| 56 |
# Generate SQL from natural language
|
| 57 |
generated_sql = agent.run(f"Convert this request into SQL: {user_query}")
|
| 58 |
|
| 59 |
+
# Log the generated SQL for debugging
|
| 60 |
+
print(f"Generated SQL: {generated_sql}")
|
| 61 |
+
|
| 62 |
# Execute the SQL query and return the result
|
| 63 |
+
result = sql_engine(generated_sql)
|
| 64 |
+
|
| 65 |
+
# Log the result for debugging
|
| 66 |
+
print(f"SQL Query Result: {result}")
|
| 67 |
+
|
| 68 |
+
return result
|
| 69 |
|
| 70 |
# Define Gradio interface
|
| 71 |
demo = gr.Interface(
|