Spaces:
Running
Running
terryyz commited on
Commit ยท
48a3a23
1
Parent(s): c833d3f
fix
Browse files593 unmodified lines59459559659759859960048 unmodified lines649650651652653654173 unmodified lines82882983083183283392 unmodified lines9269279289299309317 unmodified lines93994094194294394427 unmodified lines97297397497597697797811 unmodified lines990991992993994995996189 unmodified lines118611871188118911901191119211932 unmodified lines11961197119811991200120112021203120412051206120712081209121012111212121312142 unmodified lines121712181219122012211222122312241225122612271228290 unmodified lines15191520152115221523152438 unmodified lines15631564156515661567156840 unmodified lines16091610161116121613161437 unmodified lines16521653165416551656165740 unmodified lines16981699170017011702170340 unmodified lines17441745174617471748174929 unmodified lines177917801781178217831784121 unmodified lines190619071908190919101911593 unmodified lineschat_stats_a = f"**Conversation:** {turn_count_a} turns | **Total Messages:** {len(state0['messages']) if state0 else 0}"chat_stats_b = f"**Conversation:** {turn_count_b} turns | **Total Messages:** {len(state1['messages']) if state1 else 0}"return state0, state1, chat0, chat1, response0, response1, code0, code1, env0, env1, sandbox_state0, sandbox_state1, sandbox_output0, sandbox_output1, sandbox_component_update0, sandbox_component_update1, chat_stats_a, chat_stats_b, sandbox_view_a, sandbox_view_bdef format_chat_history(messages):"""Format messages for chat display with turn numbers"""48 unmodified lines"**Conversation:** 0 turns | **Total Messages:** 0", # chat_stats_b"", # sandbox_view_a (duplicate)"", # sandbox_view_b (duplicate)f"**Model A:** {model_a}", # model_display_af"**Model B:** {model_b}", # model_display_b"", # text_input173 unmodified lines"", # sandbox_view_b (empty)state0, # state0_varstate1, # state1_vartext, # Keep original text inputf"**Model A:** {model_a}", # Update model display Af"**Model B:** {model_b}", # Update model display B92 unmodified linessandbox_view_b, # sandbox_view_bstate0, # state0_varstate1, # state1_vartext, # Keep original text inputf"**Model A:** {model_a}", # Update model display Af"**Model B:** {model_b}", # Update model display B7 unmodified lines)def run_sandbox_code(sandbox_state: dict, code: str, install_command: str) -> tuple[str, str, str]:"""Run code in the appropriate sandbox environment"""if not code.strip():27 unmodified linesreturn result['sandbox_url'], "", result['stderr']elif env == SandboxEnvironment.GRADIO:print(f"DEBUG: running gradio sandbox")sandbox_url, sandbox_id, stderr = run_gradio_sandbox(code, install_command, sandbox_state.get('sandbox_id'))sandbox_state['sandbox_id'] = sandbox_idreturn sandbox_url, "", stderr11 unmodified linesreturn sandbox_url, "", stderrelif env == SandboxEnvironment.PYTHON_RUNNER:print(f"DEBUG: running python runner")output, stderr = run_code_interpreter(code, 'python', install_command)return "", output, stderr189 unmodified lines# Model A Sandboxwith gr.Column():gr.Markdown("### Model A Sandbox")with gr.Tabs():with gr.Tab("View"):sandbox_view_a = gr.Markdown("**Sandbox output will appear here automatically**")2 unmodified lineslabel="Model A Sandbox",visible=False,)with gr.Tab("Code"):code_a = gr.Code(label="Extracted Code",language="python",lines=8,interactive=False,)# Model B Sandboxwith gr.Column():gr.Markdown("### Model B Sandbox")with gr.Tabs():with gr.Tab("View"):sandbox_view_b = gr.Markdown("**Sandbox output will appear here automatically**")2 unmodified lineslabel="Model B Sandbox",visible=False,)with gr.Tab("Code"):code_b = gr.Code(label="Extracted Code",language="python",lines=8,interactive=False,)# Vote UI components290 unmodified linesresult[19] if len(result) > 19 else "", # sandbox_view_bnew_state0, # state0_varnew_state1, # state1_vartext, # Keep original text inputf"**Model A:** {model_a}", # Update model display Af"**Model B:** {model_b}", # Update model display B38 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B40 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B37 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B40 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B40 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B29 unmodified lineschat_stats_b, # Reset conversation statistics for model Bsandbox_view_a, # Reset sandbox view for model Asandbox_view_b, # Reset sandbox view for model Bmodel_display_a, # Reset model display Amodel_display_b, # Reset model display Btext_input, # Clear text input121 unmodified lines"", # Clear text_input to preserve examples)# Vote button click handlersfor vote_btn, vote_type in [(vote_left_btn, "left"),593 unmodified lines59459559659759859960060160260360448 unmodified lines653654655656657658659660173 unmodified lines83483583683783883984084192 unmodified lines9349359369379389399409417 unmodified lines94995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999027 unmodified lines10181019102010211022102311 unmodified lines103510361037103810391040189 unmodified lines123012311232123312341235123612372 unmodified lines124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712682 unmodified lines1271127212731274127512761277127812791280128112821283128412851286128712881289129012911292290 unmodified lines1583158415851586158715881589159038 unmodified lines1629163016311632163316341635163640 unmodified lines1677167816791680168116821683168437 unmodified lines1722172317241725172617271728172940 unmodified lines1770177117721773177417751776177740 unmodified lines1818181918201821182218231824182529 unmodified lines1855185618571858185918601861186218631864121 unmodified lines19861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044593 unmodified lineschat_stats_a = f"**Conversation:** {turn_count_a} turns | **Total Messages:** {len(state0['messages']) if state0 else 0}"chat_stats_b = f"**Conversation:** {turn_count_b} turns | **Total Messages:** {len(state1['messages']) if state1 else 0}"# Get install commands from sandbox statesinstall_command0 = sandbox_state0.get('install_command', '') if sandbox_state0 else ''install_command1 = sandbox_state1.get('install_command', '') if sandbox_state1 else ''return state0, state1, chat0, chat1, response0, response1, code0, code1, env0, env1, sandbox_state0, sandbox_state1, sandbox_output0, sandbox_output1, sandbox_component_update0, sandbox_component_update1, chat_stats_a, chat_stats_b, sandbox_view_a, sandbox_view_b, install_command0, install_command1def format_chat_history(messages):"""Format messages for chat display with turn numbers"""48 unmodified lines"**Conversation:** 0 turns | **Total Messages:** 0", # chat_stats_b"", # sandbox_view_a (duplicate)"", # sandbox_view_b (duplicate)"", # install_command_a"", # install_command_bf"**Model A:** {model_a}", # model_display_af"**Model B:** {model_b}", # model_display_b"", # text_input173 unmodified lines"", # sandbox_view_b (empty)state0, # state0_varstate1, # state1_varstate0.get('install_command', ''), # state0_install_commandstate1.get('install_command', ''), # state1_install_commandtext, # Keep original text inputf"**Model A:** {model_a}", # Update model display Af"**Model B:** {model_b}", # Update model display B92 unmodified linessandbox_view_b, # sandbox_view_bstate0, # state0_varstate1, # state1_varstate0.get('install_command', ''), # state0_install_commandstate1.get('install_command', ''), # state1_install_commandtext, # Keep original text inputf"**Model A:** {model_a}", # Update model display Af"**Model B:** {model_b}", # Update model display B7 unmodified lines)def rerun_code_execution(state, current_code: str, current_install_command: str, model_name: str) -> tuple[dict, str, str, str]:"""Re-run code execution for a specific model using the current code and install command from the UI components"""if not state or not state.get("sandbox_state"):return state, "", "", ""sandbox_state = state["sandbox_state"]if not current_code.strip():return state, "", "", "No code to re-run"# Update the sandbox state with the current code and install commandsandbox_state['code_to_execute'] = current_codesandbox_state['install_command'] = current_install_command# Re-run the code execution with the updated code and install commandsandbox_url, sandbox_output, sandbox_error = run_sandbox_code(sandbox_state, current_code, current_install_command)# Update sandbox view with new outputsandbox_view = ""if sandbox_output:sandbox_view += sandbox_outputif sandbox_error:sandbox_view = f"<details closed><summary><strong>๐จ Errors/Warnings</strong></summary>\n\n```\n{sandbox_error}\n```\n\n</details>\n\n" + sandbox_view# Update sandbox component if we have a URLsandbox_component_update = Noneif sandbox_url:sandbox_component_update = gr.update(value=(sandbox_url, True, []),visible=True,key=f"sandbox_{model_name.lower()}_{int(time.time() * 1000)}" # Unique key to force refresh)return state, sandbox_view, sandbox_component_update, sandbox_errordef run_sandbox_code(sandbox_state: dict, code: str, install_command: str) -> tuple[str, str, str]:"""Run code in the appropriate sandbox environment"""if not code.strip():27 unmodified linesreturn result['sandbox_url'], "", result['stderr']elif env == SandboxEnvironment.GRADIO:sandbox_url, sandbox_id, stderr = run_gradio_sandbox(code, install_command, sandbox_state.get('sandbox_id'))sandbox_state['sandbox_id'] = sandbox_idreturn sandbox_url, "", stderr11 unmodified linesreturn sandbox_url, "", stderrelif env == SandboxEnvironment.PYTHON_RUNNER:output, stderr = run_code_interpreter(code, 'python', install_command)return "", output, stderr189 unmodified lines# Model A Sandboxwith gr.Column():gr.Markdown("### Model A Sandbox")with gr.Tabs() as tabs_a:with gr.Tab("View", id=0):sandbox_view_a = gr.Markdown("**Sandbox output will appear here automatically**")2 unmodified lineslabel="Model A Sandbox",visible=False,)with gr.Tab("Code", id=1):code_a = gr.Code(label="Extracted Code",language="python",lines=8,interactive=True,)install_command_a = gr.Textbox(label="Install Command",placeholder="bash command to install dependencies",interactive=True,lines=1,)rerun_code_a_btn = gr.Button("๐ Re-run Code",variant="huggingface",)# Model B Sandboxwith gr.Column():gr.Markdown("### Model B Sandbox")with gr.Tabs() as tabs_b:with gr.Tab("View", id=2):sandbox_view_b = gr.Markdown("**Sandbox output will appear here automatically**")2 unmodified lineslabel="Model B Sandbox",visible=False,)with gr.Tab("Code", id=3):code_b = gr.Code(label="Extracted Code",language="python",lines=8,interactive=True,)install_command_b = gr.Textbox(label="Install Command",placeholder="bash command to install dependencies",interactive=True,lines=1,)rerun_code_b_btn = gr.Button("๐ Re-run Code",size="huggingface")# Vote UI components290 unmodified linesresult[19] if len(result) > 19 else "", # sandbox_view_bnew_state0, # state0_varnew_state1, # state1_varnew_state0.get('install_command', ''), # state0_install_commandnew_state1.get('install_command', ''), # state1_install_commandtext, # Keep original text inputf"**Model A:** {model_a}", # Update model display Af"**Model B:** {model_b}", # Update model display B38 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Binstall_command_a, # Install command for model Ainstall_command_b, # Install command for model Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B40 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Binstall_command_a, # Install command for model Ainstall_command_b, # Install command for model Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B37 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Binstall_command_a, # Install command for model Ainstall_command_b, # Install command for model Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B40 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Binstall_command_a, # Install command for model Ainstall_command_b, # Install command for model Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B40 unmodified linessandbox_view_b, # Sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Binstall_command_a, # Install command for model Ainstall_command_b, # Install command for model Btext_input, # Clear the text input after sendingmodel_display_a, # Update model display Amodel_display_b, # Update model display B29 unmodified lineschat_stats_b, # Reset conversation statistics for model Bsandbox_view_a, # Reset sandbox view for model Asandbox_view_b, # Reset sandbox view for model Bstate0_var, # Updated state for run button Astate1_var, # Updated state for run button Binstall_command_a, # Clear install command for model Ainstall_command_b, # Clear install command for model Bmodel_display_a, # Reset model display Amodel_display_b, # Reset model display Btext_input, # Clear text input121 unmodified lines"", # Clear text_input to preserve examples)# Re-run code button handlersdef rerun_code_a(state0, current_code_a, current_install_command_a):"""Re-run code execution for Model A"""updated_state, sandbox_view, sandbox_component_update, error = rerun_code_execution(state0, current_code_a, current_install_command_a, "A")return (updated_state, # state0_varsandbox_view, # sandbox_view_asandbox_component_update if sandbox_component_update else gr.skip(), # sandbox_component_a)def rerun_code_b(state1, current_code_b, current_install_command_b):"""Re-run code execution for Model B"""updated_state, sandbox_view, sandbox_component_update, error = rerun_code_execution(state1, current_code_b, current_install_command_b, "B")return (updated_state, # state1_varsandbox_view, # sandbox_view_bsandbox_component_update if sandbox_component_update else gr.skip(), # sandbox_component_b)def change_to_view_a():return gr.Tabs(selected=0)def change_to_view_b():return gr.Tabs(selected=2)rerun_code_a_btn.click(fn=change_to_view_a,inputs=[],outputs=[tabs_a]).then(fn=rerun_code_a,inputs=[state0_var, code_a, install_command_a],outputs=[state0_var, # state0_varsandbox_view_a, # sandbox_view_asandbox_component_a, # sandbox_component_a])rerun_code_b_btn.click(fn=change_to_view_b,inputs=[],outputs=[tabs_b]).then(fn=rerun_code_b,inputs=[state1_var, code_b, install_command_b],outputs=[state1_var, # state1_varsandbox_view_b, # sandbox_view_bsandbox_component_b, # sandbox_component_b])# Vote button click handlersfor vote_btn, vote_type in [(vote_left_btn, "left"),
app.py+144-11