Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 16,545 Bytes
bae4131 3119795 e3a07b7 133c6d8 ea047ad 67741f2 d1ed69b ea047ad fdfafe5 3adea5e bae4131 ea047ad 133c6d8 bae4131 570d85c ea047ad fdfafe5 bae4131 ea047ad 6454c0e e3a07b7 2d54755 e3a07b7 64a657c 2d54755 e3a07b7 3119795 133c6d8 ea047ad 50871c5 3119795 e3a07b7 089a447 e3a07b7 ea047ad 8c14d95 ea047ad 50871c5 7e88e41 ea047ad 8c14d95 7ccf9d4 133c6d8 c471c3c 133c6d8 c471c3c 133c6d8 974f602 ea047ad 089a447 7e88e41 133c6d8 089a447 bae4131 ea047ad 570d85c ea047ad 8c14d95 25580aa 8c14d95 50871c5 8c14d95 50871c5 133c6d8 25580aa ea047ad 25580aa ea047ad 25580aa ea047ad 25580aa 089a447 ea047ad 8c14d95 50871c5 ea047ad 50871c5 bae4131 50871c5 bae4131 50871c5 67741f2 8c14d95 bae4131 089a447 3d76e98 23510fc 3d76e98 4a9b060 3d76e98 7ccf9d4 3d76e98 7ccf9d4 23510fc 089a447 d50990e 7ccf9d4 bae4131 ea047ad 9562cba 67741f2 9562cba 8943d1f 67741f2 8943d1f 67741f2 9562cba 6f2e0a9 9562cba ea047ad 67741f2 9562cba 67741f2 ea047ad 8943d1f ea047ad 9562cba 8943d1f 9562cba 8943d1f 9562cba ea047ad 9562cba 8943d1f ea047ad 8943d1f ea047ad 8943d1f 67741f2 9562cba 67741f2 bae4131 50871c5 c869604 4cba6f0 463fca1 4cba6f0 463fca1 4cba6f0 463fca1 4cba6f0 8cc9490 aaaafe3 8cc9490 c869604 fdfafe5 78afa9e fdfafe5 78afa9e 8943d1f fdfafe5 78afa9e c869604 d50990e c869604 133c6d8 e3a07b7 089a447 d50990e 54fa655 fdfafe5 d50990e 54fa655 be6a58f 54fa655 ea047ad 54fa655 d50990e 54fa655 d50990e 54fa655 ea047ad 54fa655 fdfafe5 ea047ad 54fa655 ea047ad 54fa655 3adea5e 54fa655 3adea5e 54fa655 3adea5e 54fa655 3adea5e 54fa655 3adea5e 54fa655 3adea5e 54fa655 ea047ad 54fa655 fdfafe5 54fa655 fdfafe5 54fa655 570d85c 54fa655 9562cba 54fa655 fdfafe5 9562cba 78afa9e 9562cba 67741f2 aaaafe3 3adea5e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
import os
import sys
import time
import uuid
import asyncio
from pathlib import Path
from loguru import logger
import gradio as gr
from datasets import load_dataset
from huggingface_hub import HfApi, whoami
from yourbench_space import PATH
from yourbench_space.utils import (
STAGES,
SubprocessManagerGroup,
save_files,
update_dataset,
map_stage_names,
is_running_locally,
on_generation_succsess,
)
from yourbench_space.config import generate_and_save_config
from yourbench_space.evaluation import run_evaluations, create_eval_file
project_description = """
# π YourBench
### Dynamic Benchmark Generation from Your Documents
- Create zero-shot benchmarks from your documents β no manual labeling
- Evaluate top open models and publish a leaderboard in one click
- Run locally or explore the [source on GitHub](https://github.com/huggingface/yourbench)
β οΈ **Important:** This app uses your Hugging Face token for inference and uploads β you are responsible for any usage costs
Built with π€ by the [Hugging Face OpenEvals team](https://huggingface.co/OpenEvals)
"""
logger.remove()
logger.add(sys.stderr, level="INFO")
# Global to store all managers per session
MANAGERS = SubprocessManagerGroup()
USER_ID_SESSION_MAP: dict[str, str] = {}
docs_path = Path(__file__).parent / "docs.md"
citation_content = (
docs_path.read_text().split("# Citation")[-1].strip()
if docs_path.exists()
else "# Citation\n\nDocumentation file not found."
)
def generate_and_return(hf_org, hf_dataset_name, session_state: gr.State):
manager = MANAGERS.get(session_state)
if manager is None: # should not be possible
return (
"β Config generation failed",
gr.update(visible=False, interactive=False),
)
session_uid = session_state.value
config_path = generate_and_save_config(hf_org, hf_dataset_name, session_uid, manager.config_path)
for _ in range(5):
time.sleep(0.5)
if config_path.exists():
gr.Success("β
Config generated successfully!")
return (
"β
Config saved successfully!",
gr.update(value=str(config_path), visible=True, interactive=True),
)
gr.Error("Failed to generate config")
return (
"β Config generation failed",
gr.update(visible=False, interactive=False),
)
final_dataset = None
def update_process_status(session_state: gr.State):
"""Update process status and include exit details if process has terminated"""
if session_state is None:
return gr.update(value=False, label="Not running")
manager = MANAGERS.get(session_state.value)
if manager is None:
return gr.update(value=False, label="Not running")
is_running = manager.is_running()
if not is_running:
exit_code, exit_reason = manager.get_exit_details()
status_text = (
f"Process Status: Stopped - {exit_reason}, exit code - {exit_code}"
if exit_reason
else "Process Status: Stopped"
)
return gr.update(value=False, label=status_text)
return gr.update(value=True, label="Process Status: Running")
def prepare_task(session_uid: str, oauth_token: gr.OAuthToken | None, hf_dataset_name: str, _=None):
if oauth_token is None and not is_running_locally():
gr.Warning("You need to log in to use this Space")
return
new_env = os.environ.copy()
if oauth_token:
new_env["HF_TOKEN"] = oauth_token.token
new_env["DATASET_PREFIX"] = hf_dataset_name
MANAGERS.start_process(session_uid, custom_env=new_env)
def update_hf_org_dropdown(oauth_token: gr.OAuthToken | None):
if oauth_token is None:
return gr.Dropdown([], label="Organization")
try:
user_info = whoami(oauth_token.token)
org_names = [org["name"] for org in user_info.get("orgs", [])]
user_name = user_info.get("name", "Unknown User")
org_names.insert(0, user_name)
return gr.Dropdown(org_names, value=user_name, label="Organization")
except Exception as e:
return gr.Dropdown([], label="Organization")
def switch_to_run_generation_tab():
return gr.Tabs(selected=1)
def enable_button(files):
return gr.update(interactive=bool(files))
def run_evaluation_pipeline(oauth_token: gr.OAuthToken | None, org_name, eval_name, config_name="lighteval"):
eval_ds_name = f"{org_name}/{eval_name}"
repo_id = f"{org_name}/leaderboard_yourbench_{eval_ds_name.replace('/', '_')}"
folder_path = str(Path(PATH) / "yourbench_space" / "leaderboard_space")
new_env = os.environ.copy()
if oauth_token:
hf_token = oauth_token.token
new_env["HF_TOKEN"] = hf_token
else:
hf_token = os.environ.get("HF_TOKEN")
try:
load_dataset(eval_ds_name, name=config_name, streaming=True, token=hf_token)
except Exception as e:
logger.error(f"Failed to load dataset '{eval_ds_name}': {e}")
return "β Failed: Dataset loading error"
try:
create_eval_file(eval_ds_name)
status = asyncio.run(run_evaluations(org=org_name, eval_ds_name=eval_ds_name, custom_env=new_env))
except Exception as e:
logger.error(f"Evaluation error: {e}")
return f"β Failed: Evaluation error\n{e}"
api = HfApi()
space_was_regenerated = False
try:
api.create_repo(
repo_id=repo_id,
repo_type="space",
space_sdk="gradio",
token=hf_token,
)
except Exception as e:
if "409" in str(e) and "already created this space repo" in str(e):
logger.info(f"Space '{repo_id}' already exists. Deleting and regenerating it.")
try:
api.delete_repo(repo_id=repo_id, repo_type="space", token=hf_token)
api.create_repo(
repo_id=repo_id,
repo_type="space",
space_sdk="gradio",
token=hf_token,
)
space_was_regenerated = True
except Exception as delete_err:
logger.error(f"Failed to delete and recreate space '{repo_id}': {delete_err}")
return f"β
Evaluation succeeded\nβ Failed: Could not recreate space\n{delete_err}"
else:
logger.error(f"Space creation error: {e}")
return f"β
Evaluation succeeded\nβ Failed: Space creation error\n{e}"
try:
api.upload_folder(
repo_id=repo_id,
repo_type="space",
folder_path=folder_path,
token=hf_token,
)
api.add_space_secret(
repo_id=repo_id,
key="HF_TOKEN",
value=hf_token,
token=hf_token,
)
api.add_space_variable(repo_id=repo_id, key="TASK", value=eval_ds_name, token=hf_token)
api.add_space_variable(repo_id=repo_id, key="ORG_NAME", value=org_name, token=hf_token)
except Exception as e:
logger.error(f"Failed during space setup: {e}")
return f"β
Evaluation succeeded\nβ Failed: Space setup error\n{e}"
if space_was_regenerated:
return f"β
Evaluation succeeded\nπ Space '{repo_id}' was regenerated successfully"
return f"β
Evaluation and Space creation completed successfully for: {repo_id}"
def init_session(profile: gr.OAuthProfile | None):
"""Update session on load"""
if is_running_locally():
username = "local"
elif profile:
username = profile.username
else:
username = None
local_uuid = USER_ID_SESSION_MAP.get(username, str(uuid.uuid4()))
if manager := MANAGERS.get(local_uuid):
if manager.is_running():
logger.info(f"Found existing running session for {local_uuid}, restoring")
return gr.State(local_uuid, delete_callback=lambda uid: MANAGERS.remove(uid))
else:
logger.info(f"Found existing stale session for {local_uuid}, starting new")
MANAGERS.remove(local_uuid)
local_uuid = str(uuid.uuid4())
if username:
USER_ID_SESSION_MAP[username] = local_uuid
MANAGERS.create(local_uuid)
logger.info(f"Started session for {local_uuid}")
return gr.State(local_uuid, delete_callback=lambda uid: MANAGERS.remove(uid))
btn_launch_evals = gr.Button(
"π Launch Evaluation",
visible=True,
interactive=True, # Start non-interactive
variant="primary",
)
with gr.Blocks(theme=gr.themes.Default()) as app:
session_state = gr.State()
gr.Markdown(project_description)
with gr.Tabs() as tabs:
with gr.Tab("Choose Documents & Settings", id=0):
with gr.Column():
gr.Markdown("### π Choose your documents and settings")
gr.Markdown(
"Upload your source documents that will form the knowledge base for your benchmark. Set a Hugging Face organization and dataset name."
)
gr.Markdown(
"This step also generates a config file for running the benchmark pipeline. You can download it to run YourBench locally."
)
with gr.Row():
with gr.Accordion("Hugging Face Settings"):
login_btn = gr.LoginButton()
hf_org_dropdown = gr.Dropdown(choices=[], label="Organization", allow_custom_value=True)
app.load(update_hf_org_dropdown, inputs=None, outputs=hf_org_dropdown)
hf_dataset_name = gr.Textbox(
label="Dataset name",
value="yourbench",
info="Name of your new evaluation dataset",
)
with gr.Accordion("Upload Files"):
file_input = gr.File(
label="Upload text files",
file_count="multiple",
file_types=[".txt", ".md", ".html", ".pdf"],
)
output = gr.Textbox(label="Log")
file_input.upload(
save_files,
inputs=[session_state, file_input],
outputs=output,
)
delete_button = gr.Button("Delete Uploaded Files", visible=False)
preview_button = gr.Button("Generate New Config", interactive=False)
log_message = gr.Textbox(label="Log Message", visible=True)
download_button = gr.File(label="Download Config", visible=False, interactive=False)
file_input.change(
lambda files: gr.update(visible=bool(files)),
inputs=file_input,
outputs=delete_button,
)
file_input.change(enable_button, inputs=file_input, outputs=preview_button)
def clean_and_confirm(uid):
MANAGERS.clean_workdir(uid)
return (
"ποΈ All uploaded files have been deleted!",
gr.update(value=None),
gr.update(interactive=False),
)
delete_button.click(
clean_and_confirm,
inputs=session_state,
outputs=[output, file_input, preview_button],
)
preview_button.click(
generate_and_return,
inputs=[hf_org_dropdown, hf_dataset_name, session_state],
outputs=[log_message, download_button],
)
preview_button.click(
switch_to_run_generation_tab,
inputs=None,
outputs=tabs,
)
with gr.Tab("Run Benchmark Pipeline", id=1):
with gr.Column():
gr.Markdown("### βοΈ Run the benchmark generation pipeline")
gr.Markdown(
"Start the pipeline to process documents, generate questions, and build the private evaluation dataset. Watch logs, track progress, and preview the results."
)
with gr.Row():
start_button = gr.Button("Start Task")
stop_button = gr.Button("Stop Task")
kill_button = gr.Button("Kill Task")
start_button.click(prepare_task, inputs=[session_state, login_btn, hf_dataset_name])
stop_button.click(MANAGERS.stop_process, inputs=session_state)
kill_button.click(MANAGERS.kill_process, inputs=session_state)
process_status = gr.Checkbox(label="Process Status", interactive=False)
status_timer = gr.Timer(2.0, active=True)
status_timer.tick(update_process_status, inputs=session_state, outputs=process_status)
with gr.Row():
with gr.Accordion("Stages", open=True):
stages_table = gr.CheckboxGroup(
choices=map_stage_names(STAGES),
value=[],
label="Pipeline Stages Completed",
container=False,
interactive=False,
)
with gr.Row():
with gr.Column():
with gr.Accordion("Log Output", open=True):
log_output = gr.Code(language=None, lines=20, interactive=False)
with gr.Column():
with gr.Accordion("Ingestion Preview"):
ingestion_df = gr.DataFrame()
with gr.Accordion("Summarization Preview"):
summarization_df = gr.DataFrame()
with gr.Accordion("Single Shot Preview"):
single_shot_df = gr.DataFrame()
with gr.Accordion("Multi Hop Preview"):
multi_hop_df = gr.DataFrame()
with gr.Accordion("Lighteval Preview"):
lighteval_df = gr.DataFrame()
stages_table.change(
update_dataset,
inputs=[stages_table, hf_org_dropdown, hf_dataset_name],
outputs=[ingestion_df, summarization_df, single_shot_df, multi_hop_df, lighteval_df],
)
stages_table.change(
on_generation_succsess,
inputs=stages_table,
outputs=[tabs, btn_launch_evals],
)
# TODO: this timer should only be active when the second tab is passed to active for the first time
log_timer = gr.Timer(1.0, active=True)
log_timer.tick(
MANAGERS.read_and_get_output,
inputs=session_state,
outputs=[log_output, stages_table],
)
with gr.Tab("Evaluate Models on Benchmark", id=2):
with gr.Column():
gr.Markdown("### π§ͺ Evaluate models on your benchmark")
gr.Markdown(
"Runs the evaluation with [Lighteval](https://github.com/huggingface/lighteval) on the resulted dataset using 5+ open models, then deploys a leaderboard as a Hugging Face Space under your org."
)
with gr.Row():
with gr.Column():
btn_launch_evals.render()
with gr.Column():
clear_status_btn = gr.Button("Clear", variant="secondary")
with gr.Accordion("Evaluation Log", open=True):
eval_status = gr.Textbox(label="", lines=6, interactive=False, show_label=False)
btn_launch_evals.click(
run_evaluation_pipeline,
[hf_org_dropdown, hf_dataset_name, gr.State("lighteval")],
eval_status,
)
clear_status_btn.click(lambda: "", outputs=eval_status)
app.load(init_session, outputs=session_state)
app.launch(allowed_paths=[PATH])
|