Spaces:
Running
Running
matterattetatte
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,83 @@
|
|
1 |
import streamlit as st
|
2 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel,
|
3 |
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
)
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
)
|
15 |
|
16 |
manager_agent = CodeAgent(
|
@@ -30,14 +97,14 @@ def log_agent_action(prompt, result, agent_name):
|
|
30 |
st.code(result, language="text")
|
31 |
|
32 |
# Streamlit app title
|
33 |
-
st.title("AI
|
34 |
|
35 |
# App description
|
36 |
-
st.write("Generate
|
37 |
|
38 |
# Input blog topic or prompt
|
39 |
|
40 |
-
prompt = st.text_area("Enter the your
|
41 |
|
42 |
# Button to generate blog content
|
43 |
if st.button("Generate Summary"):
|
@@ -51,7 +118,7 @@ if st.button("Generate Summary"):
|
|
51 |
st.write(result)
|
52 |
|
53 |
# Log backend activity
|
54 |
-
log_agent_action(prompt, result, "
|
55 |
except Exception as e:
|
56 |
st.error(f"An error occurred: {e}")
|
57 |
else:
|
@@ -59,4 +126,4 @@ if st.button("Generate Summary"):
|
|
59 |
|
60 |
# Footer
|
61 |
st.markdown("---")
|
62 |
-
st.caption("Powered by SmolAgents,
|
|
|
1 |
import streamlit as st
|
2 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, ManagedAgent, VisitWebpageTool, tool
|
3 |
|
4 |
|
5 |
+
from sqlalchemy import (
|
6 |
+
create_engine,
|
7 |
+
MetaData,
|
8 |
+
Table,
|
9 |
+
Column,
|
10 |
+
String,
|
11 |
+
Integer,
|
12 |
+
Float,
|
13 |
+
insert,
|
14 |
+
inspect,
|
15 |
+
text,
|
16 |
)
|
17 |
|
18 |
+
engine = create_engine("sqlite:///:memory:")
|
19 |
+
metadata_obj = MetaData()
|
20 |
+
|
21 |
+
# create city SQL table
|
22 |
+
table_name = "receipts"
|
23 |
+
receipts = Table(
|
24 |
+
table_name,
|
25 |
+
metadata_obj,
|
26 |
+
Column("receipt_id", Integer, primary_key=True),
|
27 |
+
Column("customer_name", String(16), primary_key=True),
|
28 |
+
Column("price", Float),
|
29 |
+
Column("tip", Float),
|
30 |
+
)
|
31 |
+
metadata_obj.create_all(engine)
|
32 |
+
|
33 |
+
rows = [
|
34 |
+
{"receipt_id": 1, "customer_name": "Alan Payne", "price": 12.06, "tip": 1.20},
|
35 |
+
{"receipt_id": 2, "customer_name": "Alex Mason", "price": 23.86, "tip": 0.24},
|
36 |
+
{"receipt_id": 3, "customer_name": "Woodrow Wilson", "price": 53.43, "tip": 5.43},
|
37 |
+
{"receipt_id": 4, "customer_name": "Margaret James", "price": 21.11, "tip": 1.00},
|
38 |
+
]
|
39 |
+
for row in rows:
|
40 |
+
stmt = insert(receipts).values(**row)
|
41 |
+
with engine.begin() as connection:
|
42 |
+
cursor = connection.execute(stmt)
|
43 |
+
|
44 |
+
inspector = inspect(engine)
|
45 |
+
columns_info = [(col["name"], col["type"]) for col in inspector.get_columns("receipts")]
|
46 |
+
|
47 |
+
table_description = "Columns:\n" + "\n".join([f" - {name}: {col_type}" for name, col_type in columns_info])
|
48 |
+
print(table_description)
|
49 |
+
|
50 |
+
@tool
|
51 |
+
def sql_engine(query: str) -> str:
|
52 |
+
"""
|
53 |
+
Allows you to perform SQL queries on the table. Returns a string representation of the result.
|
54 |
+
The table is named 'receipts'. Its description is as follows:
|
55 |
+
Columns:
|
56 |
+
- receipt_id: INTEGER
|
57 |
+
- customer_name: VARCHAR(16)
|
58 |
+
- price: FLOAT
|
59 |
+
- tip: FLOAT
|
60 |
+
|
61 |
+
Args:
|
62 |
+
query: The query to perform. This should be correct SQL.
|
63 |
+
"""
|
64 |
+
output = ""
|
65 |
+
with engine.connect() as con:
|
66 |
+
rows = con.execute(text(query))
|
67 |
+
for row in rows:
|
68 |
+
output += "\n" + str(row)
|
69 |
+
return output
|
70 |
+
|
71 |
+
|
72 |
+
sql_agent = CodeAgent(
|
73 |
+
tools=[sql_engine],
|
74 |
+
model=HfApiModel("meta-llama/Meta-Llama-3.1-8B-Instruct"),
|
75 |
+
)
|
76 |
+
|
77 |
+
managed_sql_agent = ManagedAgent(
|
78 |
+
agent=sql_agent,
|
79 |
+
name="sql search",
|
80 |
+
description="Runs SQL queries. Give it your query as an argument. Also, this agent should provide the actual query it ran.",
|
81 |
)
|
82 |
|
83 |
manager_agent = CodeAgent(
|
|
|
97 |
st.code(result, language="text")
|
98 |
|
99 |
# Streamlit app title
|
100 |
+
st.title("AI SQL Assistant Agent researching your query and summarizing it")
|
101 |
|
102 |
# App description
|
103 |
+
st.write("Generate SQL queries using human speech powered by SmolAgents.")
|
104 |
|
105 |
# Input blog topic or prompt
|
106 |
|
107 |
+
prompt = st.text_area("Enter the your prompt:", placeholder="E.g., Can you give me the name of the client who got the most expensive receipt?")
|
108 |
|
109 |
# Button to generate blog content
|
110 |
if st.button("Generate Summary"):
|
|
|
118 |
st.write(result)
|
119 |
|
120 |
# Log backend activity
|
121 |
+
log_agent_action(prompt, result, "SQL Agent")
|
122 |
except Exception as e:
|
123 |
st.error(f"An error occurred: {e}")
|
124 |
else:
|
|
|
126 |
|
127 |
# Footer
|
128 |
st.markdown("---")
|
129 |
+
st.caption("Powered by SmolAgents, and Streamlit")
|