Skip to content

Commit

Permalink
Skip sandbox execution when code not changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoeyyhc committed Dec 11, 2024
1 parent 36cb8af commit 68a134a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion fastchat/serve/gradio_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ def clear_history(sandbox_state,request: gr.Request):

state = None
sandbox_state['enabled_round'] = 0
sandbox_state['code_to_execute'] = ""
return (state, [], "") + (disable_btn,) * 5 + (sandbox_state,)

def clear_sandbox_components(*components):
Expand Down Expand Up @@ -1157,7 +1158,7 @@ def build_single_model_ui(models, add_promotion_links=False):
# trigger sandbox run
chatbot.select(fn=on_click_run_code,
inputs=[state, sandbox_state, sandbox_output, sandbox_ui, sandbox_code],
outputs=[*sandbox_components])
outputs=[sandbox_output, sandbox_ui, sandbox_code])

return [state, model_selector]

Expand Down
17 changes: 17 additions & 0 deletions fastchat/serve/sandbox/code_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ class ChatbotSandboxState(TypedDict):
enable_sandbox: bool
sandbox_environment: str | None
sandbox_instruction: str | None
code_to_execute : str | None
enabled_round: int



def create_chatbot_sandbox_state() -> ChatbotSandboxState:
Expand All @@ -146,6 +148,7 @@ def create_chatbot_sandbox_state() -> ChatbotSandboxState:
"enable_sandbox": False,
"sandbox_environment": None,
"sandbox_instruction": None,
"code_to_execute":"",
"enabled_round": 0
}

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

code, code_language, is_web_page = extract_result

# validate whether code to execute has been updated.
previous_code = sandbox_state.get('code_to_execute', '')
if previous_code == code:
print("Code has not changed. Skipping execution.")
yield (
gr.skip(),
gr.skip(),
gr.skip()
)
return

sandbox_state['code_to_execute'] = code

if code_language == 'tsx':
code_language = 'typescript'
code_language = code_language.lower() if code_language and code_language.lower(
Expand Down

0 comments on commit 68a134a

Please sign in to comment.