computer-agent / app.py
lvwerra's picture
lvwerra HF Staff
refactor
29ccb45
raw
history blame
17.8 kB
import gradio as gr
import os
import json
import shutil
import traceback
from textwrap import dedent
import time
from threading import Timer
from huggingface_hub import upload_folder, login
from e2b_desktop import Sandbox
from smolagents import CodeAgent
from smolagents.monitoring import LogLevel
from smolagents.gradio_ui import GradioUI, stream_to_gradio
from e2bqwen import QwenVLAPIModel, E2BVisionAgent
E2B_API_KEY = os.getenv("E2B_API_KEY")
HF_TOKEN = os.getenv("HUGGINGFACE_API_KEY")
HF_ENDPOINT_URL = os.getenv("HUGGINGFACE_ENPOINT_URL")
SANDBOXES = {}
SANDBOX_METADATA = {}
SANDBOX_TIMEOUT = 600
WIDTH = 1280
HEIGHT = 960
TMP_DIR = './tmp/'
if not os.path.exists(TMP_DIR):
os.makedirs(TMP_DIR)
CSS_PATH = "./custom.css"
SANDBOX_TEMPLATE = "./sandbox.html"
FOOTER_PATH = "./footer.html"
JS_PATH = "./custom.js"
login(token=HF_TOKEN)
model = QwenVLAPIModel(
hf_base_url=HF_ENDPOINT_URL,
hf_token = HF_TOKEN,
)
with open(CSS_PATH, "r") as f:
custom_css = f.read().replace("<<WIDTH>>", str(WIDTH+15)).replace("<<HEIGHT>>", str(HEIGHT+10))
with open(FOOTER_PATH, "r") as f:
footer_html = f.read()
with open(SANDBOX_TEMPLATE, "r") as f:
sandbox_html_template = f.read().replace("<<WIDTH>>", str(WIDTH+15)).replace("<<HEIGHT>>", str(HEIGHT+10))
with open(JS_PATH, "r") as f:
custom_js = f.read()
def write_to_console_log(log_file_path, message):
"""
Appends a message to the specified log file with a newline character.
Parameters:
log_file_path (str): Path to the log file
message (str): Message to append to the log file
"""
if log_file_path is None:
return False
try:
# Open the file in append mode
with open(log_file_path, 'a') as log_file:
# Write the message followed by a newline
log_file.write(f"{message}\n")
return True
except Exception as e:
print(f"Error writing to log file: {str(e)}")
return False
def upload_to_hf_and_remove(folder_path):
repo_id = "smolagents/computer-agent-logs"
try:
folder_name = os.path.basename(os.path.normpath(folder_path))
# Upload the folder to Huggingface
print(f"Uploading {folder_path} to {repo_id}/{folder_name}...")
url = upload_folder(
folder_path=folder_path,
repo_id=repo_id,
repo_type="dataset",
path_in_repo=folder_name,
ignore_patterns=[".git/*", ".gitignore"]
)
# Remove the local folder after successful upload
print(f"Upload complete. Removing local folder {folder_path}...")
shutil.rmtree(folder_path)
print("Local folder removed successfully.")
return url
except Exception as e:
print(f"Error during upload or cleanup: {str(e)}")
raise
def cleanup_sandboxes():
"""Remove sandboxes that haven't been accessed for more than 5 minutes"""
current_time = time.time()
sandboxes_to_remove = []
for session_id, metadata in SANDBOX_METADATA.items():
if current_time - metadata['last_accessed'] > SANDBOX_TIMEOUT:
sandboxes_to_remove.append(session_id)
for session_id in sandboxes_to_remove:
if session_id in SANDBOXES:
try:
# Upload data before removing if needed
data_dir = os.path.join(TMP_DIR, session_id)
if os.path.exists(data_dir):
upload_to_hf_and_remove(data_dir)
# Close the sandbox
SANDBOXES[session_id].kill()
del SANDBOXES[session_id]
del SANDBOX_METADATA[session_id]
print(f"Cleaned up sandbox for session {session_id}")
except Exception as e:
print(f"Error cleaning up sandbox {session_id}: {str(e)}")
def get_or_create_sandbox(session_hash):
current_time = time.time()
# Check if sandbox exists and is still valid
if (session_hash in SANDBOXES and
session_hash in SANDBOX_METADATA and
current_time - SANDBOX_METADATA[session_hash]['created_at'] < SANDBOX_TIMEOUT):
# Update last accessed time
SANDBOX_METADATA[session_hash]['last_accessed'] = current_time
return SANDBOXES[session_hash]
# Close existing sandbox if it exists but is too old
if session_hash in SANDBOXES:
try:
print(f"Closing expired sandbox for session {session_hash}")
SANDBOXES[session_hash].kill()
except Exception as e:
print(f"Error closing expired sandbox: {str(e)}")
# Create new sandbox
print(f"Creating new sandbox for session {session_hash}")
desktop = Sandbox(api_key=E2B_API_KEY, resolution=(WIDTH, HEIGHT), dpi=96, timeout=SANDBOX_TIMEOUT)
desktop.stream.start(require_auth=True)
setup_cmd = """sudo mkdir -p /usr/lib/firefox-esr/distribution && echo '{"policies":{"OverrideFirstRunPage":"","OverridePostUpdatePage":"","DisableProfileImport":true,"DontCheckDefaultBrowser":true}}' | sudo tee /usr/lib/firefox-esr/distribution/policies.json > /dev/null"""
desktop.commands.run(setup_cmd)
# Store sandbox with metadata
SANDBOXES[session_hash] = desktop
SANDBOX_METADATA[session_hash] = {
'created_at': current_time,
'last_accessed': current_time
}
return desktop
def update_html(interactive_mode: bool, request: gr.Request):
session_hash = request.session_hash
desktop = get_or_create_sandbox(session_hash)
auth_key = desktop.stream.get_auth_key()
# Add view_only parameter based on interactive_mode
base_url = desktop.stream.get_url(auth_key=auth_key)
stream_url = base_url if interactive_mode else f"{base_url}&view_only=true"
# Set status indicator class and text
status_class = "status-interactive" if interactive_mode else "status-view-only"
status_text = "Interactive" if interactive_mode else "Agent running: View only"
creation_time = SANDBOX_METADATA[session_hash]['created_at'] if session_hash in SANDBOX_METADATA else time.time()
sandbox_html_content = sandbox_html_template.format(
stream_url=stream_url,
status_class=status_class,
status_text=status_text,
)
# Add hidden field with creation time for JavaScript to use
sandbox_html_content += f'<div id="sandbox-creation-time" style="display:none;" data-time="{creation_time}" data-timeout="{SANDBOX_TIMEOUT}"></div>'
return sandbox_html_content
def generate_interaction_id(request):
"""Generate a unique ID combining session hash and timestamp"""
return f"{request.session_hash}_{int(time.time())}"
def save_final_status(folder, status, details = None):
a = open(os.path.join(folder,"status.json"),"w")
a.write(json.dumps({"status":status,"details":details}))
a.close()
def get_log_file_path(session_hash):
"""
Creates a log file path based on the session hash.
Makes sure the directory exists.
"""
log_dir = os.path.join(TMP_DIR, session_hash)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
return os.path.join(log_dir, 'console.log')
def initialize_session(interactive_mode, request: gr.Request):
session_hash = request.session_hash
# Create session-specific log file
log_path = get_log_file_path(session_hash)
# Initialize log file if it doesn't exist
if not os.path.exists(log_path):
with open(log_path, 'w') as f:
f.write(f"Ready to go...\n")
# Return HTML and session hash
return update_html(interactive_mode, request), session_hash
# Function to read log content that gets the path from session hash
def update_terminal_from_session(session_hash):
if not session_hash:
return "Waiting for session..."
log_path = get_log_file_path(session_hash)
return read_log_content(log_path)
def create_agent(data_dir, desktop, log_file):
return E2BVisionAgent(
model=model,
data_dir=data_dir,
desktop=desktop,
max_steps=200,
verbosity_level=LogLevel.INFO,
planning_interval=10,
log_file = log_file
)
class EnrichedGradioUI(GradioUI):
def log_user_message(self, text_input):
import gradio as gr
return (
text_input,
gr.Button(interactive=False),
)
def interact_with_agent(self, task_input, stored_messages, session_state, session_hash, request: gr.Request):
import gradio as gr
interaction_id = generate_interaction_id(request)
desktop = get_or_create_sandbox(session_hash)
# Create data directory for this session
data_dir = os.path.join(TMP_DIR, interaction_id)
if not os.path.exists(data_dir):
os.makedirs(data_dir)
log_file = get_log_file_path(session_hash)
if "agent" not in session_state:
session_state["agent"] = create_agent(data_dir=data_dir, desktop=desktop, log_file=log_file)
# Construct the full task with instructions
full_task = task_input + dedent(f"""
The desktop has a resolution of {WIDTH}x{HEIGHT}, take it into account to decide clicking coordinates.
When clicking an element, always make sure to click THE MIDDLE of that element! Else you risk to miss it.
Always analyze the latest screenshot carefully before performing actions. Make sure to:
1. Look at elements on the screen to determine what to click or interact with
2. Use precise coordinates for mouse movements and clicks
3. Wait for page loads or animations to complete using the wait() tool
4. Sometimes you may have missed a click, so never assume that you're on the right page, always make sure that your previous action worked. In the screenshot you can see if the mouse is out of the clickable area. Pay special attention to this.
When you receive a task, break it down into step-by-step actions. On each step, look at the current screenshot to validate if previous steps worked and decide the next action.
We can only execute one action at a time. On each step, answer only a python blob with the action to perform
""")
# try:
stored_messages.append(gr.ChatMessage(role="user", content=task_input))
yield stored_messages
for msg in stream_to_gradio(session_state["agent"], task=full_task, reset_agent_memory=False):
stored_messages.append(msg)
yield stored_messages
yield stored_messages
# save_final_status(data_dir, "completed", details = str(session_state["agent"].memory.get_succinct_steps()))
# except Exception as e:
# error_message=f"Error in interaction: {str(e)}"
# stored_messages.append(gr.ChatMessage(role="assistant", content=error_message))
# yield stored_messages
# save_final_status(data_dir, "failed", details = str(error_message))
# finally:
# upload_to_hf_and_remove(data_dir)
theme = gr.themes.Default(font=["Oxanium", "sans-serif"], primary_hue="amber", secondary_hue="blue")
# Create a Gradio app with Blocks
with gr.Blocks(theme=theme, css=custom_css, js=custom_js, fill_width=True) as demo:
#Storing session hash in a state variable
session_hash_state = gr.State(None)
gr.HTML("""<h1 style="color:var(--color-accent);">Computer Agent - Input your task and run your personal assistant!<h1>
<h5>Input your task in the left sidebar to see our agent get to work.</h5>""")
with gr.Row():
sandbox_html = gr.HTML(
value=sandbox_html_template.format(
stream_url="",
status_class="status-interactive",
status_text="Interactive"
),
label="Output"
)
with gr.Sidebar(position="left"):
task_input = gr.Textbox(
value="Find picture of cute puppies",
label="Enter your task below:",
elem_classes="primary-color-label"
)
gr.Examples(
examples=[
"Check the commuting time between Bern and Zurich",
"Write 'Hello World' in a text editor",
"Search a flight Paris - Berlin for tomorrow",
"Could you head to Fontainebleau (France) in Google Maps then drag and drop to position the castle of Fontainebleau exactly in the center?",
"Download me a picture of a puppy from Google, then head to Hugging Face, find a Space dedicated to background removal, and use it to remove the puppy picture's background"
],
inputs = task_input,
label= "Example Tasks",
examples_per_page=4
)
session_state = gr.State({})
stored_messages = gr.State([])
with gr.Group(visible=False) as results_container:
results_output = gr.Textbox(
label="Results",
interactive=False,
elem_id="results-output"
)
update_btn = gr.Button("Let's go!", variant="primary")
minimalist_toggle = gr.Checkbox(label="Minimalist mode - no frame", value=False)
def apply_theme(minimalist_mode: bool):
if not minimalist_mode:
return """
<style>
:root {
--body-background-fill: black!important;
--body-text-color: #f59e0b!important;
--block-text-color: #f59e0b!important;
--color-accent: #00b8ff!important;
}
.sandbox-frame {
display: block!important;
}
.body {
color: #00b8ff!important;
}
.sandbox-iframe, .bsod-image {
/* top: 73px; */
top: 99px;
/* left: 74px; */
left: 110px;
}
.sandbox-iframe {
transform: scale(0.535);
/* transform: scale(0.59); */
}
.status-bar {
position: absolute;
bottom: 86px;
left: 355px;
}
.status-text {
color: #fed244;
}
</style>
"""
else:
return """
<style>
.sandbox-container {
height: 700px!important;
}
.sandbox-iframe {
transform: scale(0.65);
}
</style>
"""
# Hidden HTML element to inject CSS dynamically
theme_styles = gr.HTML(apply_theme(False), visible=False)
minimalist_toggle.change(
fn=apply_theme,
inputs=[minimalist_toggle],
outputs=[theme_styles]
)
footer = gr.HTML(
value=footer_html,
label="Header"
)
chatbot_display = gr.Chatbot(
label="Agent's execution logs",
type="messages",
avatar_images=(
None,
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/smolagents/mascot_smol.png",
),
resizable=True,
scale=1,
)
agent_ui = EnrichedGradioUI(CodeAgent(tools=[], model=None, name="ok", description="ok"))
def read_log_content(log_file, tail=4):
"""Read the contents of a log file for a specific session"""
if not log_file:
return "Waiting for session..."
if not os.path.exists(log_file):
return "Waiting for machine from the future to boot..."
try:
with open(log_file, 'r') as f:
lines = f.readlines()
return "".join(lines[-tail:] if len(lines) > tail else lines)
except Exception as e:
return f"Guru meditation: {str(e)}"
# Function to set view-only mode
def clear_and_set_view_only(task_input, request: gr.Request):
# First clear the results, then set view-only mode
return "", update_html(False, request), gr.update(visible=False)
def set_interactive(request: gr.Request):
return update_html(True, request)
is_interactive = gr.Checkbox(value=True, visible=False)
# Chain the events
view_only_event = update_btn.click(
fn=clear_and_set_view_only,
inputs=[task_input],
outputs=[results_output, sandbox_html, results_container]
)
view_only_event.then(agent_ui.interact_with_agent, [task_input, stored_messages, session_state, session_hash_state], [chatbot_display]).then(
fn=set_interactive,
inputs=[],
outputs=sandbox_html
)
demo.load(
fn=initialize_session,
inputs=[is_interactive],
outputs=[sandbox_html, session_hash_state],
)
# Launch the app
if __name__ == "__main__":
Timer(60, cleanup_sandboxes).start() # Run every minute
demo.launch()