diff --git a/gradio_ui.py b/gradio_ui.py index cec96e2..db4cade 100644 --- a/gradio_ui.py +++ b/gradio_ui.py @@ -104,10 +104,10 @@ def stop_eval(): if current_process and current_process.poll() is None: try: pgid = os.getpgid(current_process.pid) - os.killpg(pgid, signal.SIGINT) + os.killpg(pgid, signal.SIGINT) # ✅ 优雅终止 time.sleep(2) if current_process.poll() is None: - os.killpg(pgid, signal.SIGKILL) + os.killpg(pgid, signal.SIGKILL) # ❗ 强制终止 return "[✅ 已发送终止信号 (SIGINT → SIGKILL fallback)]\n" except Exception as e: return f"[❌ 终止失败: {e}]\n" @@ -117,6 +117,7 @@ def stop_eval(): return "[⚠️ 无活动 evalscope 进程]\n" + # ---------------- Run/Stop 控制器 ---------------- def toggle_run( inputs, native, other, output_choices, @@ -128,6 +129,12 @@ def toggle_run( is_running ): global should_stop + + if not inputs: + msg = "[❌ 错误] 必须至少选择一个输入源(API、本地、基准或自定义)才能开始运行。\n" + yield msg, False, gr.update(value="Run Evaluation") + return + if not is_running: should_stop = False yield from run_eval( @@ -143,12 +150,6 @@ def toggle_run( yield msg, False, gr.update(value="Run Evaluation") -# ---------------- 禁用按钮逻辑 ---------------- -def update_button_enable(inputs, outputs): - enabled = bool(inputs or outputs) - return gr.update(interactive=enabled) - - # ---------------- 互斥逻辑 ---------------- def enforce_input_exclusive_and_toggle_fields(selected): order = ["API Models", "Local Models", "Benchmarks", "Custom Datasets"] @@ -168,7 +169,9 @@ def enforce_input_exclusive_and_toggle_fields(selected): final_list = [itm for itm in order if itm in final_sel] input_update = gr.update() if list(selected) == final_list else gr.update(value=final_list) - api_field_update = gr.update(visible="API Models" in final_sel) + + show_api_fields = "API Models" in final_sel + api_field_update = gr.update(visible=show_api_fields) # ✅ 正确 return input_update, api_field_update @@ -177,6 +180,7 @@ def enforce_input_exclusive_and_toggle_fields(selected): with gr.Blocks(title="EvalScope 全功能界面") as demo: is_running = gr.State(value=False) + # ===== 输入源 ===== with gr.Group(): with gr.Row(): input_choices = gr.CheckboxGroup( @@ -185,9 +189,17 @@ with gr.Blocks(title="EvalScope 全功能界面") as demo: interactive=True ) + # ===== API 地址 & 运行参数(统一控制显示) ===== with gr.Column(visible=False) as api_fields: - api_url_input = gr.Textbox(label="API 地址", placeholder="https://api.example.com/v1/chat") - api_token_input = gr.Textbox(label="Token 密钥", type="password", placeholder="sk-xxx") + api_url_input = gr.Textbox( + label="API 地址", + placeholder="https://api.example.com/v1/chat" + ) + api_token_input = gr.Textbox( + label="Token 密钥", + type="password", + placeholder="sk-xxx" + ) with gr.Accordion("运行参数(可选修改)", open=False): with gr.Row(): api_provider_dropdown = gr.Dropdown( @@ -205,13 +217,29 @@ with gr.Blocks(title="EvalScope 全功能界面") as demo: placeholder="e.g. my-llm-7b" ) with gr.Row(): - max_tokens_slider = gr.Slider("Max Tokens (--max-tokens)", 256, 8192, 256, value=1024) - min_tokens_slider = gr.Slider("Min Tokens (--min-tokens)", 0, 4096, 64, value=1024) + max_tokens_slider = gr.Slider( + label="Max Tokens (--max-tokens)", + minimum=256, maximum=8192, step=256, value=1024 + ) + min_tokens_slider = gr.Slider( + label="Min Tokens (--min-tokens)", + minimum=0, maximum=4096, step=64, value=1024 + ) with gr.Row(): - parallel_slider = gr.Slider("并发请求数 (--parallel)", 1, 16, 1, value=1) - num_req_slider = gr.Slider("请求条数 (--number)", 1, 1000, 1, value=100) - max_prompt_len_slider = gr.Slider("最大 Prompt 长度 (--max-prompt-length)", 2048, 32768, 512, value=15360) + parallel_slider = gr.Slider( + label="并发请求数 (--parallel)", + minimum=1, maximum=16, step=1, value=1 + ) + num_req_slider = gr.Slider( + label="请求条数 (--number)", + minimum=1, maximum=1000, step=1, value=100 + ) + max_prompt_len_slider = gr.Slider( + label="最大 Prompt 长度 (--max-prompt-length)", + minimum=2048, maximum=32768, step=512, value=15360 + ) + # ===== 本地/外部组件 ===== with gr.Row(): with gr.Column(): native_choices = gr.CheckboxGroup( @@ -224,13 +252,14 @@ with gr.Blocks(title="EvalScope 全功能界面") as demo: choices=["OpenCompass", "VLMEvalKit", "RAGAS", "MTEB/CMTEB"] ) + # ===== 输出形式 ===== output_choices = gr.CheckboxGroup( label="输出形式", choices=["Evaluation Report", "Gradio", "WandB", "Swanlab"] ) - run_button = gr.Button("Run Evaluation", interactive=False) - + # ===== 控制按钮 & 日志 ===== + run_button = gr.Button("Run Evaluation") output_text = gr.TextArea( label="执行结果", lines=20, @@ -238,22 +267,11 @@ with gr.Blocks(title="EvalScope 全功能界面") as demo: show_copy_button=True ) + # ===== 绑定事件 ===== input_choices.change( fn=enforce_input_exclusive_and_toggle_fields, inputs=input_choices, - outputs=[input_choices, api_fields] - ) - - input_choices.change( - fn=update_button_enable, - inputs=[input_choices, output_choices], - outputs=run_button - ) - - output_choices.change( - fn=update_button_enable, - inputs=[input_choices, output_choices], - outputs=run_button + outputs=[input_choices, api_fields] # ✅ 只输出这两个 ) run_button.click(