Use request.session_hash for session ids
Browse files
app.py
CHANGED
|
@@ -80,8 +80,8 @@ def upload_to_hf_and_remove(folder_paths: list[str]):
|
|
| 80 |
print(f"Copying {folder_path} to temporary directory...")
|
| 81 |
shutil.copytree(folder_path, target_path)
|
| 82 |
# Remove the original folder after copying
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
# Upload the entire temporary directory
|
| 87 |
print(f"Uploading all folders to {repo_id}...")
|
|
@@ -122,29 +122,29 @@ def cleanup_sandboxes():
|
|
| 122 |
print(f"Error cleaning up sandbox {session_id}: {str(e)}")
|
| 123 |
|
| 124 |
|
| 125 |
-
def get_or_create_sandbox(
|
| 126 |
current_time = time.time()
|
| 127 |
|
| 128 |
if (
|
| 129 |
-
|
| 130 |
-
and
|
| 131 |
-
and current_time - SANDBOX_METADATA[
|
| 132 |
< SANDBOX_TIMEOUT
|
| 133 |
):
|
| 134 |
-
print(f"Reusing Sandbox for
|
| 135 |
-
SANDBOX_METADATA[
|
| 136 |
-
return SANDBOXES[
|
| 137 |
else:
|
| 138 |
print("No sandbox found, creating a new one")
|
| 139 |
|
| 140 |
-
if
|
| 141 |
try:
|
| 142 |
-
print(f"Closing expired sandbox for session {
|
| 143 |
-
SANDBOXES[
|
| 144 |
except Exception as e:
|
| 145 |
print(f"Error closing expired sandbox: {str(e)}")
|
| 146 |
|
| 147 |
-
print(f"Creating new sandbox for session {
|
| 148 |
desktop = Sandbox(
|
| 149 |
api_key=E2B_API_KEY,
|
| 150 |
resolution=(WIDTH, HEIGHT),
|
|
@@ -156,18 +156,18 @@ def get_or_create_sandbox(session_uuid):
|
|
| 156 |
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"""
|
| 157 |
desktop.commands.run(setup_cmd)
|
| 158 |
|
| 159 |
-
print(f"Sandbox ID for session {
|
| 160 |
|
| 161 |
-
SANDBOXES[
|
| 162 |
-
SANDBOX_METADATA[
|
| 163 |
"created_at": current_time,
|
| 164 |
"last_accessed": current_time,
|
| 165 |
}
|
| 166 |
return desktop
|
| 167 |
|
| 168 |
|
| 169 |
-
def update_html(interactive_mode: bool,
|
| 170 |
-
desktop = get_or_create_sandbox(
|
| 171 |
auth_key = desktop.stream.get_auth_key()
|
| 172 |
base_url = desktop.stream.get_url(auth_key=auth_key)
|
| 173 |
stream_url = base_url if interactive_mode else f"{base_url}&view_only=true"
|
|
@@ -175,8 +175,8 @@ def update_html(interactive_mode: bool, session_uuid):
|
|
| 175 |
status_class = "status-interactive" if interactive_mode else "status-view-only"
|
| 176 |
status_text = "Interactive" if interactive_mode else "Agent running..."
|
| 177 |
creation_time = (
|
| 178 |
-
SANDBOX_METADATA[
|
| 179 |
-
if
|
| 180 |
else time.time()
|
| 181 |
)
|
| 182 |
|
|
@@ -189,8 +189,8 @@ def update_html(interactive_mode: bool, session_uuid):
|
|
| 189 |
return sandbox_html_content
|
| 190 |
|
| 191 |
|
| 192 |
-
def generate_interaction_id(
|
| 193 |
-
return f"{
|
| 194 |
|
| 195 |
|
| 196 |
def save_final_status(folder, status: str, summary, error_message=None) -> None:
|
|
@@ -208,14 +208,11 @@ def extract_browser_uuid(js_uuid):
|
|
| 208 |
return js_uuid
|
| 209 |
|
| 210 |
|
| 211 |
-
def initialize_session(interactive_mode,
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
else:
|
| 217 |
-
print(f"[LOAD] Got UUID from browser: {browser_uuid}")
|
| 218 |
-
return update_html(interactive_mode, browser_uuid), browser_uuid
|
| 219 |
|
| 220 |
|
| 221 |
def create_agent(data_dir, desktop):
|
|
@@ -238,7 +235,7 @@ def create_agent(data_dir, desktop):
|
|
| 238 |
)
|
| 239 |
|
| 240 |
|
| 241 |
-
|
| 242 |
|
| 243 |
|
| 244 |
class EnrichedGradioUI(GradioUI):
|
|
@@ -255,13 +252,14 @@ class EnrichedGradioUI(GradioUI):
|
|
| 255 |
task_input,
|
| 256 |
stored_messages,
|
| 257 |
session_state,
|
| 258 |
-
session_uuid,
|
| 259 |
consent_storage,
|
| 260 |
request: gr.Request,
|
| 261 |
):
|
| 262 |
-
interaction_id = generate_interaction_id(
|
| 263 |
-
desktop = get_or_create_sandbox(
|
| 264 |
-
|
|
|
|
|
|
|
| 265 |
|
| 266 |
data_dir = os.path.join(TMP_DIR, interaction_id)
|
| 267 |
print("CREATING DATA DIR", data_dir, "FROM", TMP_DIR, interaction_id)
|
|
@@ -351,6 +349,7 @@ class EnrichedGradioUI(GradioUI):
|
|
| 351 |
save_final_status(
|
| 352 |
data_dir, status, summary=summary, error_message=error_message
|
| 353 |
)
|
|
|
|
| 354 |
|
| 355 |
|
| 356 |
theme = gr.themes.Default(
|
|
@@ -360,7 +359,6 @@ theme = gr.themes.Default(
|
|
| 360 |
# Create a Gradio app with Blocks
|
| 361 |
with gr.Blocks(theme=theme, css=custom_css, js=CUSTOM_JS) as demo:
|
| 362 |
# Storing session hash in a state variable
|
| 363 |
-
session_uuid_state = gr.State(None)
|
| 364 |
print("Starting the app!")
|
| 365 |
with gr.Row():
|
| 366 |
sandbox_html = gr.HTML(
|
|
@@ -454,11 +452,11 @@ _Please note that we store the task logs by default so **do not write any person
|
|
| 454 |
return f"Guru meditation: {str(e)}"
|
| 455 |
|
| 456 |
# Function to set view-only mode
|
| 457 |
-
def clear_and_set_view_only(task_input,
|
| 458 |
-
return update_html(False,
|
| 459 |
|
| 460 |
-
def set_interactive(
|
| 461 |
-
return update_html(True,
|
| 462 |
|
| 463 |
def reactivate_stop_btn():
|
| 464 |
return gr.Button("Stop the agent!", variant="huggingface")
|
|
@@ -469,7 +467,7 @@ _Please note that we store the task logs by default so **do not write any person
|
|
| 469 |
run_event = (
|
| 470 |
run_btn.click(
|
| 471 |
fn=clear_and_set_view_only,
|
| 472 |
-
inputs=[task_input
|
| 473 |
outputs=[sandbox_html],
|
| 474 |
)
|
| 475 |
.then(
|
|
@@ -478,12 +476,11 @@ _Please note that we store the task logs by default so **do not write any person
|
|
| 478 |
task_input,
|
| 479 |
stored_messages,
|
| 480 |
session_state,
|
| 481 |
-
session_uuid_state,
|
| 482 |
consent_storage,
|
| 483 |
],
|
| 484 |
outputs=[chatbot_display],
|
| 485 |
)
|
| 486 |
-
.then(fn=set_interactive, inputs=[
|
| 487 |
.then(fn=reactivate_stop_btn, outputs=[stop_btn])
|
| 488 |
)
|
| 489 |
|
|
@@ -497,26 +494,29 @@ _Please note that we store the task logs by default so **do not write any person
|
|
| 497 |
|
| 498 |
stop_btn.click(fn=interrupt_agent, inputs=[session_state], outputs=[stop_btn])
|
| 499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
demo.load(
|
| 501 |
fn=lambda: True, # dummy to trigger the load
|
| 502 |
outputs=[is_interactive],
|
| 503 |
).then(
|
| 504 |
fn=initialize_session,
|
| 505 |
-
js="() => localStorage.getItem('gradio-session-uuid') || (() => { const id = self.crypto.randomUUID(); localStorage.setItem('gradio-session-uuid', id); return id })()",
|
| 506 |
inputs=[is_interactive],
|
| 507 |
-
outputs=[sandbox_html
|
| 508 |
)
|
| 509 |
|
| 510 |
-
def upload_interaction_logs():
|
| 511 |
-
data_dirs = []
|
| 512 |
-
for interaction_id in list(INTERACTION_IDS.keys()):
|
| 513 |
-
data_dir = os.path.join(TMP_DIR, interaction_id)
|
| 514 |
-
if os.path.exists(data_dir):
|
| 515 |
-
data_dirs.append(data_dir)
|
| 516 |
-
INTERACTION_IDS.pop(interaction_id)
|
| 517 |
-
|
| 518 |
-
upload_to_hf_and_remove(data_dirs)
|
| 519 |
-
|
| 520 |
demo.unload(fn=upload_interaction_logs)
|
| 521 |
|
| 522 |
# Launch the app
|
|
|
|
| 80 |
print(f"Copying {folder_path} to temporary directory...")
|
| 81 |
shutil.copytree(folder_path, target_path)
|
| 82 |
# Remove the original folder after copying
|
| 83 |
+
shutil.rmtree(folder_path)
|
| 84 |
+
print(f"Original folder {folder_path} removed.")
|
| 85 |
|
| 86 |
# Upload the entire temporary directory
|
| 87 |
print(f"Uploading all folders to {repo_id}...")
|
|
|
|
| 122 |
print(f"Error cleaning up sandbox {session_id}: {str(e)}")
|
| 123 |
|
| 124 |
|
| 125 |
+
def get_or_create_sandbox(session_hash: str):
|
| 126 |
current_time = time.time()
|
| 127 |
|
| 128 |
if (
|
| 129 |
+
session_hash in SANDBOXES
|
| 130 |
+
and session_hash in SANDBOX_METADATA
|
| 131 |
+
and current_time - SANDBOX_METADATA[session_hash]["created_at"]
|
| 132 |
< SANDBOX_TIMEOUT
|
| 133 |
):
|
| 134 |
+
print(f"Reusing Sandbox for session {session_hash}")
|
| 135 |
+
SANDBOX_METADATA[session_hash]["last_accessed"] = current_time
|
| 136 |
+
return SANDBOXES[session_hash]
|
| 137 |
else:
|
| 138 |
print("No sandbox found, creating a new one")
|
| 139 |
|
| 140 |
+
if session_hash in SANDBOXES:
|
| 141 |
try:
|
| 142 |
+
print(f"Closing expired sandbox for session {session_hash}")
|
| 143 |
+
SANDBOXES[session_hash].kill()
|
| 144 |
except Exception as e:
|
| 145 |
print(f"Error closing expired sandbox: {str(e)}")
|
| 146 |
|
| 147 |
+
print(f"Creating new sandbox for session {session_hash}")
|
| 148 |
desktop = Sandbox(
|
| 149 |
api_key=E2B_API_KEY,
|
| 150 |
resolution=(WIDTH, HEIGHT),
|
|
|
|
| 156 |
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"""
|
| 157 |
desktop.commands.run(setup_cmd)
|
| 158 |
|
| 159 |
+
print(f"Sandbox ID for session {session_hash} is {desktop.sandbox_id}.")
|
| 160 |
|
| 161 |
+
SANDBOXES[session_hash] = desktop
|
| 162 |
+
SANDBOX_METADATA[session_hash] = {
|
| 163 |
"created_at": current_time,
|
| 164 |
"last_accessed": current_time,
|
| 165 |
}
|
| 166 |
return desktop
|
| 167 |
|
| 168 |
|
| 169 |
+
def update_html(interactive_mode: bool, session_hash: str):
|
| 170 |
+
desktop = get_or_create_sandbox(session_hash)
|
| 171 |
auth_key = desktop.stream.get_auth_key()
|
| 172 |
base_url = desktop.stream.get_url(auth_key=auth_key)
|
| 173 |
stream_url = base_url if interactive_mode else f"{base_url}&view_only=true"
|
|
|
|
| 175 |
status_class = "status-interactive" if interactive_mode else "status-view-only"
|
| 176 |
status_text = "Interactive" if interactive_mode else "Agent running..."
|
| 177 |
creation_time = (
|
| 178 |
+
SANDBOX_METADATA[session_hash]["created_at"]
|
| 179 |
+
if session_hash in SANDBOX_METADATA
|
| 180 |
else time.time()
|
| 181 |
)
|
| 182 |
|
|
|
|
| 189 |
return sandbox_html_content
|
| 190 |
|
| 191 |
|
| 192 |
+
def generate_interaction_id(session_hash: str):
|
| 193 |
+
return f"{session_hash}_{int(time.time())}"
|
| 194 |
|
| 195 |
|
| 196 |
def save_final_status(folder, status: str, summary, error_message=None) -> None:
|
|
|
|
| 208 |
return js_uuid
|
| 209 |
|
| 210 |
|
| 211 |
+
def initialize_session(interactive_mode, request: gr.Request):
|
| 212 |
+
assert request.session_hash is not None
|
| 213 |
+
print("GETTING REQUEST HASH:", request.session_hash)
|
| 214 |
+
new_uuid = str(uuid.uuid4())
|
| 215 |
+
return update_html(interactive_mode, request.session_hash), new_uuid
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
|
| 218 |
def create_agent(data_dir, desktop):
|
|
|
|
| 235 |
)
|
| 236 |
|
| 237 |
|
| 238 |
+
INTERACTION_IDS_PER_SESSION_HASH: dict[str, dict[str, bool]] = {}
|
| 239 |
|
| 240 |
|
| 241 |
class EnrichedGradioUI(GradioUI):
|
|
|
|
| 252 |
task_input,
|
| 253 |
stored_messages,
|
| 254 |
session_state,
|
|
|
|
| 255 |
consent_storage,
|
| 256 |
request: gr.Request,
|
| 257 |
):
|
| 258 |
+
interaction_id = generate_interaction_id(request.session_hash)
|
| 259 |
+
desktop = get_or_create_sandbox(request.session_hash)
|
| 260 |
+
if request.session_hash not in INTERACTION_IDS_PER_SESSION_HASH:
|
| 261 |
+
INTERACTION_IDS_PER_SESSION_HASH[request.session_hash] = {}
|
| 262 |
+
INTERACTION_IDS_PER_SESSION_HASH[request.session_hash][interaction_id] = True
|
| 263 |
|
| 264 |
data_dir = os.path.join(TMP_DIR, interaction_id)
|
| 265 |
print("CREATING DATA DIR", data_dir, "FROM", TMP_DIR, interaction_id)
|
|
|
|
| 349 |
save_final_status(
|
| 350 |
data_dir, status, summary=summary, error_message=error_message
|
| 351 |
)
|
| 352 |
+
print("SAVING FINAL STATUS", data_dir, status, summary, error_message)
|
| 353 |
|
| 354 |
|
| 355 |
theme = gr.themes.Default(
|
|
|
|
| 359 |
# Create a Gradio app with Blocks
|
| 360 |
with gr.Blocks(theme=theme, css=custom_css, js=CUSTOM_JS) as demo:
|
| 361 |
# Storing session hash in a state variable
|
|
|
|
| 362 |
print("Starting the app!")
|
| 363 |
with gr.Row():
|
| 364 |
sandbox_html = gr.HTML(
|
|
|
|
| 452 |
return f"Guru meditation: {str(e)}"
|
| 453 |
|
| 454 |
# Function to set view-only mode
|
| 455 |
+
def clear_and_set_view_only(task_input, request: gr.Request):
|
| 456 |
+
return update_html(False, request.session_hash)
|
| 457 |
|
| 458 |
+
def set_interactive(request: gr.Request):
|
| 459 |
+
return update_html(True, request.session_hash)
|
| 460 |
|
| 461 |
def reactivate_stop_btn():
|
| 462 |
return gr.Button("Stop the agent!", variant="huggingface")
|
|
|
|
| 467 |
run_event = (
|
| 468 |
run_btn.click(
|
| 469 |
fn=clear_and_set_view_only,
|
| 470 |
+
inputs=[task_input],
|
| 471 |
outputs=[sandbox_html],
|
| 472 |
)
|
| 473 |
.then(
|
|
|
|
| 476 |
task_input,
|
| 477 |
stored_messages,
|
| 478 |
session_state,
|
|
|
|
| 479 |
consent_storage,
|
| 480 |
],
|
| 481 |
outputs=[chatbot_display],
|
| 482 |
)
|
| 483 |
+
.then(fn=set_interactive, inputs=[], outputs=[sandbox_html])
|
| 484 |
.then(fn=reactivate_stop_btn, outputs=[stop_btn])
|
| 485 |
)
|
| 486 |
|
|
|
|
| 494 |
|
| 495 |
stop_btn.click(fn=interrupt_agent, inputs=[session_state], outputs=[stop_btn])
|
| 496 |
|
| 497 |
+
def upload_interaction_logs(session: gr.Request):
|
| 498 |
+
data_dirs = []
|
| 499 |
+
for interaction_id in list(
|
| 500 |
+
INTERACTION_IDS_PER_SESSION_HASH[session.session_hash].keys()
|
| 501 |
+
):
|
| 502 |
+
data_dir = os.path.join(TMP_DIR, interaction_id)
|
| 503 |
+
if os.path.exists(data_dir):
|
| 504 |
+
data_dirs.append(data_dir)
|
| 505 |
+
del INTERACTION_IDS_PER_SESSION_HASH[session.session_hash][
|
| 506 |
+
interaction_id
|
| 507 |
+
]
|
| 508 |
+
|
| 509 |
+
upload_to_hf_and_remove(data_dirs)
|
| 510 |
+
|
| 511 |
demo.load(
|
| 512 |
fn=lambda: True, # dummy to trigger the load
|
| 513 |
outputs=[is_interactive],
|
| 514 |
).then(
|
| 515 |
fn=initialize_session,
|
|
|
|
| 516 |
inputs=[is_interactive],
|
| 517 |
+
outputs=[sandbox_html],
|
| 518 |
)
|
| 519 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
demo.unload(fn=upload_interaction_logs)
|
| 521 |
|
| 522 |
# Launch the app
|