Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import Tool
|
4 |
+
from transformers.agents import (
|
5 |
+
ReactCodeAgent,
|
6 |
+
ReactJsonAgent,
|
7 |
+
HfApiEngine,
|
8 |
+
ManagedAgent,
|
9 |
+
stream_to_gradio,
|
10 |
+
)
|
11 |
+
from transformers.agents.search import DuckDuckGoSearchTool
|
12 |
+
import requests
|
13 |
+
from markdownify import markdownify as md
|
14 |
+
from requests.exceptions import RequestException
|
15 |
+
import re
|
16 |
+
import spaces
|
17 |
+
from huggingface_hub import login
|
18 |
+
|
19 |
+
# Read the Hugging Face API token from the environment variable
|
20 |
+
hf_token = os.getenv("HF_TOKEN")
|
21 |
+
|
22 |
+
# Authenticate with the Hugging Face API
|
23 |
+
login(token=hf_token)
|
24 |
+
|
25 |
+
class VisitWebpageTool(Tool):
|
26 |
+
"""
|
27 |
+
A tool to visit a webpage and return its content as a markdown string.
|
28 |
+
"""
|
29 |
+
name = "visit_webpage"
|
30 |
+
description = "Visits a webpage at the given URL and returns its content as a markdown string."
|
31 |
+
inputs = {
|
32 |
+
"url": {
|
33 |
+
"type": "text",
|
34 |
+
"description": "The URL of the webpage to visit.",
|
35 |
+
}
|
36 |
+
}
|
37 |
+
output_type = "text"
|
38 |
+
|
39 |
+
def forward(self, url: str) -> str:
|
40 |
+
"""
|
41 |
+
Fetch the webpage content and convert it to markdown.
|
42 |
+
"""
|
43 |
+
try:
|
44 |
+
response = requests.get(url)
|
45 |
+
response.raise_for_status()
|
46 |
+
markdown_content = md(response.text).strip()
|
47 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
48 |
+
return markdown_content
|
49 |
+
except RequestException as e:
|
50 |
+
return f"Error fetching the webpage: {str(e)}"
|
51 |
+
except Exception as e:
|
52 |
+
return f"An unexpected error occurred: {str(e)}"
|
53 |
+
|
54 |
+
# Initialize the LLM engine with the Hugging Face API token
|
55 |
+
llm_engine = HfApiEngine(model="meta-llama/Meta-Llama-3.1-70B-Instruct")
|
56 |
+
|
57 |
+
# Initialize the web agent with necessary tools and engine
|
58 |
+
web_agent = ReactJsonAgent(
|
59 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
60 |
+
llm_engine=llm_engine,
|
61 |
+
max_iterations=10,
|
62 |
+
)
|
63 |
+
|
64 |
+
# Create a managed web agent
|
65 |
+
managed_web_agent = ManagedAgent(
|
66 |
+
agent=web_agent,
|
67 |
+
name="search_agent",
|
68 |
+
description="Runs web searches for you. Give it your query as an argument.",
|
69 |
+
)
|
70 |
+
|
71 |
+
# Initialize the manager agent with the managed web agent
|
72 |
+
manager_agent = ReactCodeAgent(
|
73 |
+
tools=[],
|
74 |
+
llm_engine=llm_engine,
|
75 |
+
managed_agents=[managed_web_agent],
|
76 |
+
additional_authorized_imports=["time", "datetime"],
|
77 |
+
)
|
78 |
+
|
79 |
+
@spaces.GPU(duration=120)
|
80 |
+
def interact_with_agent(task):
|
81 |
+
"""
|
82 |
+
Interact with the agent and stream the responses to Gradio.
|
83 |
+
"""
|
84 |
+
messages = []
|
85 |
+
messages.append(gr.ChatMessage(role="user", content=task))
|
86 |
+
yield messages
|
87 |
+
for msg in stream_to_gradio(manager_agent, task):
|
88 |
+
messages.append(msg)
|
89 |
+
yield messages + [
|
90 |
+
gr.ChatMessage(role="assistant", content="β³ Task not finished yet!")
|
91 |
+
]
|
92 |
+
yield messages
|
93 |
+
|
94 |
+
# Create the Gradio interface
|
95 |
+
with gr.Blocks() as demo:
|
96 |
+
text_input = gr.Textbox(lines=1, label="Chat Message", value="How many years ago was Stripe founded?")
|
97 |
+
submit = gr.Button("Run multi-agent system!")
|
98 |
+
chatbot = gr.Chatbot(
|
99 |
+
label="Agent",
|
100 |
+
type="messages",
|
101 |
+
avatar_images=(
|
102 |
+
None,
|
103 |
+
"https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
|
104 |
+
),
|
105 |
+
)
|
106 |
+
submit.click(interact_with_agent, [text_input], [chatbot])
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
demo.launch()
|