Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files
agent.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tools import visit_webpage, image_generation_tool, image_diplay_tool
|
2 |
+
from smolagents import (
|
3 |
+
CodeAgent,
|
4 |
+
ToolCallingAgent,
|
5 |
+
HfApiModel,
|
6 |
+
ManagedAgent,
|
7 |
+
DuckDuckGoSearchTool
|
8 |
+
)
|
9 |
+
|
10 |
+
def multi_agent_framework(model_id):
|
11 |
+
model = HfApiModel(model_id)
|
12 |
+
|
13 |
+
web_agent = ToolCallingAgent(
|
14 |
+
tools=[DuckDuckGoSearchTool(), visit_webpage],
|
15 |
+
model=model,
|
16 |
+
max_steps=5,
|
17 |
+
)
|
18 |
+
|
19 |
+
managed_web_agent = ManagedAgent(
|
20 |
+
agent=web_agent,
|
21 |
+
name="search",
|
22 |
+
description="Runs web searches for you. Give it your query as an argument.",
|
23 |
+
)
|
24 |
+
|
25 |
+
manager_agent = CodeAgent(
|
26 |
+
tools=[image_generation_tool, image_diplay_tool],
|
27 |
+
model=model,
|
28 |
+
managed_agents=[managed_web_agent],
|
29 |
+
additional_authorized_imports=["time", "numpy", "pandas", "requests"],
|
30 |
+
)
|
31 |
+
|
32 |
+
return manager_agent
|
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from agent import multi_agent_framework
|
3 |
+
|
4 |
+
|
5 |
+
# Define the multi agent framework
|
6 |
+
model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
7 |
+
agent = multi_agent_framework(model_id)
|
8 |
+
|
9 |
+
# Function to log agent actions
|
10 |
+
def log_agent_action(prompt, result):
|
11 |
+
st.write(f"### Agent Activity")
|
12 |
+
st.write("**Prompt Sent to Agent:**")
|
13 |
+
st.code(prompt, language="text")
|
14 |
+
st.write("**Agent Output:**")
|
15 |
+
st.code(result, language="text")
|
16 |
+
|
17 |
+
# Streamlit app title
|
18 |
+
st.title("Multi Agent GPT")
|
19 |
+
|
20 |
+
# App description
|
21 |
+
st.write("Generate creative content, search the web and generate images enriched with the power of MultiAgent framework")
|
22 |
+
|
23 |
+
# Input blog topic or prompt
|
24 |
+
user_prompt = st.text_area("How may I help you?:", placeholder="E.g., Generate me a picture of cute puppy")
|
25 |
+
|
26 |
+
# Button to generate content
|
27 |
+
if st.button("Generate"):
|
28 |
+
if user_prompt:
|
29 |
+
with st.spinner("Generating content with our Multi agents"):
|
30 |
+
try:
|
31 |
+
# Run the agent with the given prompt
|
32 |
+
result = agent.run(user_prompt)
|
33 |
+
# Display the generated content
|
34 |
+
st.subheader("Generated Content:")
|
35 |
+
st.write(result)
|
36 |
+
|
37 |
+
# Log backend activity
|
38 |
+
log_agent_action(user_prompt, result)
|
39 |
+
except Exception as e:
|
40 |
+
st.error(f"An error occurred: {e}")
|
41 |
+
else:
|
42 |
+
st.warning("Please enter a prompt to proceed.")
|
43 |
+
|
44 |
+
# Footer
|
45 |
+
st.markdown("---")
|
46 |
+
st.caption("Powered by SmolAgents, DuckDuckGo, black-forest-labs and Streamlit")
|
tools.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import requests
|
3 |
+
from PIL import Image
|
4 |
+
from IPython.display import display
|
5 |
+
from markdownify import markdownify
|
6 |
+
from requests.exceptions import RequestException
|
7 |
+
from smolagents import (
|
8 |
+
Tool,
|
9 |
+
tool
|
10 |
+
)
|
11 |
+
|
12 |
+
@tool
|
13 |
+
def visit_webpage(url: str) -> str:
|
14 |
+
"""Visits a webpage at the given URL and returns its content as a markdown string.
|
15 |
+
|
16 |
+
Args:
|
17 |
+
url: The URL of the webpage to visit.
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
The content of the webpage converted to Markdown, or an error message if the request fails.
|
21 |
+
"""
|
22 |
+
try:
|
23 |
+
# Send a GET request to the URL
|
24 |
+
response = requests.get(url)
|
25 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
26 |
+
|
27 |
+
# Convert the HTML content to Markdown
|
28 |
+
markdown_content = markdownify(response.text).strip()
|
29 |
+
|
30 |
+
# Remove multiple line breaks
|
31 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
32 |
+
|
33 |
+
return markdown_content
|
34 |
+
|
35 |
+
except RequestException as e:
|
36 |
+
return f"Error fetching the webpage: {str(e)}"
|
37 |
+
except Exception as e:
|
38 |
+
return f"An unexpected error occurred: {str(e)}"
|
39 |
+
|
40 |
+
@tool
|
41 |
+
def image_diplay_tool(image_path : str) -> object:
|
42 |
+
"""
|
43 |
+
This is a tool that returns the image object and displays it
|
44 |
+
|
45 |
+
Args:
|
46 |
+
image_path: The images's path for displaying.
|
47 |
+
"""
|
48 |
+
try:
|
49 |
+
# Open the .webp image using Pillow
|
50 |
+
img = Image.open(image_path)
|
51 |
+
# Display the image
|
52 |
+
display(img)
|
53 |
+
except Exception as e:
|
54 |
+
print(f"Error displaying image: {e}")
|
55 |
+
return img
|
56 |
+
|
57 |
+
image_generation_tool = Tool.from_space(
|
58 |
+
"black-forest-labs/FLUX.1-schnell",
|
59 |
+
name="image_generator",
|
60 |
+
description="Generate an image from a prompt and return its path. Make sure to improve the prompt. Another tool MUST be called for displaying image"
|
61 |
+
)
|