Sayandip commited on
Commit
a99657f
·
verified ·
1 Parent(s): 9851cfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import os
2
  import gradio as gr
3
- from sqlalchemy import (
4
- create_engine, MetaData, Table, Column, String, Integer, Float, insert, text, inspect
5
- )
6
  from smolagents import tool, CodeAgent, InferenceClientModel
7
 
8
  # --- Setup SQLite database (persistent in Space) ---
@@ -27,9 +25,9 @@ rows = [
27
  {"receipt_id": 3, "customer_name": "Woodrow Wilson", "price": 53.43, "tip": 5.43},
28
  {"receipt_id": 4, "customer_name": "Margaret James", "price": 21.11, "tip": 1.00},
29
  ]
30
- with engine.begin() as conn:
31
- for row in rows:
32
- stmt = insert(receipts).values(**row)
33
  conn.execute(stmt)
34
 
35
  # --- Create waiters table ---
@@ -47,20 +45,28 @@ rows = [
47
  {"receipt_id": 3, "waiter_name": "Michael Watts"},
48
  {"receipt_id": 4, "waiter_name": "Margaret James"},
49
  ]
50
- with engine.begin() as conn:
51
- for row in rows:
52
- stmt = insert(waiters).values(**row)
53
  conn.execute(stmt)
54
 
55
  # --- Define SQL tool for the agent ---
56
  @tool
57
  def sql_engine(query: str) -> list:
58
- """Executes SQL queries on the available tables: receipts and waiters."""
 
 
 
 
 
 
 
 
59
  try:
60
  print(f"🧩 Executing query: {query}") # Debug log
61
  with engine.connect() as con:
62
  rows = con.execute(text(query))
63
- results = [tuple(row) for row in rows]
64
  return results or []
65
  except Exception as e:
66
  return [f"⚠️ SQL Error: {str(e)}"]
@@ -74,6 +80,7 @@ for table in ["receipts", "waiters"]:
74
  [f" - {name}: {col_type}" for name, col_type in columns_info]
75
  )
76
  updated_description += table_description
 
77
  sql_engine.description = updated_description
78
 
79
  # --- Create the agent ---
@@ -90,6 +97,7 @@ def ask_agent(question: str) -> str:
90
  """Ask the AI agent a question and return its answer."""
91
  try:
92
  result = agent.run(question)
 
93
  if isinstance(result, list):
94
  result_str = "\n".join(str(r) for r in result)
95
  return result_str or "No results."
 
1
  import os
2
  import gradio as gr
3
+ from sqlalchemy import create_engine, MetaData, Table, Column, String, Integer, Float, insert, text, inspect
 
 
4
  from smolagents import tool, CodeAgent, InferenceClientModel
5
 
6
  # --- Setup SQLite database (persistent in Space) ---
 
25
  {"receipt_id": 3, "customer_name": "Woodrow Wilson", "price": 53.43, "tip": 5.43},
26
  {"receipt_id": 4, "customer_name": "Margaret James", "price": 21.11, "tip": 1.00},
27
  ]
28
+ for row in rows:
29
+ stmt = insert(receipts).values(**row)
30
+ with engine.begin() as conn:
31
  conn.execute(stmt)
32
 
33
  # --- Create waiters table ---
 
45
  {"receipt_id": 3, "waiter_name": "Michael Watts"},
46
  {"receipt_id": 4, "waiter_name": "Margaret James"},
47
  ]
48
+ for row in rows:
49
+ stmt = insert(waiters).values(**row)
50
+ with engine.begin() as conn:
51
  conn.execute(stmt)
52
 
53
  # --- Define SQL tool for the agent ---
54
  @tool
55
  def sql_engine(query: str) -> list:
56
+ """
57
+ Executes SQL queries on the available tables: receipts and waiters.
58
+
59
+ Args:
60
+ query: SQL query string.
61
+
62
+ Returns:
63
+ List of tuples with query results.
64
+ """
65
  try:
66
  print(f"🧩 Executing query: {query}") # Debug log
67
  with engine.connect() as con:
68
  rows = con.execute(text(query))
69
+ results = [tuple(row) for row in rows] # Keep results as tuples
70
  return results or []
71
  except Exception as e:
72
  return [f"⚠️ SQL Error: {str(e)}"]
 
80
  [f" - {name}: {col_type}" for name, col_type in columns_info]
81
  )
82
  updated_description += table_description
83
+
84
  sql_engine.description = updated_description
85
 
86
  # --- Create the agent ---
 
97
  """Ask the AI agent a question and return its answer."""
98
  try:
99
  result = agent.run(question)
100
+ # Convert tuples to readable string if result is a list of tuples
101
  if isinstance(result, list):
102
  result_str = "\n".join(str(r) for r in result)
103
  return result_str or "No results."