Remove end of run errors, simplifies app.py by pruning elements
Browse files- app.py +18 -56
- e2bqwen.py +5 -2
app.py
CHANGED
|
@@ -478,7 +478,7 @@ class EnrichedGradioUI(GradioUI):
|
|
| 478 |
text_input,
|
| 479 |
gr.Button(interactive=False),
|
| 480 |
)
|
| 481 |
-
def interact_with_agent(self, task_input,
|
| 482 |
import gradio as gr
|
| 483 |
|
| 484 |
interaction_id = generate_interaction_id(request)
|
|
@@ -510,22 +510,20 @@ class EnrichedGradioUI(GradioUI):
|
|
| 510 |
""")
|
| 511 |
|
| 512 |
try:
|
| 513 |
-
|
| 514 |
-
yield
|
| 515 |
|
| 516 |
for msg in stream_to_gradio(session_state["agent"], task=full_task, reset_agent_memory=False):
|
| 517 |
-
|
| 518 |
-
yield
|
| 519 |
|
| 520 |
-
yield
|
| 521 |
save_final_status(data_dir, "completed", details = str(session_state["agent"].memory.get_succinct_steps()))
|
| 522 |
except Exception as e:
|
| 523 |
error_message=f"Error in interaction: {str(e)}"
|
| 524 |
-
|
| 525 |
-
yield
|
| 526 |
save_final_status(data_dir, "failed", details = str(error_message))
|
| 527 |
-
error_result = "Error running agent - Model inference endpoints not ready. Try again later." if 'Both endpoints failed' in error_message else "Error running agent"
|
| 528 |
-
yield gr.ChatMessage(role="assistant", content=error_result)
|
| 529 |
|
| 530 |
finally:
|
| 531 |
upload_to_hf_and_remove(data_dir)
|
|
@@ -577,20 +575,9 @@ with gr.Blocks(css=custom_css, js=custom_js, fill_width=True) as demo:
|
|
| 577 |
)
|
| 578 |
|
| 579 |
update_btn = gr.Button("Let's go!", variant="primary")
|
| 580 |
-
|
| 581 |
-
# with gr.Group(visible=True) as terminal_container:
|
| 582 |
-
|
| 583 |
-
#terminal = gr.Textbox(
|
| 584 |
-
# value="Initializing...",
|
| 585 |
-
# label='Console',
|
| 586 |
-
# lines=5,
|
| 587 |
-
# max_lines=10,
|
| 588 |
-
# interactive=False
|
| 589 |
-
#)
|
| 590 |
-
|
| 591 |
-
|
| 592 |
|
| 593 |
-
|
| 594 |
label="Agent's execution logs",
|
| 595 |
type="messages",
|
| 596 |
avatar_images=(
|
|
@@ -621,26 +608,15 @@ with gr.Blocks(css=custom_css, js=custom_js, fill_width=True) as demo:
|
|
| 621 |
return "".join(lines[-tail:] if len(lines) > tail else lines)
|
| 622 |
except Exception as e:
|
| 623 |
return f"Guru meditation: {str(e)}"
|
| 624 |
-
|
| 625 |
# Function to set view-only mode
|
| 626 |
def clear_and_set_view_only(task_input, request: gr.Request):
|
| 627 |
# First clear the results, then set view-only mode
|
| 628 |
return "", update_html(False, request), gr.update(visible=False)
|
| 629 |
|
| 630 |
-
|
| 631 |
-
def set_interactive_mode(request: gr.Request):
|
| 632 |
return update_html(True, request)
|
| 633 |
-
|
| 634 |
|
| 635 |
-
# Function to check result and conditionally set interactive mode
|
| 636 |
-
def check_and_set_interactive(result, request: gr.Request):
|
| 637 |
-
if result and not result.startswith("Error running agent"):
|
| 638 |
-
# Only set interactive mode if no error
|
| 639 |
-
return update_html(True, request)
|
| 640 |
-
else:
|
| 641 |
-
# Return the current HTML to avoid changing the display
|
| 642 |
-
# This will keep the BSOD visible
|
| 643 |
-
return gr.update()
|
| 644 |
|
| 645 |
# Chain the events
|
| 646 |
# 1. Set view-only mode when button is clicked and reset visibility
|
|
@@ -648,33 +624,19 @@ with gr.Blocks(css=custom_css, js=custom_js, fill_width=True) as demo:
|
|
| 648 |
fn=clear_and_set_view_only,
|
| 649 |
inputs=[task_input],
|
| 650 |
outputs=[results_output, sandbox_html, results_container]
|
| 651 |
-
)
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
[
|
| 655 |
-
).then(agent_ui.interact_with_agent, [stored_messages, chatbot, session_state, session_hash_state], [chatbot]).then(
|
| 656 |
-
lambda: (
|
| 657 |
-
gr.Textbox(
|
| 658 |
-
interactive=True, placeholder="Enter your prompt here and press Shift+Enter or the button"
|
| 659 |
-
),
|
| 660 |
-
gr.Button(interactive=True),
|
| 661 |
-
),
|
| 662 |
-
None,
|
| 663 |
-
[task_input],
|
| 664 |
-
).then(
|
| 665 |
-
fn=check_and_set_interactive,
|
| 666 |
-
inputs=[results_output],
|
| 667 |
outputs=sandbox_html
|
| 668 |
)
|
| 669 |
-
|
|
|
|
| 670 |
demo.load(
|
| 671 |
fn=initialize_session,
|
| 672 |
inputs=[gr.Checkbox(value=True, visible=False)],
|
| 673 |
outputs=[sandbox_html, session_hash_state]
|
| 674 |
)
|
| 675 |
-
|
| 676 |
-
# Connect refresh button to update terminal
|
| 677 |
-
|
| 678 |
|
| 679 |
|
| 680 |
# Launch the app
|
|
|
|
| 478 |
text_input,
|
| 479 |
gr.Button(interactive=False),
|
| 480 |
)
|
| 481 |
+
def interact_with_agent(self, task_input, stored_messages, session_state, session_hash, request: gr.Request):
|
| 482 |
import gradio as gr
|
| 483 |
|
| 484 |
interaction_id = generate_interaction_id(request)
|
|
|
|
| 510 |
""")
|
| 511 |
|
| 512 |
try:
|
| 513 |
+
stored_messages.append(gr.ChatMessage(role="user", content=task_input))
|
| 514 |
+
yield stored_messages
|
| 515 |
|
| 516 |
for msg in stream_to_gradio(session_state["agent"], task=full_task, reset_agent_memory=False):
|
| 517 |
+
stored_messages.append(msg)
|
| 518 |
+
yield stored_messages
|
| 519 |
|
| 520 |
+
yield stored_messages
|
| 521 |
save_final_status(data_dir, "completed", details = str(session_state["agent"].memory.get_succinct_steps()))
|
| 522 |
except Exception as e:
|
| 523 |
error_message=f"Error in interaction: {str(e)}"
|
| 524 |
+
stored_messages.append(gr.ChatMessage(role="assistant", content=error_message))
|
| 525 |
+
yield stored_messages
|
| 526 |
save_final_status(data_dir, "failed", details = str(error_message))
|
|
|
|
|
|
|
| 527 |
|
| 528 |
finally:
|
| 529 |
upload_to_hf_and_remove(data_dir)
|
|
|
|
| 575 |
)
|
| 576 |
|
| 577 |
update_btn = gr.Button("Let's go!", variant="primary")
|
| 578 |
+
cancel_btn = gr.Button("Interrupt running agent")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
|
| 580 |
+
chatbot_display = gr.Chatbot(
|
| 581 |
label="Agent's execution logs",
|
| 582 |
type="messages",
|
| 583 |
avatar_images=(
|
|
|
|
| 608 |
return "".join(lines[-tail:] if len(lines) > tail else lines)
|
| 609 |
except Exception as e:
|
| 610 |
return f"Guru meditation: {str(e)}"
|
| 611 |
+
|
| 612 |
# Function to set view-only mode
|
| 613 |
def clear_and_set_view_only(task_input, request: gr.Request):
|
| 614 |
# First clear the results, then set view-only mode
|
| 615 |
return "", update_html(False, request), gr.update(visible=False)
|
| 616 |
|
| 617 |
+
def set_interactive(request: gr.Request):
|
|
|
|
| 618 |
return update_html(True, request)
|
|
|
|
| 619 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
|
| 621 |
# Chain the events
|
| 622 |
# 1. Set view-only mode when button is clicked and reset visibility
|
|
|
|
| 624 |
fn=clear_and_set_view_only,
|
| 625 |
inputs=[task_input],
|
| 626 |
outputs=[results_output, sandbox_html, results_container]
|
| 627 |
+
)
|
| 628 |
+
view_only_event.then(agent_ui.interact_with_agent, [task_input, stored_messages, session_state, session_hash_state], [chatbot_display]).then(
|
| 629 |
+
fn=set_interactive,
|
| 630 |
+
inputs=[],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 631 |
outputs=sandbox_html
|
| 632 |
)
|
| 633 |
+
cancel_btn.click(fn=(lambda x: x), cancels=[view_only_event])
|
| 634 |
+
|
| 635 |
demo.load(
|
| 636 |
fn=initialize_session,
|
| 637 |
inputs=[gr.Checkbox(value=True, visible=False)],
|
| 638 |
outputs=[sandbox_html, session_hash_state]
|
| 639 |
)
|
|
|
|
|
|
|
|
|
|
| 640 |
|
| 641 |
|
| 642 |
# Launch the app
|
e2bqwen.py
CHANGED
|
@@ -330,6 +330,7 @@ class E2BVisionAgent(CodeAgent):
|
|
| 330 |
|
| 331 |
def take_snapshot_callback(self, memory_step: ActionStep, agent=None) -> None:
|
| 332 |
"""Callback that takes a screenshot + memory snapshot after a step completes"""
|
|
|
|
| 333 |
write_to_console_log(self.log_path, "Analyzing screen content...")
|
| 334 |
|
| 335 |
current_step = memory_step.step_number
|
|
@@ -388,7 +389,7 @@ class QwenVLAPIModel(Model):
|
|
| 388 |
model_path: str = "Qwen/Qwen2.5-VL-72B-Instruct",
|
| 389 |
provider: str = "hyperbolic",
|
| 390 |
hf_token: str = None,
|
| 391 |
-
hf_base_url: str = "https://n5wr7lfx6wp94tvl.us-east-1.aws.endpoints.huggingface.cloud
|
| 392 |
):
|
| 393 |
super().__init__()
|
| 394 |
self.model_path = model_path
|
|
@@ -401,13 +402,15 @@ class QwenVLAPIModel(Model):
|
|
| 401 |
self.hyperbolic_client = InferenceClient(
|
| 402 |
provider=self.provider,
|
| 403 |
)
|
|
|
|
|
|
|
| 404 |
|
| 405 |
# Initialize HF OpenAI-compatible client if token is provided
|
| 406 |
self.hf_client = None
|
| 407 |
if hf_token:
|
| 408 |
from openai import OpenAI
|
| 409 |
self.hf_client = OpenAI(
|
| 410 |
-
base_url=self.hf_base_url,
|
| 411 |
api_key=self.hf_token
|
| 412 |
)
|
| 413 |
|
|
|
|
| 330 |
|
| 331 |
def take_snapshot_callback(self, memory_step: ActionStep, agent=None) -> None:
|
| 332 |
"""Callback that takes a screenshot + memory snapshot after a step completes"""
|
| 333 |
+
print("FYI, here is the system prompt:", agent.system_prompt)
|
| 334 |
write_to_console_log(self.log_path, "Analyzing screen content...")
|
| 335 |
|
| 336 |
current_step = memory_step.step_number
|
|
|
|
| 389 |
model_path: str = "Qwen/Qwen2.5-VL-72B-Instruct",
|
| 390 |
provider: str = "hyperbolic",
|
| 391 |
hf_token: str = None,
|
| 392 |
+
hf_base_url: str = "https://n5wr7lfx6wp94tvl.us-east-1.aws.endpoints.huggingface.cloud"
|
| 393 |
):
|
| 394 |
super().__init__()
|
| 395 |
self.model_path = model_path
|
|
|
|
| 402 |
self.hyperbolic_client = InferenceClient(
|
| 403 |
provider=self.provider,
|
| 404 |
)
|
| 405 |
+
|
| 406 |
+
assert not self.hf_base_url.endswith("/v1/"), "Enter your base url without '/v1/' suffix."
|
| 407 |
|
| 408 |
# Initialize HF OpenAI-compatible client if token is provided
|
| 409 |
self.hf_client = None
|
| 410 |
if hf_token:
|
| 411 |
from openai import OpenAI
|
| 412 |
self.hf_client = OpenAI(
|
| 413 |
+
base_url=self.hf_base_url + "/v1/",
|
| 414 |
api_key=self.hf_token
|
| 415 |
)
|
| 416 |
|