Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 11 |
-
|
| 12 |
-
"
|
| 13 |
metadata_obj,
|
| 14 |
-
Column("
|
| 15 |
-
Column("
|
| 16 |
-
Column("price", Float),
|
| 17 |
-
Column("tip", Float),
|
| 18 |
)
|
| 19 |
metadata_obj.create_all(engine)
|
| 20 |
|
| 21 |
-
# Insert sample data
|
| 22 |
-
|
| 23 |
-
{"
|
| 24 |
-
{"
|
| 25 |
-
{"
|
| 26 |
-
{"
|
| 27 |
]
|
| 28 |
-
for row in
|
| 29 |
-
stmt = insert(
|
| 30 |
with engine.begin() as conn:
|
| 31 |
conn.execute(stmt)
|
| 32 |
|
| 33 |
-
# --- Create
|
| 34 |
-
|
| 35 |
-
"
|
| 36 |
metadata_obj,
|
| 37 |
-
Column("
|
| 38 |
-
Column("
|
|
|
|
| 39 |
)
|
| 40 |
metadata_obj.create_all(engine)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
{"
|
| 45 |
-
{"
|
| 46 |
-
{"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
]
|
| 48 |
-
for row in
|
| 49 |
-
stmt = insert(
|
| 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 ["
|
| 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
|
| 111 |
outputs=gr.Textbox(label="Agent response"),
|
| 112 |
title="🧠 Text-to-SQL Agent",
|
| 113 |
-
description="Ask natural-language questions about
|
| 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()
|