Spaces:
Running
Running
Update GRADIO_UI.py
Browse files- GRADIO_UI.py +5 -71
GRADIO_UI.py
CHANGED
@@ -2,10 +2,8 @@
|
|
2 |
# coding=utf-8
|
3 |
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
|
4 |
#
|
5 |
-
import mimetypes
|
6 |
import os
|
7 |
import re
|
8 |
-
import shutil
|
9 |
from typing import Optional
|
10 |
|
11 |
from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
|
@@ -175,16 +173,12 @@ def stream_to_gradio(
|
|
175 |
class GradioUI:
|
176 |
"""A one-line interface to launch your agent in Gradio"""
|
177 |
|
178 |
-
def __init__(self, agent: MultiStepAgent
|
179 |
if not _is_package_available("gradio"):
|
180 |
raise ModuleNotFoundError(
|
181 |
"Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
|
182 |
)
|
183 |
self.agent = agent
|
184 |
-
self.file_upload_folder = file_upload_folder
|
185 |
-
if self.file_upload_folder is not None:
|
186 |
-
if not os.path.exists(file_upload_folder):
|
187 |
-
os.mkdir(file_upload_folder)
|
188 |
|
189 |
def interact_with_agent(self, prompt, messages):
|
190 |
import gradio as gr
|
@@ -196,63 +190,11 @@ class GradioUI:
|
|
196 |
yield messages
|
197 |
yield messages
|
198 |
|
199 |
-
def upload_file(
|
200 |
-
self,
|
201 |
-
file,
|
202 |
-
file_uploads_log,
|
203 |
-
allowed_file_types=[
|
204 |
-
"application/pdf",
|
205 |
-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
206 |
-
"text/plain",
|
207 |
-
],
|
208 |
-
):
|
209 |
-
import gradio as gr
|
210 |
-
|
211 |
-
if file is None:
|
212 |
-
return gr.Textbox("No file uploaded", visible=True), file_uploads_log
|
213 |
-
|
214 |
-
try:
|
215 |
-
mime_type, _ = mimetypes.guess_type(file.name)
|
216 |
-
except Exception as e:
|
217 |
-
return gr.Textbox(f"Error: {e}", visible=True), file_uploads_log
|
218 |
-
|
219 |
-
if mime_type not in allowed_file_types:
|
220 |
-
return gr.Textbox("File type disallowed", visible=True), file_uploads_log
|
221 |
-
|
222 |
-
original_name = os.path.basename(file.name)
|
223 |
-
sanitized_name = re.sub(r"[^\w\-.]", "_", original_name)
|
224 |
-
|
225 |
-
type_to_ext = {}
|
226 |
-
for ext, t in mimetypes.types_map.items():
|
227 |
-
if t not in type_to_ext:
|
228 |
-
type_to_ext[t] = ext
|
229 |
-
|
230 |
-
sanitized_name = sanitized_name.split(".")[:-1]
|
231 |
-
sanitized_name.append("" + type_to_ext[mime_type])
|
232 |
-
sanitized_name = "".join(sanitized_name)
|
233 |
-
|
234 |
-
file_path = os.path.join(self.file_upload_folder, os.path.basename(sanitized_name))
|
235 |
-
shutil.copy(file.name, file_path)
|
236 |
-
|
237 |
-
return gr.Textbox(f"File uploaded: {file_path}", visible=True), file_uploads_log + [file_path]
|
238 |
-
|
239 |
-
def log_user_message(self, text_input, file_uploads_log):
|
240 |
-
return (
|
241 |
-
text_input
|
242 |
-
+ (
|
243 |
-
f"\nYou have been provided with these files, which might be helpful or not: {file_uploads_log}"
|
244 |
-
if len(file_uploads_log) > 0
|
245 |
-
else ""
|
246 |
-
),
|
247 |
-
"",
|
248 |
-
)
|
249 |
-
|
250 |
def launch(self, **kwargs):
|
251 |
import gradio as gr
|
252 |
|
253 |
with gr.Blocks(fill_height=True) as demo:
|
254 |
stored_messages = gr.State([])
|
255 |
-
file_uploads_log = gr.State([])
|
256 |
chatbot = gr.Chatbot(
|
257 |
label="Web Navigation Agent",
|
258 |
type="messages",
|
@@ -262,20 +204,12 @@ class GradioUI:
|
|
262 |
),
|
263 |
scale=1,
|
264 |
)
|
265 |
-
if self.file_upload_folder is not None:
|
266 |
-
upload_file = gr.File(label="Upload a file")
|
267 |
-
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
268 |
-
upload_file.change(
|
269 |
-
self.upload_file,
|
270 |
-
[upload_file, file_uploads_log],
|
271 |
-
[upload_status, file_uploads_log],
|
272 |
-
)
|
273 |
text_input = gr.Textbox(lines=1, label="Enter URL and request (e.g., [invalid url, do not cite] Click on Developers)")
|
274 |
text_input.submit(
|
275 |
-
self.
|
276 |
-
[text_input,
|
277 |
-
[
|
278 |
-
)
|
279 |
|
280 |
demo.launch(debug=True, **kwargs)
|
281 |
|
|
|
2 |
# coding=utf-8
|
3 |
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
|
4 |
#
|
|
|
5 |
import os
|
6 |
import re
|
|
|
7 |
from typing import Optional
|
8 |
|
9 |
from smolagents.agent_types import AgentAudio, AgentImage, AgentText, handle_agent_output_types
|
|
|
173 |
class GradioUI:
|
174 |
"""A one-line interface to launch your agent in Gradio"""
|
175 |
|
176 |
+
def __init__(self, agent: MultiStepAgent):
|
177 |
if not _is_package_available("gradio"):
|
178 |
raise ModuleNotFoundError(
|
179 |
"Please install 'gradio' extra to use the GradioUI: `pip install 'smolagents[gradio]'`"
|
180 |
)
|
181 |
self.agent = agent
|
|
|
|
|
|
|
|
|
182 |
|
183 |
def interact_with_agent(self, prompt, messages):
|
184 |
import gradio as gr
|
|
|
190 |
yield messages
|
191 |
yield messages
|
192 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
def launch(self, **kwargs):
|
194 |
import gradio as gr
|
195 |
|
196 |
with gr.Blocks(fill_height=True) as demo:
|
197 |
stored_messages = gr.State([])
|
|
|
198 |
chatbot = gr.Chatbot(
|
199 |
label="Web Navigation Agent",
|
200 |
type="messages",
|
|
|
204 |
),
|
205 |
scale=1,
|
206 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
207 |
text_input = gr.Textbox(lines=1, label="Enter URL and request (e.g., [invalid url, do not cite] Click on Developers)")
|
208 |
text_input.submit(
|
209 |
+
self.interact_with_agent,
|
210 |
+
[text_input, stored_messages],
|
211 |
+
[chatbot]
|
212 |
+
)
|
213 |
|
214 |
demo.launch(debug=True, **kwargs)
|
215 |
|