Spaces:
Sleeping
Sleeping
mriusero
commited on
Commit
·
416c5d2
1
Parent(s):
7ebe774
feat: enhance UI
Browse files- Gradio_UI.py +94 -68
Gradio_UI.py
CHANGED
@@ -1,18 +1,3 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
# coding=utf-8
|
3 |
-
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
|
4 |
-
#
|
5 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
6 |
-
# you may not use this file except in compliance with the License.
|
7 |
-
# You may obtain a copy of the License at
|
8 |
-
#
|
9 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
10 |
-
#
|
11 |
-
# Unless required by applicable law or agreed to in writing, software
|
12 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
13 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 |
-
# See the License for the specific language governing permissions and
|
15 |
-
# limitations under the License.
|
16 |
import mimetypes
|
17 |
import os
|
18 |
import re
|
@@ -24,7 +9,6 @@ from smolagents.agents import ActionStep, MultiStepAgent
|
|
24 |
from smolagents.memory import MemoryStep
|
25 |
from smolagents.utils import _is_package_available
|
26 |
|
27 |
-
|
28 |
def pull_messages_from_step(
|
29 |
step_log: MemoryStep,
|
30 |
):
|
@@ -122,7 +106,6 @@ def pull_messages_from_step(
|
|
122 |
yield gr.ChatMessage(role="assistant", content=f"{step_footnote}")
|
123 |
yield gr.ChatMessage(role="assistant", content="-----")
|
124 |
|
125 |
-
|
126 |
def stream_to_gradio(
|
127 |
agent,
|
128 |
task: str,
|
@@ -153,27 +136,16 @@ def stream_to_gradio(
|
|
153 |
):
|
154 |
yield message
|
155 |
|
156 |
-
final_answer = step_log
|
157 |
final_answer = handle_agent_output_types(final_answer)
|
158 |
|
159 |
-
|
160 |
-
yield gr.ChatMessage(
|
161 |
-
role="assistant",
|
162 |
-
content=f"**Final answer:**\n{final_answer.to_string()}\n",
|
163 |
-
)
|
164 |
-
elif isinstance(final_answer, AgentImage):
|
165 |
-
yield gr.ChatMessage(
|
166 |
-
role="assistant",
|
167 |
-
content={"path": final_answer.to_string(), "mime_type": "image/png"},
|
168 |
-
)
|
169 |
-
elif isinstance(final_answer, AgentAudio):
|
170 |
-
yield gr.ChatMessage(
|
171 |
-
role="assistant",
|
172 |
-
content={"path": final_answer.to_string(), "mime_type": "audio/wav"},
|
173 |
-
)
|
174 |
-
else:
|
175 |
-
yield gr.ChatMessage(role="assistant", content=f"**Final answer:** {str(final_answer)}")
|
176 |
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
class GradioUI:
|
179 |
"""A one-line interface to launch your agent in Gradio"""
|
@@ -189,15 +161,18 @@ class GradioUI:
|
|
189 |
if not os.path.exists(file_upload_folder):
|
190 |
os.mkdir(file_upload_folder)
|
191 |
|
192 |
-
def interact_with_agent(self, prompt, messages):
|
193 |
import gradio as gr
|
194 |
|
195 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
196 |
-
yield messages
|
197 |
for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
|
198 |
messages.append(msg)
|
199 |
-
yield messages
|
200 |
-
|
|
|
|
|
|
|
201 |
|
202 |
def upload_file(
|
203 |
self,
|
@@ -262,35 +237,86 @@ class GradioUI:
|
|
262 |
import gradio as gr
|
263 |
|
264 |
with gr.Blocks(fill_height=True) as demo:
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
[
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
292 |
|
293 |
-
|
|
|
294 |
|
|
|
|
|
|
|
295 |
|
296 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import mimetypes
|
2 |
import os
|
3 |
import re
|
|
|
9 |
from smolagents.memory import MemoryStep
|
10 |
from smolagents.utils import _is_package_available
|
11 |
|
|
|
12 |
def pull_messages_from_step(
|
13 |
step_log: MemoryStep,
|
14 |
):
|
|
|
106 |
yield gr.ChatMessage(role="assistant", content=f"{step_footnote}")
|
107 |
yield gr.ChatMessage(role="assistant", content="-----")
|
108 |
|
|
|
109 |
def stream_to_gradio(
|
110 |
agent,
|
111 |
task: str,
|
|
|
136 |
):
|
137 |
yield message
|
138 |
|
139 |
+
final_answer = step_log
|
140 |
final_answer = handle_agent_output_types(final_answer)
|
141 |
|
142 |
+
final_answer_str = getattr(final_answer, 'final_answer', '')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
+
final_answer_str = f"\n{final_answer_str}\n"
|
145 |
+
|
146 |
+
yield gr.ChatMessage(role="assistant", content=final_answer_str)
|
147 |
+
|
148 |
+
return final_answer_str
|
149 |
|
150 |
class GradioUI:
|
151 |
"""A one-line interface to launch your agent in Gradio"""
|
|
|
161 |
if not os.path.exists(file_upload_folder):
|
162 |
os.mkdir(file_upload_folder)
|
163 |
|
164 |
+
def interact_with_agent(self, prompt, messages, final_answer_state):
|
165 |
import gradio as gr
|
166 |
|
167 |
messages.append(gr.ChatMessage(role="user", content=prompt))
|
168 |
+
yield messages, final_answer_state
|
169 |
for msg in stream_to_gradio(self.agent, task=prompt, reset_agent_memory=False):
|
170 |
messages.append(msg)
|
171 |
+
yield messages, final_answer_state
|
172 |
+
|
173 |
+
# Update final answer state
|
174 |
+
final_answer_state = msg.content if isinstance(msg, gr.ChatMessage) else ""
|
175 |
+
yield messages, final_answer_state
|
176 |
|
177 |
def upload_file(
|
178 |
self,
|
|
|
237 |
import gradio as gr
|
238 |
|
239 |
with gr.Blocks(fill_height=True) as demo:
|
240 |
+
gr.Markdown("""
|
241 |
+
# SmolAgents Gradio UI
|
242 |
+
|
243 |
+
The current Agent is based on the model Qwen2.5-Coder-32B-Instruct.
|
244 |
+
The purpose is to show how an agent can interact with different tools to enhance its capacities.
|
245 |
+
|
246 |
+
#### Available Tools
|
247 |
+
- **Web Search**: Perform a web search using DuckDuckGo.
|
248 |
+
- **Visit Webpage**: Visit a webpage and read its content.
|
249 |
+
- **Get Current Time**: Get the current time in a specified timezone.
|
250 |
+
- **Calculator**: Perform calculations such as simplification, factorisation, integration [see API](https://github.com/aunyks/newton-api).
|
251 |
+
- **Analyze GitHub Repository**: Analyze a GitHub repository and provide insights.
|
252 |
+
|
253 |
+
\n\n
|
254 |
+
|
255 |
+
""")
|
256 |
+
|
257 |
+
with gr.Row():
|
258 |
+
with gr.Column():
|
259 |
+
stored_messages = gr.State([])
|
260 |
+
file_uploads_log = gr.State([])
|
261 |
+
final_answer_state = gr.State("")
|
262 |
+
chatbot = gr.Chatbot(
|
263 |
+
label="Agent",
|
264 |
+
type="messages",
|
265 |
+
avatar_images=(
|
266 |
+
None,
|
267 |
+
"https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/communication/Alfred.png",
|
268 |
+
),
|
269 |
+
resizeable=True,
|
270 |
+
scale=1,
|
271 |
+
)
|
272 |
+
if self.file_upload_folder is not None:
|
273 |
+
upload_file = gr.File(label="Upload a file")
|
274 |
+
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
275 |
+
upload_file.change(
|
276 |
+
self.upload_file,
|
277 |
+
[upload_file, file_uploads_log],
|
278 |
+
[upload_status, file_uploads_log],
|
279 |
+
)
|
280 |
+
text_input = gr.Textbox(lines=1, label="Chat Message")
|
281 |
+
text_input.submit(
|
282 |
+
self.log_user_message,
|
283 |
+
[text_input, file_uploads_log],
|
284 |
+
[stored_messages, text_input],
|
285 |
+
).then(self.interact_with_agent, [stored_messages, chatbot, final_answer_state],
|
286 |
+
[chatbot, final_answer_state])
|
287 |
+
|
288 |
+
with gr.Column():
|
289 |
+
final_answer_display = gr.Markdown("## Final Answer")
|
290 |
+
final_answer_state.change(
|
291 |
+
lambda state: f"## Final Answer\n\n{state}",
|
292 |
+
inputs=final_answer_state,
|
293 |
+
outputs=final_answer_display,
|
294 |
+
)
|
295 |
|
296 |
+
gr.Markdown("""
|
297 |
+
# Example Usage
|
298 |
|
299 |
+
### Question Asking
|
300 |
+
|
301 |
+
What is the capital of baguettes 🥖 and what time is it there?
|
302 |
|
303 |
+
### Ask for doc explanation
|
304 |
+
|
305 |
+
Can you please explain me this documentation :\n
|
306 |
+
https://datashader.org/user_guide/Networks.html ?
|
307 |
+
|
308 |
+
### Mathematics Calculation
|
309 |
+
|
310 |
+
What is the integral of sin(x) dx?
|
311 |
+
|
312 |
+
Can you please also factorise this polynomial :\n
|
313 |
+
x^3 - 6x^2 + 11x - 6 ?
|
314 |
+
|
315 |
+
### Analyze GitHub Repository
|
316 |
+
|
317 |
+
Can you please analyze the statistics of this repository and explain to me what its purpose is:\n
|
318 |
+
https://github.com/mriusero/defi-user-behavior-clustering ?
|
319 |
+
Feel free to star it if you like it 😜!
|
320 |
+
""")
|
321 |
+
|
322 |
+
demo.launch(debug=True, share=True, **kwargs)
|