Spaces:
Sleeping
Sleeping
Add sqlite tool
Browse files
app.py
CHANGED
|
@@ -7,32 +7,72 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
Args:
|
| 25 |
-
|
|
|
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
-
except Exception as e:
|
| 34 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
-
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
@@ -55,7 +95,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
import sqlite3
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Connect to SQLite database. It will be created if it doesn't exist
|
| 15 |
+
conn = sqlite3.connect('employee_database.db')
|
| 16 |
+
cursor = conn.cursor()
|
| 17 |
+
|
| 18 |
+
# Create employees table with basic columns
|
| 19 |
+
cursor.execute('''
|
| 20 |
+
CREATE TABLE IF NOT EXISTS employees (
|
| 21 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 22 |
+
first_name TEXT NOT NULL,
|
| 23 |
+
last_name TEXT NOT NULL,
|
| 24 |
+
job_title TEXT NOT NULL
|
| 25 |
+
)
|
| 26 |
+
''')
|
| 27 |
+
|
| 28 |
+
# Commit the transaction
|
| 29 |
+
conn.commit()
|
| 30 |
+
|
| 31 |
+
# Sample data for employees
|
| 32 |
+
employees_data = [
|
| 33 |
+
('John', 'Smith', 'Software Engineer'),
|
| 34 |
+
('Sarah', 'Johnson', 'Marketing Manager'),
|
| 35 |
+
('Michael', 'Brown', 'Data Scientist'),
|
| 36 |
+
('Emma', 'Wilson', 'Product Designer'),
|
| 37 |
+
('William', 'Davis', 'DevOps Engineer'),
|
| 38 |
+
('Olivia', 'Miller', 'UX Researcher'),
|
| 39 |
+
('James', 'Wilson', 'Backend Developer'),
|
| 40 |
+
('Jessica', 'Moore', 'Project Manager'),
|
| 41 |
+
('Robert', 'Taylor', 'Frontend Developer'),
|
| 42 |
+
('Lisa', 'Anderson', 'Quality Assurance'),
|
| 43 |
+
('Richard', 'Thomas', 'Technical Writer'),
|
| 44 |
+
('Amy', 'Jackson', 'Business Analyst'),
|
| 45 |
+
('Charles', 'White', 'Database Administrator'),
|
| 46 |
+
('Elizabeth', 'Harris', 'Customer Support'),
|
| 47 |
+
('Thomas', 'Martin', 'Network Engineer'),
|
| 48 |
+
('Jennifer', 'Thompson', 'HR Specialist'),
|
| 49 |
+
('Donald', 'Walker', 'Security Specialist'),
|
| 50 |
+
('Margaret', 'Young', 'Content Manager'),
|
| 51 |
+
('Ronald', 'Allen', 'Sales Representative'),
|
| 52 |
+
('Susan', 'Scott', 'Financial Analyst')
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
# Insert multiple records efficiently using executemany
|
| 56 |
+
cursor.executemany('''
|
| 57 |
+
INSERT INTO employees (first_name, last_name, job_title)
|
| 58 |
+
VALUES (?, ?, ?)
|
| 59 |
+
''', employees_data)
|
| 60 |
|
| 61 |
+
# Commit the transaction
|
| 62 |
+
conn.commit()
|
| 63 |
+
|
| 64 |
+
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 65 |
@tool
|
| 66 |
+
def get_employee_job_title(first_name:str, last_name:str)-> str: #it's import to specify the return type
|
| 67 |
+
conn = sqlite3.connect('employee_database.db')
|
| 68 |
+
cursor = conn.cursor()
|
| 69 |
+
"""A tool that allows to do a sqlite sql query based on first_name and last_name
|
| 70 |
Args:
|
| 71 |
+
first_name: the first argument
|
| 72 |
+
last_name: the second argument
|
| 73 |
"""
|
| 74 |
+
return cursor.execute(f"SELECT job_title FROM employees WHERE first_name='{first_name}' AND last_name='{last_name}'")
|
| 75 |
+
rows = cursor.fetchall()[0][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
final_answer = FinalAnswerTool()
|
| 78 |
|
|
|
|
| 95 |
|
| 96 |
agent = CodeAgent(
|
| 97 |
model=model,
|
| 98 |
+
tools=[final_answer, get_employee_job_title], ## add your tools here (don't remove final answer)
|
| 99 |
max_steps=6,
|
| 100 |
verbosity_level=1,
|
| 101 |
grammar=None,
|