diff --git a/gradio_ui.py b/gradio_ui.py index 406d884..3a322c9 100644 --- a/gradio_ui.py +++ b/gradio_ui.py @@ -24,7 +24,7 @@ def run_eval(inputs, native, other, outputs, api_url, api_token): ] full_output = f"[Eval Started @ {timestamp}]\n" - yield full_output + yield full_output, True, gr.update(value="Stop Evaluation") try: current_process = subprocess.Popen( @@ -32,17 +32,17 @@ def run_eval(inputs, native, other, outputs, api_url, api_token): ) for line in current_process.stdout: full_output += line - yield full_output + yield full_output, True, gr.update(value="Stop Evaluation") current_process.stdout.close() current_process.wait() except Exception as e: full_output += f"[Error] {str(e)}\n" - yield full_output + yield full_output, False, gr.update(value="Run Evaluation") finally: current_process = None full_output += "[Eval Finished]\n" - yield full_output + yield full_output, False, gr.update(value="Run Evaluation") # 停止当前 evalscope 子进程 def stop_eval(): @@ -53,18 +53,15 @@ def stop_eval(): return "[Stopped by user]\n" return "[No active process]\n" -# ✅ 修改后的 toggle_run,必须是 generator,不能 return,要 yield +# Run/Stop 按钮控制器(必须是 generator) def toggle_run(inputs, native, other, outputs, api_url, api_token, is_running): if not is_running: - # 运行任务并切换按钮为 Stop yield from run_eval(inputs, native, other, outputs, api_url, api_token) - yield "", False, gr.update(value="Run Evaluation") # 运行结束后重置按钮 else: - # 用户点击 Stop,终止子进程 msg = stop_eval() yield msg, False, gr.update(value="Run Evaluation") -# 控制输入选项互斥逻辑 +# 控制输入互斥逻辑 def enforce_input_exclusive_and_toggle_fields(selected): group1 = {"API Models", "Local Models"} group2 = {"Benchmarks", "Custom Datasets"} @@ -87,7 +84,7 @@ def enforce_input_exclusive_and_toggle_fields(selected): gr.Row.update(visible=show_api_fields) ) -# 构建 Gradio 界面 +# 构建 Gradio UI with gr.Blocks(title="EvalScope 全功能界面") as demo: is_running = gr.State(value=False) # 当前运行状态 @@ -131,10 +128,12 @@ with gr.Blocks(title="EvalScope 全功能界面") as demo: outputs=[input_choices, api_fields] ) - # ✅ 修改后的绑定,支持 Run / Stop 切换 run_button.click( fn=toggle_run, - inputs=[input_choices, native_choices, other_choices, output_choices, api_url_input, api_token_input, is_running], + inputs=[ + input_choices, native_choices, other_choices, + output_choices, api_url_input, api_token_input, is_running + ], outputs=[output_text, is_running, run_button], show_progress=True )