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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -39
app.py CHANGED
@@ -7,61 +7,56 @@ from smolagents import tool, CodeAgent, InferenceClientModel
7
  engine = create_engine("sqlite:///data.db")
8
  metadata_obj = MetaData()
9
 
10
- # --- Create receipts table ---
11
- receipts = Table(
12
- "receipts",
13
  metadata_obj,
14
- Column("receipt_id", Integer, primary_key=True),
15
- Column("customer_name", String(16), primary_key=True),
16
- Column("price", Float),
17
- Column("tip", Float),
18
  )
19
  metadata_obj.create_all(engine)
20
 
21
- # Insert sample data
22
- rows = [
23
- {"receipt_id": 1, "customer_name": "Alan Payne", "price": 12.06, "tip": 1.20},
24
- {"receipt_id": 2, "customer_name": "Alex Mason", "price": 23.86, "tip": 0.24},
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 ---
34
- waiters = Table(
35
- "waiters",
36
  metadata_obj,
37
- Column("receipt_id", Integer, primary_key=True),
38
- Column("waiter_name", String(16), primary_key=True),
 
39
  )
40
  metadata_obj.create_all(engine)
41
 
42
- rows = [
43
- {"receipt_id": 1, "waiter_name": "Corey Johnson"},
44
- {"receipt_id": 2, "waiter_name": "Michael Watts"},
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:
@@ -74,7 +69,7 @@ def sql_engine(query: str) -> list:
74
  # Dynamically describe tables
75
  updated_description = "Allows SQL queries on the following tables:\n"
76
  inspector = inspect(engine)
77
- for table in ["receipts", "waiters"]:
78
  columns_info = [(col["name"], col["type"]) for col in inspector.get_columns(table)]
79
  table_description = f"\n\nTable '{table}':\nColumns:\n" + "\n".join(
80
  [f" - {name}: {col_type}" for name, col_type in columns_info]
@@ -107,10 +102,10 @@ def ask_agent(question: str) -> str:
107
 
108
  demo = gr.Interface(
109
  fn=ask_agent,
110
- inputs=gr.Textbox(label="Ask a question (e.g., Who got the biggest tip?)"),
111
  outputs=gr.Textbox(label="Agent response"),
112
  title="🧠 Text-to-SQL Agent",
113
- description="Ask natural-language questions about a restaurant receipts database. Powered by smolagents + Meta-Llama-3.",
114
  )
115
 
116
  demo.launch()
 
7
  engine = create_engine("sqlite:///data.db")
8
  metadata_obj = MetaData()
9
 
10
+ # --- Create students table ---
11
+ students = Table(
12
+ "students",
13
  metadata_obj,
14
+ Column("student_id", Integer, primary_key=True),
15
+ Column("student_name", String(32), primary_key=True),
 
 
16
  )
17
  metadata_obj.create_all(engine)
18
 
19
+ # Insert sample student data
20
+ student_rows = [
21
+ {"student_id": 1, "student_name": "Alice Johnson"},
22
+ {"student_id": 2, "student_name": "Bob Smith"},
23
+ {"student_id": 3, "student_name": "Charlie Brown"},
24
+ {"student_id": 4, "student_name": "Diana Prince"},
25
  ]
26
+ for row in student_rows:
27
+ stmt = insert(students).values(**row)
28
  with engine.begin() as conn:
29
  conn.execute(stmt)
30
 
31
+ # --- Create subjects table ---
32
+ subjects = Table(
33
+ "subjects",
34
  metadata_obj,
35
+ Column("student_id", Integer, primary_key=True),
36
+ Column("subject_name", String(32), primary_key=True),
37
+ Column("marks", Float),
38
  )
39
  metadata_obj.create_all(engine)
40
 
41
+ # Insert sample subject/marks data
42
+ subject_rows = [
43
+ {"student_id": 1, "subject_name": "Math", "marks": 95.0},
44
+ {"student_id": 1, "subject_name": "English", "marks": 88.5},
45
+ {"student_id": 2, "subject_name": "Math", "marks": 72.0},
46
+ {"student_id": 2, "subject_name": "Science", "marks": 81.0},
47
+ {"student_id": 3, "subject_name": "Math", "marks": 85.0},
48
+ {"student_id": 3, "subject_name": "History", "marks": 90.0},
49
+ {"student_id": 4, "subject_name": "English", "marks": 78.0},
50
+ {"student_id": 4, "subject_name": "Science", "marks": 82.0},
51
  ]
52
+ for row in subject_rows:
53
+ stmt = insert(subjects).values(**row)
54
  with engine.begin() as conn:
55
  conn.execute(stmt)
56
 
57
  # --- Define SQL tool for the agent ---
58
  @tool
59
  def sql_engine(query: str) -> list:
 
 
 
 
 
 
 
 
 
60
  try:
61
  print(f"🧩 Executing query: {query}") # Debug log
62
  with engine.connect() as con:
 
69
  # Dynamically describe tables
70
  updated_description = "Allows SQL queries on the following tables:\n"
71
  inspector = inspect(engine)
72
+ for table in ["students", "subjects"]:
73
  columns_info = [(col["name"], col["type"]) for col in inspector.get_columns(table)]
74
  table_description = f"\n\nTable '{table}':\nColumns:\n" + "\n".join(
75
  [f" - {name}: {col_type}" for name, col_type in columns_info]
 
102
 
103
  demo = gr.Interface(
104
  fn=ask_agent,
105
+ inputs=gr.Textbox(label="Ask a question (e.g., Who scored highest in Math?)"),
106
  outputs=gr.Textbox(label="Agent response"),
107
  title="🧠 Text-to-SQL Agent",
108
+ description="Ask natural-language questions about students, subjects, and marks. Powered by smolagents + Meta-Llama-3.",
109
  )
110
 
111
  demo.launch()