Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
added sidebar layout
#19
by
ysharma
HF staff
- opened
app.py
CHANGED
@@ -262,15 +262,25 @@ class GradioUI:
|
|
262 |
# Get or create session-specific agent
|
263 |
if 'agent' not in session_state:
|
264 |
session_state['agent'] = create_agent()
|
265 |
-
|
266 |
-
messages.append(gr.ChatMessage(role="user", content=prompt))
|
267 |
-
yield messages
|
268 |
|
269 |
-
#
|
270 |
-
|
271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
yield messages
|
273 |
-
|
|
|
|
|
274 |
|
275 |
def upload_file(
|
276 |
self,
|
@@ -326,54 +336,178 @@ class GradioUI:
|
|
326 |
if len(file_uploads_log) > 0
|
327 |
else ""
|
328 |
),
|
329 |
-
"",
|
|
|
330 |
)
|
331 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
332 |
def launch(self, **kwargs):
|
|
|
333 |
with gr.Blocks(theme="ocean", fill_height=True) as demo:
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
378 |
|
379 |
GradioUI().launch()
|
|
|
262 |
# Get or create session-specific agent
|
263 |
if 'agent' not in session_state:
|
264 |
session_state['agent'] = create_agent()
|
|
|
|
|
|
|
265 |
|
266 |
+
# Adding monitoring
|
267 |
+
try:
|
268 |
+
# log the existence of agent memory
|
269 |
+
has_memory = hasattr(session_state['agent'], 'memory')
|
270 |
+
print(f"Agent has memory: {has_memory}")
|
271 |
+
if has_memory:
|
272 |
+
print(f"Memory type: {type(session_state['agent'].memory)}")
|
273 |
+
|
274 |
+
messages.append(gr.ChatMessage(role="user", content=prompt))
|
275 |
+
yield messages
|
276 |
+
|
277 |
+
for msg in stream_to_gradio(session_state['agent'], task=prompt, reset_agent_memory=False):
|
278 |
+
messages.append(msg)
|
279 |
+
yield messages
|
280 |
yield messages
|
281 |
+
except Exception as e:
|
282 |
+
print(f"Error in interaction: {str(e)}")
|
283 |
+
raise
|
284 |
|
285 |
def upload_file(
|
286 |
self,
|
|
|
336 |
if len(file_uploads_log) > 0
|
337 |
else ""
|
338 |
),
|
339 |
+
gr.Textbox(value="", interactive=False, placeholder="Please wait while Steps are getting populated"),
|
340 |
+
gr.Button(interactive=False)
|
341 |
)
|
342 |
|
343 |
+
def detect_device(self, request: gr.Request):
|
344 |
+
# Check whether the user device is a mobile or a computer
|
345 |
+
|
346 |
+
if not request:
|
347 |
+
return "Unknown device"
|
348 |
+
# Method 1: Check sec-ch-ua-mobile header
|
349 |
+
is_mobile_header = request.headers.get('sec-ch-ua-mobile')
|
350 |
+
if is_mobile_header:
|
351 |
+
return "Mobile" if '?1' in is_mobile_header else "Desktop"
|
352 |
+
|
353 |
+
# Method 2: Check user-agent string
|
354 |
+
user_agent = request.headers.get('user-agent', '').lower()
|
355 |
+
mobile_keywords = ['android', 'iphone', 'ipad', 'mobile', 'phone']
|
356 |
+
|
357 |
+
if any(keyword in user_agent for keyword in mobile_keywords):
|
358 |
+
return "Mobile"
|
359 |
+
|
360 |
+
# Method 3: Check platform
|
361 |
+
platform = request.headers.get('sec-ch-ua-platform', '').lower()
|
362 |
+
if platform:
|
363 |
+
if platform in ['"android"', '"ios"']:
|
364 |
+
return "Mobile"
|
365 |
+
elif platform in ['"windows"', '"macos"', '"linux"']:
|
366 |
+
return "Desktop"
|
367 |
+
|
368 |
+
# Default case if no clear indicators
|
369 |
+
return "Desktop"
|
370 |
+
|
371 |
def launch(self, **kwargs):
|
372 |
+
|
373 |
with gr.Blocks(theme="ocean", fill_height=True) as demo:
|
374 |
+
# Different layouts for mobile and computer devices
|
375 |
+
@gr.render()
|
376 |
+
def layout(request: gr.Request):
|
377 |
+
device = self.detect_device(request)
|
378 |
+
print(f"device - {device}")
|
379 |
+
# Render layout with sidebar
|
380 |
+
if device == "Desktop":
|
381 |
+
with gr.Blocks(fill_height=True,) as sidebar_demo:
|
382 |
+
with gr.Sidebar():
|
383 |
+
gr.Markdown("""# open Deep Research - free the AI agents!
|
384 |
+
|
385 |
+
OpenAI just published [Deep Research](https://openai.com/index/introducing-deep-research/), a very nice assistant that can perform deep searches on the web to answer user questions.
|
386 |
+
|
387 |
+
However, their agent has a huge downside: it's not open. So we've started a 24-hour rush to replicate and open-source it. Our resulting [open-Deep-Research agent](https://github.com/huggingface/smolagents/tree/main/examples/open_deep_research) took the #1 rank of any open submission on the GAIA leaderboard! β¨
|
388 |
+
|
389 |
+
You can try a simplified version here.<br><br>""")
|
390 |
+
with gr.Group():
|
391 |
+
gr.Markdown("**Your request**", container=True)
|
392 |
+
text_input = gr.Textbox(lines=3, label="Your request", container=False, placeholder="Enter your prompt here and press Shift+Enter or press the button")
|
393 |
+
launch_research_btn = gr.Button("Run", variant="primary")
|
394 |
+
|
395 |
+
# If an upload folder is provided, enable the upload feature
|
396 |
+
if self.file_upload_folder is not None:
|
397 |
+
upload_file = gr.File(label="Upload a file")
|
398 |
+
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
399 |
+
upload_file.change(
|
400 |
+
self.upload_file,
|
401 |
+
[upload_file, file_uploads_log],
|
402 |
+
[upload_status, file_uploads_log],
|
403 |
+
)
|
404 |
+
|
405 |
+
gr.HTML("<br><br><h4><center>Powered by:</center></h4>")
|
406 |
+
with gr.Row():
|
407 |
+
gr.HTML("""<div style="display: flex; align-items: center; gap: 8px; font-family: system-ui, -apple-system, sans-serif;">
|
408 |
+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png" style="width: 32px; height: 32px; object-fit: contain;" alt="logo">
|
409 |
+
<a href="https://github.com/huggingface/smolagents"><b>huggingface/smolagents</b></a>
|
410 |
+
</div>""")
|
411 |
+
|
412 |
+
# Add session state to store session-specific data
|
413 |
+
session_state = gr.State({}) # Initialize empty state for each session
|
414 |
+
stored_messages = gr.State([])
|
415 |
+
file_uploads_log = gr.State([])
|
416 |
+
chatbot = gr.Chatbot(
|
417 |
+
label="open-Deep-Research",
|
418 |
+
type="messages",
|
419 |
+
avatar_images=(
|
420 |
+
None,
|
421 |
+
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png",
|
422 |
+
),
|
423 |
+
resizeable=False,
|
424 |
+
scale=1,
|
425 |
+
elem_id="my-chatbot"
|
426 |
+
)
|
427 |
+
|
428 |
+
text_input.submit(
|
429 |
+
self.log_user_message,
|
430 |
+
[text_input, file_uploads_log],
|
431 |
+
[stored_messages, text_input, launch_research_btn],
|
432 |
+
).then(self.interact_with_agent,
|
433 |
+
# Include session_state in function calls
|
434 |
+
[stored_messages, chatbot, session_state],
|
435 |
+
[chatbot]
|
436 |
+
).then(lambda : (gr.Textbox(interactive=True, placeholder="Enter your prompt here and press the button"), gr.Button(interactive=True)),
|
437 |
+
None,
|
438 |
+
[text_input, launch_research_btn])
|
439 |
+
launch_research_btn.click(
|
440 |
+
self.log_user_message,
|
441 |
+
[text_input, file_uploads_log],
|
442 |
+
[stored_messages, text_input, launch_research_btn],
|
443 |
+
).then(self.interact_with_agent,
|
444 |
+
# Include session_state in function calls
|
445 |
+
[stored_messages, chatbot, session_state],
|
446 |
+
[chatbot]
|
447 |
+
).then(lambda : (gr.Textbox(interactive=True, placeholder="Enter your prompt here and press the button"), gr.Button(interactive=True)),
|
448 |
+
None,
|
449 |
+
[text_input, launch_research_btn])
|
450 |
+
|
451 |
+
# Render simple layout
|
452 |
+
else:
|
453 |
+
with gr.Blocks(fill_height=True,) as simple_demo:
|
454 |
+
gr.Markdown("""# open Deep Research - free the AI agents!
|
455 |
+
_Built with [smolagents](https://github.com/huggingface/smolagents)_
|
456 |
+
|
457 |
+
OpenAI just published [Deep Research](https://openai.com/index/introducing-deep-research/), a very nice assistant that can perform deep searches on the web to answer user questions.
|
458 |
+
|
459 |
+
However, their agent has a huge downside: it's not open. So we've started a 24-hour rush to replicate and open-source it. Our resulting [open-Deep-Research agent](https://github.com/huggingface/smolagents/tree/main/examples/open_deep_research) took the #1 rank of any open submission on the GAIA leaderboard! β¨
|
460 |
+
|
461 |
+
You can try a simplified version below. π""")
|
462 |
+
# Add session state to store session-specific data
|
463 |
+
session_state = gr.State({}) # Initialize empty state for each session
|
464 |
+
stored_messages = gr.State([])
|
465 |
+
file_uploads_log = gr.State([])
|
466 |
+
chatbot = gr.Chatbot(
|
467 |
+
label="open-Deep-Research",
|
468 |
+
type="messages",
|
469 |
+
avatar_images=(
|
470 |
+
None,
|
471 |
+
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png",
|
472 |
+
),
|
473 |
+
resizeable=True,
|
474 |
+
scale=1,
|
475 |
+
)
|
476 |
+
# If an upload folder is provided, enable the upload feature
|
477 |
+
if self.file_upload_folder is not None:
|
478 |
+
upload_file = gr.File(label="Upload a file")
|
479 |
+
upload_status = gr.Textbox(label="Upload Status", interactive=False, visible=False)
|
480 |
+
upload_file.change(
|
481 |
+
self.upload_file,
|
482 |
+
[upload_file, file_uploads_log],
|
483 |
+
[upload_status, file_uploads_log],
|
484 |
+
)
|
485 |
+
text_input = gr.Textbox(lines=1, label="Your request", placeholder="Enter your prompt here and press the button")
|
486 |
+
launch_research_btn = gr.Button("Run", variant="primary",)
|
487 |
+
|
488 |
+
text_input.submit(
|
489 |
+
self.log_user_message,
|
490 |
+
[text_input, file_uploads_log],
|
491 |
+
[stored_messages, text_input, launch_research_btn],
|
492 |
+
).then(self.interact_with_agent,
|
493 |
+
# Include session_state in function calls
|
494 |
+
[stored_messages, chatbot, session_state],
|
495 |
+
[chatbot]
|
496 |
+
).then(lambda : (gr.Textbox(interactive=True, placeholder="Enter your prompt here and press the button"), gr.Button(interactive=True)),
|
497 |
+
None,
|
498 |
+
[text_input, launch_research_btn])
|
499 |
+
launch_research_btn.click(
|
500 |
+
self.log_user_message,
|
501 |
+
[text_input, file_uploads_log],
|
502 |
+
[stored_messages, text_input, launch_research_btn],
|
503 |
+
).then(self.interact_with_agent,
|
504 |
+
# Include session_state in function calls
|
505 |
+
[stored_messages, chatbot, session_state],
|
506 |
+
[chatbot]
|
507 |
+
).then(lambda : (gr.Textbox(interactive=True, placeholder="Enter your prompt here and press the button"), gr.Button(interactive=True)),
|
508 |
+
None,
|
509 |
+
[text_input, launch_research_btn])
|
510 |
+
|
511 |
+
demo.launch(debug=True, **kwargs)
|
512 |
|
513 |
GradioUI().launch()
|