Skip to content

Commit 68a134a

Browse files
committed
Skip sandbox execution when code not changed.
1 parent 36cb8af commit 68a134a

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

fastchat/serve/gradio_web_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ def clear_history(sandbox_state,request: gr.Request):
324324

325325
state = None
326326
sandbox_state['enabled_round'] = 0
327+
sandbox_state['code_to_execute'] = ""
327328
return (state, [], "") + (disable_btn,) * 5 + (sandbox_state,)
328329

329330
def clear_sandbox_components(*components):
@@ -1157,7 +1158,7 @@ def build_single_model_ui(models, add_promotion_links=False):
11571158
# trigger sandbox run
11581159
chatbot.select(fn=on_click_run_code,
11591160
inputs=[state, sandbox_state, sandbox_output, sandbox_ui, sandbox_code],
1160-
outputs=[*sandbox_components])
1161+
outputs=[sandbox_output, sandbox_ui, sandbox_code])
11611162

11621163
return [state, model_selector]
11631164

fastchat/serve/sandbox/code_runner.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,9 @@ class ChatbotSandboxState(TypedDict):
135135
enable_sandbox: bool
136136
sandbox_environment: str | None
137137
sandbox_instruction: str | None
138+
code_to_execute : str | None
138139
enabled_round: int
140+
139141

140142

141143
def create_chatbot_sandbox_state() -> ChatbotSandboxState:
@@ -146,6 +148,7 @@ def create_chatbot_sandbox_state() -> ChatbotSandboxState:
146148
"enable_sandbox": False,
147149
"sandbox_environment": None,
148150
"sandbox_instruction": None,
151+
"code_to_execute":"",
149152
"enabled_round": 0
150153
}
151154

@@ -501,6 +504,20 @@ def on_click_run_code(
501504
raise ValueError("E2B_API_KEY is not set in env vars.")
502505

503506
code, code_language, is_web_page = extract_result
507+
508+
# validate whether code to execute has been updated.
509+
previous_code = sandbox_state.get('code_to_execute', '')
510+
if previous_code == code:
511+
print("Code has not changed. Skipping execution.")
512+
yield (
513+
gr.skip(),
514+
gr.skip(),
515+
gr.skip()
516+
)
517+
return
518+
519+
sandbox_state['code_to_execute'] = code
520+
504521
if code_language == 'tsx':
505522
code_language = 'typescript'
506523
code_language = code_language.lower() if code_language and code_language.lower(

0 commit comments

Comments
 (0)