diff --git a/fastchat/serve/gradio_web_server.py b/fastchat/serve/gradio_web_server.py index e12284279..0b9755d4d 100644 --- a/fastchat/serve/gradio_web_server.py +++ b/fastchat/serve/gradio_web_server.py @@ -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): @@ -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] diff --git a/fastchat/serve/sandbox/code_runner.py b/fastchat/serve/sandbox/code_runner.py index 243359102..1d8eea761 100644 --- a/fastchat/serve/sandbox/code_runner.py +++ b/fastchat/serve/sandbox/code_runner.py @@ -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: @@ -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 } @@ -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(