Spaces:
Running
Running
instantiate db
Browse files
app.py
CHANGED
|
@@ -1,52 +1,16 @@
|
|
| 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:
|
|
|
|
| 1 |
import os
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import gradio as gr
|
| 4 |
+
from sqlalchemy import text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
from smolagents import tool, CodeAgent, HfApiModel
|
| 6 |
|
| 7 |
+
# Import the database setup from database.py
|
| 8 |
+
from database import engine, receipts
|
| 9 |
+
|
| 10 |
# Load Hugging Face token from environment variables
|
| 11 |
load_dotenv(override=True)
|
| 12 |
hf_token = os.getenv("HF_TOKEN")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# SQL Execution function
|
| 15 |
@tool
|
| 16 |
def sql_engine(query: str) -> str:
|