Skip to content

Commit

Permalink
Merge pull request #16 from BigComputer-Project/zoey/fix_checkbox
Browse files Browse the repository at this point in the history
Fix slow response of checkbox
  • Loading branch information
Zoeyyhc authored Dec 11, 2024
2 parents 7435ffb + c94fd68 commit 36cb8af
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
13 changes: 6 additions & 7 deletions fastchat/serve/gradio_block_arena_anony.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
get_model_description_md,
)
from fastchat.serve.remote_logger import get_remote_logger
from fastchat.serve.sandbox.code_runner import DEFAULT_SANDBOX_INSTRUCTIONS, SUPPORTED_SANDBOX_ENVIRONMENTS, create_chatbot_sandbox_state, on_click_run_code, update_sandbox_config
from fastchat.serve.sandbox.code_runner import DEFAULT_SANDBOX_INSTRUCTIONS, SUPPORTED_SANDBOX_ENVIRONMENTS, create_chatbot_sandbox_state, on_click_run_code, update_sandbox_config,update_visibility
from fastchat.utils import (
build_logger,
moderation_filter,
Expand Down Expand Up @@ -615,10 +615,14 @@ def build_side_by_side_ui_anony(models):

# update sandbox global config
enable_sandbox_checkbox.change(
fn=update_visibility,
inputs=[enable_sandbox_checkbox],
outputs=hidden_components
).then(
fn=lambda enable, env: "" if not enable else DEFAULT_SANDBOX_INSTRUCTIONS.get(env, ""),
inputs=[enable_sandbox_checkbox, sandbox_env_choice],
outputs=[sandbox_instruction_textarea]
).then(
).then(
fn=update_sandbox_config,
inputs=[
enable_sandbox_checkbox,
Expand All @@ -627,13 +631,8 @@ def build_side_by_side_ui_anony(models):
*sandbox_states
],
outputs=[*sandbox_states]
).then(
fn=lambda enable: [gr.update(visible=enable) for _ in hidden_components],
inputs=[enable_sandbox_checkbox],
outputs=hidden_components
)


with gr.Row():
textbox = gr.Textbox(
show_label=False,
Expand Down
10 changes: 5 additions & 5 deletions fastchat/serve/gradio_block_arena_named.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
get_model_description_md,
)
from fastchat.serve.remote_logger import get_remote_logger
from fastchat.serve.sandbox.code_runner import DEFAULT_SANDBOX_INSTRUCTIONS, SUPPORTED_SANDBOX_ENVIRONMENTS, create_chatbot_sandbox_state, on_click_run_code, update_sandbox_config
from fastchat.serve.sandbox.code_runner import DEFAULT_SANDBOX_INSTRUCTIONS, SUPPORTED_SANDBOX_ENVIRONMENTS, create_chatbot_sandbox_state, on_click_run_code, update_sandbox_config, update_visibility
from fastchat.utils import (
build_logger,
moderation_filter,
Expand Down Expand Up @@ -486,6 +486,10 @@ def build_side_by_side_ui_named(models):

# update sandbox global config
enable_sandbox_checkbox.change(
fn=update_visibility,
inputs=[enable_sandbox_checkbox],
outputs=hidden_components
).then(
fn=lambda enable, env: "" if not enable else DEFAULT_SANDBOX_INSTRUCTIONS.get(env, ""),
inputs=[enable_sandbox_checkbox, sandbox_env_choice],
outputs=[sandbox_instruction_textarea]
Expand All @@ -498,10 +502,6 @@ def build_side_by_side_ui_named(models):
*sandbox_states
],
outputs=[*sandbox_states]
).then(
fn=lambda enable: [gr.update(visible=enable) for _ in hidden_components],
inputs=[enable_sandbox_checkbox],
outputs=hidden_components
)

with gr.Row():
Expand Down
35 changes: 14 additions & 21 deletions fastchat/serve/gradio_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from fastchat.serve.api_provider import get_api_provider_stream_iter
from fastchat.serve.gradio_global_state import Context
from fastchat.serve.remote_logger import get_remote_logger
from fastchat.serve.sandbox.code_runner import RUN_CODE_BUTTON_HTML, ChatbotSandboxState, DEFAULT_SANDBOX_INSTRUCTIONS, SUPPORTED_SANDBOX_ENVIRONMENTS, create_chatbot_sandbox_state, on_click_run_code, update_sandbox_config_single_model
from fastchat.serve.sandbox.code_runner import RUN_CODE_BUTTON_HTML, ChatbotSandboxState, DEFAULT_SANDBOX_INSTRUCTIONS, SUPPORTED_SANDBOX_ENVIRONMENTS, create_chatbot_sandbox_state, on_click_run_code, update_sandbox_config_single_model, update_visibility_for_single_model
from fastchat.utils import (
build_logger,
get_window_url_params_js,
Expand Down Expand Up @@ -830,7 +830,6 @@ def get_model_description_md(models):
ct += 1
return model_description_md


def build_about():
about_markdown = """
# About Us
Expand Down Expand Up @@ -929,8 +928,8 @@ def build_single_model_ui(models, add_promotion_links=False):
gr.Code, # sandbox_code
] | None] = []

sandbox_ui_code_group = gr.Group(visible=False)
with sandbox_ui_code_group:
sandbox_group = gr.Group(visible=False)
with sandbox_group:
sandbox_column = gr.Column(visible=False,scale=1)
with sandbox_column:
sandbox_state = gr.State(create_chatbot_sandbox_state())
Expand Down Expand Up @@ -986,6 +985,10 @@ def build_single_model_ui(models, add_promotion_links=False):
value='',
visible=False
)

hidden_components = []
hidden_components.extend([sandbox_group,sandbox_column,sandbox_title,sandbox_code_tab,sandbox_output_tab,sandbox_env_choice,sandbox_instruction_accordion,sandbox_instruction_textarea])

sandbox_env_choice.change(
fn=lambda env, enable: "" if not enable else DEFAULT_SANDBOX_INSTRUCTIONS[env],
inputs=[sandbox_env_choice, enable_sandbox_checkbox],
Expand All @@ -1011,32 +1014,22 @@ def build_single_model_ui(models, add_promotion_links=False):
outputs=[sandbox_state]
)
# update sandbox global config
enable_sandbox_checkbox.change(
enable_sandbox_checkbox.change (
fn=update_visibility_for_single_model,
inputs=[enable_sandbox_checkbox],
outputs=hidden_components
).then(
fn=lambda enable, env: "" if not enable else DEFAULT_SANDBOX_INSTRUCTIONS.get(env, ""),
inputs=[enable_sandbox_checkbox, sandbox_env_choice],
outputs=[sandbox_instruction_textarea]
).then(
) .then(
fn=update_sandbox_config_single_model,
inputs=[
enable_sandbox_checkbox,
sandbox_env_choice,
sandbox_instruction_textarea,
sandbox_state
],
sandbox_state],
outputs=[sandbox_state]
).then(
fn=lambda enable: (
gr.update(visible=enable),
gr.update(visible=enable),
gr.update(visible=enable),
gr.update(visible=enable),
gr.update(visible=enable),
gr.update(visible=enable),
gr.update(visible=enable),
gr.update(visible=enable),
),
inputs=[enable_sandbox_checkbox],
outputs=[sandbox_ui_code_group,sandbox_column,sandbox_title,sandbox_code_tab,sandbox_output_tab,sandbox_env_choice,sandbox_instruction_accordion,sandbox_instruction_textarea]
)

with gr.Row():
Expand Down
5 changes: 5 additions & 0 deletions fastchat/serve/sandbox/code_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ def update_sandbox_config_single_model(

return state

def update_visibility(visible):
return [gr.update(visible=visible)] * 13

def update_visibility_for_single_model(visible):
return [gr.update(visible=visible) ] * 8

def extract_code_from_markdown(message: str) -> tuple[str, str, bool] | None:
'''
Expand Down

0 comments on commit 36cb8af

Please sign in to comment.