Sayandip commited on
Commit
bff2a36
·
verified ·
1 Parent(s): 1363d5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -3,11 +3,11 @@ 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 file) ---
7
  engine = create_engine("sqlite:///data.db")
8
  metadata_obj = MetaData()
9
 
10
- # Create receipts table
11
  receipts = Table(
12
  "receipts",
13
  metadata_obj,
@@ -30,7 +30,7 @@ for row in rows:
30
  with engine.begin() as conn:
31
  conn.execute(stmt)
32
 
33
- # Create waiters table
34
  waiters = Table(
35
  "waiters",
36
  metadata_obj,
@@ -52,7 +52,7 @@ for row in rows:
52
 
53
  # --- Define SQL tool for the agent ---
54
  @tool
55
- def sql_engine(query: str) -> str:
56
  """
57
  Executes SQL queries on the available tables: receipts and waiters.
58
 
@@ -60,18 +60,16 @@ def sql_engine(query: str) -> str:
60
  query: SQL query string.
61
 
62
  Returns:
63
- A string representing the query results, or an error message.
64
  """
65
  try:
66
- print(f"🧩 Executing query: {query}") # Debug log in Space console
67
  with engine.connect() as con:
68
  rows = con.execute(text(query))
69
- results = [tuple(row) for row in rows]
70
- if not results:
71
- return "No results."
72
- return "\n".join(str(r) for r in results)
73
  except Exception as e:
74
- return f"⚠️ SQL Error: {str(e)}"
75
 
76
  # Dynamically describe tables
77
  updated_description = "Allows SQL queries on the following tables:\n"
@@ -85,20 +83,26 @@ for table in ["receipts", "waiters"]:
85
  sql_engine.description = updated_description
86
 
87
  # --- Create the agent ---
88
- # Use the HF_TOKEN secret stored in the Space
89
  agent = CodeAgent(
90
  tools=[sql_engine],
91
  model=InferenceClientModel(
92
  "meta-llama/Meta-Llama-3-8B-Instruct",
93
- api_key=os.environ.get("HF_TOKEN") # 👈 Hugging Face secret
94
  ),
95
  )
96
 
97
  # --- Define Gradio interface ---
98
  def ask_agent(question: str) -> str:
99
- """Ask the AI agent a question."""
100
- result = agent.run(question)
101
- return result
 
 
 
 
 
 
 
102
 
103
  demo = gr.Interface(
104
  fn=ask_agent,
 
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) ---
7
  engine = create_engine("sqlite:///data.db")
8
  metadata_obj = MetaData()
9
 
10
+ # --- Create receipts table ---
11
  receipts = Table(
12
  "receipts",
13
  metadata_obj,
 
30
  with engine.begin() as conn:
31
  conn.execute(stmt)
32
 
33
+ # --- Create waiters table ---
34
  waiters = Table(
35
  "waiters",
36
  metadata_obj,
 
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
 
 
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)}"]
73
 
74
  # Dynamically describe tables
75
  updated_description = "Allows SQL queries on the following tables:\n"
 
83
  sql_engine.description = updated_description
84
 
85
  # --- Create the agent ---
 
86
  agent = CodeAgent(
87
  tools=[sql_engine],
88
  model=InferenceClientModel(
89
  "meta-llama/Meta-Llama-3-8B-Instruct",
90
+ api_key=os.environ.get("HF_TOKEN") # Use secret from Space
91
  ),
92
  )
93
 
94
  # --- Define Gradio interface ---
95
  def ask_agent(question: str) -> str:
96
+ """Ask the AI agent a question and return its answer."""
97
+ try:
98
+ result = agent.run(question)
99
+ # Convert tuples to readable string if result is a list of tuples
100
+ if isinstance(result, list):
101
+ result_str = "\n".join(str(r) for r in result)
102
+ return result_str or "No results."
103
+ return str(result)
104
+ except Exception as e:
105
+ return f"⚠️ Error: {str(e)}"
106
 
107
  demo = gr.Interface(
108
  fn=ask_agent,