ZennyKenny commited on
Commit
1f7ee11
·
verified ·
1 Parent(s): edb7e14

update tools

Browse files
Files changed (1) hide show
  1. app.py +39 -35
app.py CHANGED
@@ -8,40 +8,6 @@ import spaces
8
  from database import engine, receipts
9
 
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
17
- - customer_name: VARCHAR(16)
18
- - price: FLOAT
19
- - tip: FLOAT
20
- Args:
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(
41
- tools=[sql_engine], # Ensure sql_engine is properly registered
42
- model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
43
- )
44
-
45
  def query_sql(user_query: str) -> str:
46
  """
47
  Converts natural language input to an SQL query using CodeAgent
@@ -59,13 +25,51 @@ def query_sql(user_query: str) -> str:
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(
 
8
  from database import engine, receipts
9
 
10
  @tool
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def query_sql(user_query: str) -> str:
12
  """
13
  Converts natural language input to an SQL query using CodeAgent
 
25
  # Log the generated SQL for debugging
26
  print(f"Generated SQL: {generated_sql}")
27
 
28
+ # Ensure we only execute valid SQL queries
29
+ if not generated_sql.lower().startswith(("select", "show", "pragma")):
30
+ return "Error: Only SELECT queries are allowed."
31
+
32
  # Execute the SQL query and return the result
33
  result = sql_engine(generated_sql)
34
 
35
  # Log the result for debugging
36
  print(f"SQL Query Result: {result}")
37
 
38
+ # If the result is empty, return a friendly message
39
+ if not result.strip():
40
+ return "No results found."
41
+
42
+ return result # Return clean result, avoiding passing raw SQL back
43
+
44
+ # Initialize CodeAgent to generate SQL queries from natural language
45
+ agent = CodeAgent(
46
+ tools=[sql_engine], # Ensure sql_engine is properly registered
47
+ model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
48
+ )
49
+
50
+ @tool
51
+ def sql_engine(query: str) -> str:
52
+ """
53
+ Executes an SQL query on the 'receipts' table and returns formatted results.
54
+
55
+ Args:
56
+ query: The SQL query to execute.
57
+
58
+ Returns:
59
+ Query result as a formatted string.
60
+ """
61
+ try:
62
+ with engine.connect() as con:
63
+ rows = con.execute(text(query)).fetchall()
64
+
65
+ if not rows:
66
+ return "No results found."
67
+
68
+ # Convert results into a readable string format
69
+ return "\n".join([", ".join(map(str, row)) for row in rows])
70
+
71
+ except Exception as e:
72
+ return f"Error: {str(e)}"
73
 
74
  # Define Gradio interface
75
  demo = gr.Interface(