Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,63 +3,64 @@ 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 |
-
#
|
| 11 |
-
|
| 12 |
-
receipts
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
metadata_obj.create_all(engine)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
waiters
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
metadata_obj.create_all(engine)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
|
| 55 |
# --- Define SQL tool for the agent ---
|
| 56 |
@tool
|
| 57 |
-
def sql_engine(query: str):
|
| 58 |
"""
|
| 59 |
Executes SQL queries on the available tables: receipts and waiters.
|
| 60 |
|
| 61 |
Args:
|
| 62 |
query: SQL query string.
|
|
|
|
|
|
|
|
|
|
| 63 |
"""
|
| 64 |
try:
|
| 65 |
print(f"🧩 Executing query: {query}") # Debug log in Space console
|
|
@@ -68,11 +69,11 @@ def sql_engine(query: str):
|
|
| 68 |
results = [tuple(row) for row in rows]
|
| 69 |
if not results:
|
| 70 |
return "No results."
|
| 71 |
-
return results
|
| 72 |
except Exception as e:
|
| 73 |
return f"⚠️ SQL Error: {str(e)}"
|
| 74 |
|
| 75 |
-
#
|
| 76 |
updated_description = "Allows SQL queries on the following tables:\n"
|
| 77 |
inspector = inspect(engine)
|
| 78 |
for table in ["receipts", "waiters"]:
|
|
@@ -83,25 +84,26 @@ for table in ["receipts", "waiters"]:
|
|
| 83 |
updated_description += table_description
|
| 84 |
sql_engine.description = updated_description
|
| 85 |
|
| 86 |
-
# --- Create the
|
|
|
|
| 87 |
agent = CodeAgent(
|
| 88 |
tools=[sql_engine],
|
| 89 |
model=InferenceClientModel(
|
| 90 |
"meta-llama/Meta-Llama-3-8B-Instruct",
|
| 91 |
-
api_key=os.environ.get("HF_TOKEN")
|
| 92 |
),
|
| 93 |
)
|
| 94 |
|
| 95 |
# --- Define Gradio interface ---
|
| 96 |
-
def ask_agent(question):
|
| 97 |
-
"""Ask the AI agent a question
|
| 98 |
result = agent.run(question)
|
| 99 |
return result
|
| 100 |
|
| 101 |
demo = gr.Interface(
|
| 102 |
fn=ask_agent,
|
| 103 |
-
inputs=gr.Textbox(label="
|
| 104 |
-
outputs=gr.Textbox(label="
|
| 105 |
title="🧠 Text-to-SQL Agent",
|
| 106 |
description="Ask natural-language questions about a restaurant receipts database. Powered by smolagents + Meta-Llama-3.",
|
| 107 |
)
|
|
|
|
| 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,
|
| 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) -> str:
|
| 56 |
"""
|
| 57 |
Executes SQL queries on the available tables: receipts and waiters.
|
| 58 |
|
| 59 |
Args:
|
| 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
|
|
|
|
| 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"
|
| 78 |
inspector = inspect(engine)
|
| 79 |
for table in ["receipts", "waiters"]:
|
|
|
|
| 84 |
updated_description += table_description
|
| 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,
|
| 105 |
+
inputs=gr.Textbox(label="Ask a question (e.g., Who got the biggest tip?)"),
|
| 106 |
+
outputs=gr.Textbox(label="Agent response"),
|
| 107 |
title="🧠 Text-to-SQL Agent",
|
| 108 |
description="Ask natural-language questions about a restaurant receipts database. Powered by smolagents + Meta-Llama-3.",
|
| 109 |
)
|