initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
import gradio as gr
|
4 |
+
from sqlalchemy import (
|
5 |
+
create_engine,
|
6 |
+
MetaData,
|
7 |
+
Table,
|
8 |
+
Column,
|
9 |
+
String,
|
10 |
+
Integer,
|
11 |
+
Float,
|
12 |
+
insert,
|
13 |
+
text,
|
14 |
+
)
|
15 |
+
from smolagents import tool, CodeAgent, HfApiModel
|
16 |
+
|
17 |
+
# Load Hugging Face token from environment variables
|
18 |
+
load_dotenv(override=True)
|
19 |
+
hf_token = os.getenv("HF_TOKEN")
|
20 |
+
|
21 |
+
# Initialize in-memory SQLite database
|
22 |
+
engine = create_engine("sqlite:///:memory:")
|
23 |
+
metadata_obj = MetaData()
|
24 |
+
|
25 |
+
# Create 'receipts' table
|
26 |
+
receipts = Table(
|
27 |
+
"receipts",
|
28 |
+
metadata_obj,
|
29 |
+
Column("receipt_id", Integer, primary_key=True),
|
30 |
+
Column("customer_name", String(16), primary_key=True),
|
31 |
+
Column("price", Float),
|
32 |
+
Column("tip", Float),
|
33 |
+
)
|
34 |
+
metadata_obj.create_all(engine)
|
35 |
+
|
36 |
+
# Function to insert data
|
37 |
+
def insert_rows_into_table(rows, table):
|
38 |
+
with engine.begin() as connection:
|
39 |
+
connection.execute(insert(table), rows)
|
40 |
+
|
41 |
+
# Insert sample data
|
42 |
+
rows = [
|
43 |
+
{"receipt_id": 1, "customer_name": "Alan Payne", "price": 12.06, "tip": 1.20},
|
44 |
+
{"receipt_id": 2, "customer_name": "Alex Mason", "price": 23.86, "tip": 0.24},
|
45 |
+
{"receipt_id": 3, "customer_name": "Woodrow Wilson", "price": 53.43, "tip": 5.43},
|
46 |
+
{"receipt_id": 4, "customer_name": "Margaret James", "price": 21.11, "tip": 1.00},
|
47 |
+
]
|
48 |
+
insert_rows_into_table(rows, receipts)
|
49 |
+
|
50 |
+
# SQL Execution function
|
51 |
+
@tool
|
52 |
+
def sql_engine(query: str) -> str:
|
53 |
+
"""
|
54 |
+
Allows you to perform SQL queries on the table. Returns a string representation of the result.
|
55 |
+
The table is named 'receipts'. Its description is as follows:
|
56 |
+
Columns:
|
57 |
+
- receipt_id: INTEGER
|
58 |
+
- customer_name: VARCHAR(16)
|
59 |
+
- price: FLOAT
|
60 |
+
- tip: FLOAT
|
61 |
+
|
62 |
+
Args:
|
63 |
+
query: The query to perform. This should be correct SQL.
|
64 |
+
"""
|
65 |
+
output = ""
|
66 |
+
try:
|
67 |
+
with engine.connect() as con:
|
68 |
+
rows = con.execute(text(query))
|
69 |
+
for row in rows:
|
70 |
+
output += "\n" + str(row)
|
71 |
+
except Exception as e:
|
72 |
+
output = f"Error: {str(e)}"
|
73 |
+
return output.strip()
|
74 |
+
|
75 |
+
# Set up the Hugging Face agent
|
76 |
+
agent = CodeAgent(
|
77 |
+
tools=[sql_engine],
|
78 |
+
model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct", token=hf_token),
|
79 |
+
)
|
80 |
+
|
81 |
+
# Gradio function
|
82 |
+
def query_sql(user_query):
|
83 |
+
return sql_engine(user_query)
|
84 |
+
|
85 |
+
# Define Gradio interface
|
86 |
+
iface = gr.Interface(
|
87 |
+
fn=query_sql,
|
88 |
+
inputs=gr.Textbox(label="Enter your SQL Query"),
|
89 |
+
outputs=gr.Textbox(label="Query Result"),
|
90 |
+
title="SQL Query Executor",
|
91 |
+
description="Enter SQL queries to interact with an in-memory SQLite database.",
|
92 |
+
allow_flagging="never",
|
93 |
+
)
|
94 |
+
|
95 |
+
# Launch the app
|
96 |
+
if __name__ == "__main__":
|
97 |
+
iface.launch()
|