Csplk commited on
Commit
bbb7a01
β€’
1 Parent(s): 104da00

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -0
app.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from markdownify import markdownify as md
4
+ from requests.exceptions import RequestException
5
+ import re
6
+ from transformers.agents import (
7
+ ReactCodeAgent,
8
+ ReactJsonAgent,
9
+ HfApiEngine,
10
+ ManagedAgent,
11
+ stream_to_gradio,
12
+ Tool,
13
+ )
14
+ from transformers.agents.search import DuckDuckGoSearchTool
15
+ import gradio as gr
16
+ import datetime
17
+ from huggingface_hub import login
18
+
19
+ # Log in to Hugging Face
20
+ hf_token = os.getenv("hf_token")
21
+ login(hf_token)
22
+ model = "meta-llama/Meta-Llama-3.1-70B-Instruct"
23
+
24
+ # Define the VisitWebpageTool
25
+ class VisitWebpageTool(Tool):
26
+ name = "visit_webpage"
27
+ description = "Visits a webpage at the given url and returns its content as a markdown string."
28
+ inputs = {
29
+ "url": {
30
+ "type": "text",
31
+ "description": "The url of the webpage to visit.",
32
+ }
33
+ }
34
+ output_type = "text"
35
+
36
+ def forward(self, url: str) -> str:
37
+ try:
38
+ response = requests.get(url)
39
+ response.raise_for_status()
40
+ markdown_content = md(response.text).strip()
41
+ markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
42
+ return markdown_content
43
+ except RequestException as e:
44
+ return f"Error fetching the webpage: {str(e)}"
45
+ except Exception as e:
46
+ return f"An unexpected error occurred: {str(e)}"
47
+
48
+ visit_page_tool = VisitWebpageTool()
49
+
50
+ # Set up the multi-agent system
51
+ llm_engine = HfApiEngine(model)
52
+
53
+ # Agent to clarify specifics
54
+ clarify_agent = ReactJsonAgent(
55
+ tools=[],
56
+ llm_engine=llm_engine,
57
+ max_iterations=20,
58
+ )
59
+
60
+ # Agent to plan the project
61
+ plan_agent = ReactJsonAgent(
62
+ tools=[],
63
+ llm_engine=llm_engine,
64
+ max_iterations=20,
65
+ )
66
+
67
+ # Agent to break the plan into tasks
68
+ task_agent = ReactJsonAgent(
69
+ tools=[],
70
+ llm_engine=llm_engine,
71
+ max_iterations=20,
72
+ )
73
+
74
+ # Agent to execute tasks
75
+ execute_agent = ReactCodeAgent(
76
+ tools=[],
77
+ llm_engine=llm_engine,
78
+ max_iterations=20,
79
+ additional_authorized_imports=['requests', 'bs4']
80
+ )
81
+
82
+ # Agent to review code
83
+ review_agent = ReactCodeAgent(
84
+ tools=[],
85
+ llm_engine=llm_engine,
86
+ max_iterations=20,
87
+ additional_authorized_imports=['requests', 'bs4', 'transformers']
88
+ )
89
+
90
+ # Agent to revise code
91
+ revise_agent = ReactCodeAgent(
92
+ tools=[],
93
+ llm_engine=llm_engine,
94
+ max_iterations=20,
95
+ )
96
+
97
+ # Managed agents
98
+ managed_clarify_agent = ManagedAgent(
99
+ agent=clarify_agent,
100
+ name="clarify_agent",
101
+ description="Clarifies project specifics if not given in the project description.",
102
+ )
103
+
104
+ managed_plan_agent = ManagedAgent(
105
+ agent=plan_agent,
106
+ name="plan_agent",
107
+ description="Plans the project.",
108
+ )
109
+
110
+ managed_task_agent = ManagedAgent(
111
+ agent=task_agent,
112
+ name="task_agent",
113
+ description="Breaks the plan into tasks.",
114
+ )
115
+
116
+ managed_execute_agent = ManagedAgent(
117
+ agent=execute_agent,
118
+ name="execute_agent",
119
+ description="Executes the tasks.",
120
+ )
121
+
122
+ managed_review_agent = ManagedAgent(
123
+ agent=review_agent,
124
+ name="review_agent",
125
+ description="Reviews the code written in completion of tasks.",
126
+ )
127
+
128
+ managed_revise_agent = ManagedAgent(
129
+ agent=revise_agent,
130
+ name="revise_agent",
131
+ description="Revises the code if needed.",
132
+ )
133
+
134
+ # Manager agent
135
+ manager_agent = ReactCodeAgent(
136
+ tools=[],
137
+ llm_engine=llm_engine,
138
+ managed_agents=[
139
+ managed_clarify_agent,
140
+ managed_plan_agent,
141
+ managed_task_agent,
142
+ managed_execute_agent,
143
+ managed_review_agent,
144
+ managed_revise_agent,
145
+ ],
146
+ additional_authorized_imports=["time", "datetime"],
147
+ )
148
+
149
+ # Implement the Gradio interface
150
+ def interact_with_agent(task):
151
+ messages = []
152
+ messages.append(gr.ChatMessage(role="user", content=task))
153
+ yield messages
154
+ try:
155
+ for msg in stream_to_gradio(manager_agent, task):
156
+ messages.append(msg)
157
+ yield messages + [
158
+ gr.ChatMessage(role="assistant", content="⏳ Task not finished yet!")
159
+ ]
160
+ except Exception as e:
161
+ messages.append(gr.ChatMessage(role="assistant", content=f"Error: {str(e)}"))
162
+ yield messages
163
+
164
+ with gr.Blocks() as demo:
165
+ gr.Markdown("# Multi-agent Software Team")
166
+ gr.Markdown("Gradio space based on the multiagent_web_assistant cookbook https://huggingface.co/learn/cookbook/multiagent_web_assistant")
167
+ text_input = gr.Textbox(lines=1, label="Project Description", value="Create a simple web app that allows users to upload images and apply filters.")
168
+ submit = gr.Button("Start Project Development")
169
+ chatbot = gr.Chatbot(
170
+ label="Agent",
171
+ type="messages",
172
+ avatar_images=(
173
+ None,
174
+ "https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png",
175
+ ),
176
+ )
177
+ submit.click(interact_with_agent, [text_input], [chatbot])
178
+
179
+ if __name__ == "__main__":
180
+ demo.launch(share=True)