m-ric HF Staff commited on
Commit
a5afd35
·
1 Parent(s): 50a6a2d

Working chatbot logs

Browse files
Files changed (1) hide show
  1. app.py +17 -20
app.py CHANGED
@@ -6,6 +6,7 @@ import traceback
6
  from e2b_desktop import Sandbox
7
  from huggingface_hub import upload_folder, login
8
  from smolagents.monitoring import LogLevel
 
9
  from textwrap import dedent
10
  import time
11
  from threading import Timer
@@ -424,7 +425,7 @@ def update_terminal_from_session(session_hash):
424
  return read_log_content(log_path)
425
 
426
 
427
- def create_agent():
428
  return E2BVisionAgent(
429
  model=model,
430
  data_dir=data_dir,
@@ -436,7 +437,7 @@ def create_agent():
436
  )
437
 
438
  class EnrichedGradioUI(GradioUI):
439
- def interact_with_agent(self, task_input, messages, session_state, session_hash):
440
  import gradio as gr
441
 
442
  interaction_id = generate_interaction_id(request)
@@ -446,9 +447,12 @@ class EnrichedGradioUI(GradioUI):
446
  data_dir = os.path.join(TMP_DIR, interaction_id)
447
  if not os.path.exists(data_dir):
448
  os.makedirs(data_dir)
449
-
450
  log_file = get_log_file_path(session_hash)
451
 
 
 
 
452
  # Construct the full task with instructions
453
  full_task = task_input + dedent(f"""
454
  The desktop has a resolution of {WIDTH}x{HEIGHT}, take it into account to decide clicking coordinates.
@@ -464,12 +468,8 @@ class EnrichedGradioUI(GradioUI):
464
  We can only execute one action at a time. On each step, answer only a python blob with the action to perform
465
  """)
466
 
467
- # Get the agent type from the template agent
468
- if "agent" not in session_state:
469
- session_state["agent"] = create_agent()
470
-
471
  try:
472
- messages.append(gr.ChatMessage(role="user", content=prompt))
473
  yield messages
474
 
475
  for msg in stream_to_gradio(session_state["agent"], task=full_task, reset_agent_memory=False):
@@ -477,7 +477,7 @@ class EnrichedGradioUI(GradioUI):
477
  yield messages
478
 
479
  yield messages
480
- save_final_status(data_dir, "completed", details = agent.memory.get_succinct_steps())
481
  except Exception as e:
482
  error_message=f"Error in interaction: {str(e)}"
483
  messages.append(gr.ChatMessage(role="assistant", content=error_message))
@@ -556,7 +556,8 @@ with gr.Blocks(css=custom_css, js=custom_js) as demo:
556
  resizeable=True,
557
  scale=1,
558
  )
559
- agent_ui = GradioUI(session_state["agent"])
 
560
 
561
  def read_log_content(log_file, tail=4):
562
  """Read the contents of a log file for a specific session"""
@@ -576,7 +577,7 @@ with gr.Blocks(css=custom_css, js=custom_js) as demo:
576
  # Function to set view-only mode
577
  def clear_and_set_view_only(task_input, request: gr.Request):
578
  # First clear the results, then set view-only mode
579
- return "", update_html(False, request), gr.update(visible=False), gr.update(visible=True)
580
 
581
  # Function to set interactive mode
582
  def set_interactive_mode(request: gr.Request):
@@ -592,18 +593,18 @@ with gr.Blocks(css=custom_css, js=custom_js) as demo:
592
  # Return the current HTML to avoid changing the display
593
  # This will keep the BSOD visible
594
  return gr.update()
595
-
596
 
597
  # Chain the events
598
  # 1. Set view-only mode when button is clicked and reset visibility
599
  view_only_event = update_btn.click(
600
  fn=clear_and_set_view_only,
601
  inputs=[task_input],
602
- outputs=[results_output, html_output, results_container, terminal_container]
603
  ).then(
604
  agent_ui.log_user_message,
605
  [task_input, task_input],
606
- [stored_messages, text_input, submit_btn],
607
  ).then(agent_ui.interact_with_agent, [stored_messages, chatbot, session_state, session_hash_state], [chatbot]).then(
608
  lambda: (
609
  gr.Textbox(
@@ -612,7 +613,7 @@ with gr.Blocks(css=custom_css, js=custom_js) as demo:
612
  gr.Button(interactive=True),
613
  ),
614
  None,
615
- [text_input, submit_btn],
616
  ).then(
617
  fn=check_and_set_interactive,
618
  inputs=[results_output],
@@ -626,11 +627,7 @@ with gr.Blocks(css=custom_css, js=custom_js) as demo:
626
  )
627
 
628
  # Connect refresh button to update terminal
629
- refresh_btn.click(
630
- fn=update_terminal_from_session,
631
- inputs=[session_hash_state],
632
- outputs=[terminal]
633
- )
634
 
635
 
636
  # Launch the app
 
6
  from e2b_desktop import Sandbox
7
  from huggingface_hub import upload_folder, login
8
  from smolagents.monitoring import LogLevel
9
+ from smolagents.gradio_ui import GradioUI, stream_to_gradio
10
  from textwrap import dedent
11
  import time
12
  from threading import Timer
 
425
  return read_log_content(log_path)
426
 
427
 
428
+ def create_agent(data_dir, desktop, log_file):
429
  return E2BVisionAgent(
430
  model=model,
431
  data_dir=data_dir,
 
437
  )
438
 
439
  class EnrichedGradioUI(GradioUI):
440
+ def interact_with_agent(self, task_input, messages, session_state, session_hash, request: gr.Request):
441
  import gradio as gr
442
 
443
  interaction_id = generate_interaction_id(request)
 
447
  data_dir = os.path.join(TMP_DIR, interaction_id)
448
  if not os.path.exists(data_dir):
449
  os.makedirs(data_dir)
450
+
451
  log_file = get_log_file_path(session_hash)
452
 
453
+ if "agent" not in session_state:
454
+ session_state["agent"] = create_agent(data_dir=data_dir, desktop=desktop, log_file=log_file)
455
+
456
  # Construct the full task with instructions
457
  full_task = task_input + dedent(f"""
458
  The desktop has a resolution of {WIDTH}x{HEIGHT}, take it into account to decide clicking coordinates.
 
468
  We can only execute one action at a time. On each step, answer only a python blob with the action to perform
469
  """)
470
 
 
 
 
 
471
  try:
472
+ messages.append(gr.ChatMessage(role="user", content=full_task))
473
  yield messages
474
 
475
  for msg in stream_to_gradio(session_state["agent"], task=full_task, reset_agent_memory=False):
 
477
  yield messages
478
 
479
  yield messages
480
+ save_final_status(data_dir, "completed", details = session_state["agent"].memory.get_succinct_steps())
481
  except Exception as e:
482
  error_message=f"Error in interaction: {str(e)}"
483
  messages.append(gr.ChatMessage(role="assistant", content=error_message))
 
556
  resizeable=True,
557
  scale=1,
558
  )
559
+ from smolagents import CodeAgent
560
+ agent_ui = EnrichedGradioUI(CodeAgent(tools=[], model=None, name="ok", description="ok"))
561
 
562
  def read_log_content(log_file, tail=4):
563
  """Read the contents of a log file for a specific session"""
 
577
  # Function to set view-only mode
578
  def clear_and_set_view_only(task_input, request: gr.Request):
579
  # First clear the results, then set view-only mode
580
+ return "", update_html(False, request), gr.update(visible=False)
581
 
582
  # Function to set interactive mode
583
  def set_interactive_mode(request: gr.Request):
 
593
  # Return the current HTML to avoid changing the display
594
  # This will keep the BSOD visible
595
  return gr.update()
596
+ submit_btn = gr.Button("dont click")
597
 
598
  # Chain the events
599
  # 1. Set view-only mode when button is clicked and reset visibility
600
  view_only_event = update_btn.click(
601
  fn=clear_and_set_view_only,
602
  inputs=[task_input],
603
+ outputs=[results_output, html_output, results_container]
604
  ).then(
605
  agent_ui.log_user_message,
606
  [task_input, task_input],
607
+ [stored_messages, task_input, submit_btn],
608
  ).then(agent_ui.interact_with_agent, [stored_messages, chatbot, session_state, session_hash_state], [chatbot]).then(
609
  lambda: (
610
  gr.Textbox(
 
613
  gr.Button(interactive=True),
614
  ),
615
  None,
616
+ [task_input, submit_btn],
617
  ).then(
618
  fn=check_and_set_interactive,
619
  inputs=[results_output],
 
627
  )
628
 
629
  # Connect refresh button to update terminal
630
+
 
 
 
 
631
 
632
 
633
  # Launch the app