Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
4 |
-
from transformers.agents import (
|
5 |
ReactCodeAgent,
|
6 |
ReactJsonAgent,
|
7 |
HfApiEngine,
|
@@ -9,66 +7,41 @@ from transformers.agents import (
|
|
9 |
stream_to_gradio,
|
10 |
)
|
11 |
from transformers.agents.search import DuckDuckGoSearchTool
|
12 |
-
import
|
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 |
-
#
|
20 |
-
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
|
|
24 |
|
25 |
-
|
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 |
-
|
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 |
-
#
|
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 |
-
#
|
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 |
-
#
|
72 |
manager_agent = ReactCodeAgent(
|
73 |
tools=[],
|
74 |
llm_engine=llm_engine,
|
@@ -76,11 +49,21 @@ manager_agent = ReactCodeAgent(
|
|
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
|
@@ -96,7 +79,8 @@ with gr.Blocks() as demo:
|
|
96 |
gr.Markdown("# multi-agent-web-browser")
|
97 |
gr.Markdown("Gradio space based on the multiagent_web_assistant cookbook https://huggingface.co/learn/cookbook/multiagent_web_assistant")
|
98 |
text_input = gr.Textbox(lines=1, label="Chat Message", value="How many years ago was Stripe founded?")
|
99 |
-
|
|
|
100 |
chatbot = gr.Chatbot(
|
101 |
label="Agent",
|
102 |
type="messages",
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import (
|
|
|
3 |
ReactCodeAgent,
|
4 |
ReactJsonAgent,
|
5 |
HfApiEngine,
|
|
|
7 |
stream_to_gradio,
|
8 |
)
|
9 |
from transformers.agents.search import DuckDuckGoSearchTool
|
10 |
+
from visit_webpage_tool import VisitWebpageTool # Import the VisitWebpageTool class
|
|
|
|
|
|
|
|
|
11 |
from huggingface_hub import login
|
12 |
+
import os
|
13 |
+
import time
|
14 |
+
import random
|
15 |
|
16 |
+
# Define the model
|
17 |
+
model = "meta-llama/Meta-Llama-3.1-70B-Instruct"
|
18 |
|
19 |
+
# Initialize the LLM engine with the Hugging Face token
|
20 |
+
hf_token = os.getenv("HF_TOKEN")
|
21 |
+
if not hf_token:
|
22 |
+
raise ValueError("Hugging Face API token not found. Please set the hf_token environment variable.")
|
23 |
|
24 |
+
# Authenticate with Hugging Face
|
25 |
+
login(hf_token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Initialize the LLM engine with reduced max_new_tokens
|
28 |
+
llm_engine = HfApiEngine(model)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
# Create the web agent
|
|
|
|
|
|
|
31 |
web_agent = ReactJsonAgent(
|
32 |
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
33 |
llm_engine=llm_engine,
|
34 |
max_iterations=10,
|
35 |
)
|
36 |
|
37 |
+
# Wrap the web agent into a ManagedAgent
|
38 |
managed_web_agent = ManagedAgent(
|
39 |
agent=web_agent,
|
40 |
name="search_agent",
|
41 |
description="Runs web searches for you. Give it your query as an argument.",
|
42 |
)
|
43 |
|
44 |
+
# Create the manager agent
|
45 |
manager_agent = ReactCodeAgent(
|
46 |
tools=[],
|
47 |
llm_engine=llm_engine,
|
|
|
49 |
additional_authorized_imports=["time", "datetime"],
|
50 |
)
|
51 |
|
52 |
+
# Define a retry mechanism with exponential backoff
|
53 |
+
def retry_with_backoff(func, retries=5, backoff_factor=2):
|
54 |
+
for attempt in range(retries):
|
55 |
+
try:
|
56 |
+
return func()
|
57 |
+
except Exception as e:
|
58 |
+
if attempt < retries - 1:
|
59 |
+
sleep_time = backoff_factor ** attempt + random.uniform(0, 1)
|
60 |
+
time.sleep(sleep_time)
|
61 |
+
else:
|
62 |
+
raise e
|
63 |
+
|
64 |
+
# Define the Gradio interaction function
|
65 |
@spaces.GPU(duration=120)
|
66 |
def interact_with_agent(task):
|
|
|
|
|
|
|
67 |
messages = []
|
68 |
messages.append(gr.ChatMessage(role="user", content=task))
|
69 |
yield messages
|
|
|
79 |
gr.Markdown("# multi-agent-web-browser")
|
80 |
gr.Markdown("Gradio space based on the multiagent_web_assistant cookbook https://huggingface.co/learn/cookbook/multiagent_web_assistant")
|
81 |
text_input = gr.Textbox(lines=1, label="Chat Message", value="How many years ago was Stripe founded?")
|
82 |
+
text_input = gr.Textbox(lines=1, label="Chat Message", value="How many years ago was Stripe founded?")
|
83 |
+
submit = gr.Button("Run web search agent!")
|
84 |
chatbot = gr.Chatbot(
|
85 |
label="Agent",
|
86 |
type="messages",
|