This commit is contained in:
hailin 2025-07-08 13:24:51 +08:00
parent ff7ce8c4f0
commit 7484f13400
1 changed files with 37 additions and 8 deletions

View File

@ -2,7 +2,12 @@ import time
import gradio as gr import gradio as gr
import subprocess import subprocess
# 全局变量:当前子进程
current_process = None
# 启动 evalscope 的逻辑(支持 yield 输出)
def run_eval(inputs, native, other, outputs, api_url, api_token): def run_eval(inputs, native, other, outputs, api_url, api_token):
global current_process
timestamp = time.strftime("%Y%m%d-%H%M%S") timestamp = time.strftime("%Y%m%d-%H%M%S")
command = [ command = [
"evalscope", "perf", "evalscope", "perf",
@ -20,21 +25,43 @@ def run_eval(inputs, native, other, outputs, api_url, api_token):
full_output = f"[Eval Started @ {timestamp}]\n" full_output = f"[Eval Started @ {timestamp}]\n"
yield full_output yield full_output
try: try:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1) current_process = subprocess.Popen(
for line in process.stdout: command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1
)
for line in current_process.stdout:
full_output += line full_output += line
yield full_output # ✅ 每次 yield 累积的完整内容 yield full_output
process.stdout.close() current_process.stdout.close()
process.wait() current_process.wait()
except Exception as e: except Exception as e:
full_output += f"[Error] {str(e)}\n" full_output += f"[Error] {str(e)}\n"
yield full_output yield full_output
finally:
current_process = None
full_output += "[Eval Finished]\n" full_output += "[Eval Finished]\n"
yield full_output yield full_output
# 停止当前 evalscope 子进程
def stop_eval():
global current_process
if current_process and current_process.poll() is None:
current_process.terminate()
current_process = None
return "[Stopped by user]\n"
return "[No active process]\n"
# 按钮行为切换函数Run / Stop
def toggle_run(inputs, native, other, outputs, api_url, api_token, is_running):
if not is_running:
return run_eval(inputs, native, other, outputs, api_url, api_token), True, gr.update(value="Stop Evaluation")
else:
msg = stop_eval()
return msg, False, gr.update(value="Run Evaluation")
# 控制输入选项互斥逻辑
def enforce_input_exclusive_and_toggle_fields(selected): def enforce_input_exclusive_and_toggle_fields(selected):
group1 = {"API Models", "Local Models"} group1 = {"API Models", "Local Models"}
group2 = {"Benchmarks", "Custom Datasets"} group2 = {"Benchmarks", "Custom Datasets"}
@ -57,7 +84,9 @@ def enforce_input_exclusive_and_toggle_fields(selected):
gr.Row.update(visible=show_api_fields) gr.Row.update(visible=show_api_fields)
) )
# 构建 Gradio 界面
with gr.Blocks(title="EvalScope 全功能界面") as demo: with gr.Blocks(title="EvalScope 全功能界面") as demo:
is_running = gr.State(value=False) # 当前运行状态
with gr.Group(): with gr.Group():
with gr.Row(): with gr.Row():
input_choices = gr.CheckboxGroup( input_choices = gr.CheckboxGroup(
@ -99,9 +128,9 @@ with gr.Blocks(title="EvalScope 全功能界面") as demo:
) )
run_button.click( run_button.click(
fn=run_eval, fn=toggle_run,
inputs=[input_choices, native_choices, other_choices, output_choices, api_url_input, api_token_input], inputs=[input_choices, native_choices, other_choices, output_choices, api_url_input, api_token_input, is_running],
outputs=output_text, outputs=[output_text, is_running, run_button],
show_progress=True show_progress=True
) )