File size: 33,687 Bytes
b59a5d0 955b6a7 b59a5d0 955b6a7 b59a5d0 955b6a7 b59a5d0 05b75ca b59a5d0 05b75ca b59a5d0 |
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 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 |
"""
Data loader module for loading benchmark results from HuggingFace Dataset.
"""
import json
import logging
from pathlib import Path
from typing import List, Dict, Any, Optional
from datetime import datetime
import pandas as pd
from huggingface_hub import snapshot_download, list_models
logger = logging.getLogger(__name__)
def load_benchmark_data(
dataset_repo: str,
token: Optional[str] = None,
) -> pd.DataFrame:
"""Load benchmark data from HuggingFace Dataset repository.
Args:
dataset_repo: HuggingFace dataset repository ID (e.g., "username/dataset-name")
token: HuggingFace API token (optional, for private datasets)
Returns:
DataFrame containing all benchmark results
"""
if not dataset_repo:
return pd.DataFrame()
try:
# Download the entire repository snapshot
logger.info(f"Downloading dataset snapshot from {dataset_repo}...")
local_dir = snapshot_download(
repo_id=dataset_repo,
repo_type="dataset",
token=token,
)
logger.info(f"Dataset downloaded to {local_dir}")
# Find all JSON files in the downloaded directory
local_path = Path(local_dir)
json_files = list(local_path.rglob("*.json"))
if not json_files:
logger.warning("No JSON files found in dataset")
return pd.DataFrame()
logger.info(f"Found {len(json_files)} JSON files")
# Load all benchmark results
all_results = []
for file_path in json_files:
try:
with open(file_path, "r") as f:
result = json.load(f)
if result:
flattened = flatten_result(result)
all_results.append(flattened)
except Exception as e:
logger.error(f"Error loading {file_path}: {e}")
continue
if not all_results:
return pd.DataFrame()
logger.info(f"Loaded {len(all_results)} benchmark results")
# Convert to DataFrame
df = pd.DataFrame(all_results)
# Enrich with HuggingFace model metadata
df = enrich_with_hf_metadata(df)
# Add first-timer-friendly score
df = add_first_timer_score(df)
# Sort by model name and timestamp
if "modelId" in df.columns and "timestamp" in df.columns:
df = df.sort_values(["modelId", "timestamp"], ascending=[True, False])
return df
except Exception as e:
logger.error(f"Error loading benchmark data: {e}")
return pd.DataFrame()
def flatten_result(result: Dict[str, Any]) -> Dict[str, Any]:
"""Flatten nested benchmark result for display.
The HF Dataset format is already flattened by the bench service,
so we just need to extract the relevant fields.
Args:
result: Raw benchmark result dictionary
Returns:
Flattened dictionary with extracted fields
"""
# Convert timestamp from milliseconds to datetime
timestamp_ms = result.get("timestamp", 0)
timestamp_dt = None
if timestamp_ms:
try:
timestamp_dt = datetime.fromtimestamp(timestamp_ms / 1000)
except (ValueError, OSError):
timestamp_dt = None
# Determine actual status - if there's an error, it should be "failed"
status = result.get("status", "")
if "error" in result:
status = "failed"
flat = {
"id": result.get("id", ""),
"platform": result.get("platform", ""),
"modelId": result.get("modelId", ""),
"task": result.get("task", ""),
"mode": result.get("mode", ""),
"repeats": result.get("repeats", 0),
"batchSize": result.get("batchSize", 0),
"device": result.get("device", ""),
"browser": result.get("browser", ""),
"dtype": result.get("dtype", ""),
"headed": result.get("headed", False),
"status": status,
"timestamp": timestamp_dt,
"runtime": result.get("runtime", ""),
# Initialize metric fields with None (will be filled if metrics exist)
"load_ms_p50": None,
"load_ms_p90": None,
"first_infer_ms_p50": None,
"first_infer_ms_p90": None,
"subsequent_infer_ms_p50": None,
"subsequent_infer_ms_p90": None,
}
# Extract metrics if available (already at top level)
if "metrics" in result:
metrics = result["metrics"]
# Load time
if "load_ms" in metrics and "p50" in metrics["load_ms"]:
flat["load_ms_p50"] = metrics["load_ms"]["p50"]
flat["load_ms_p90"] = metrics["load_ms"]["p90"]
# First inference time
if "first_infer_ms" in metrics and "p50" in metrics["first_infer_ms"]:
flat["first_infer_ms_p50"] = metrics["first_infer_ms"]["p50"]
flat["first_infer_ms_p90"] = metrics["first_infer_ms"]["p90"]
# Subsequent inference time
if "subsequent_infer_ms" in metrics and "p50" in metrics["subsequent_infer_ms"]:
flat["subsequent_infer_ms_p50"] = metrics["subsequent_infer_ms"]["p50"]
flat["subsequent_infer_ms_p90"] = metrics["subsequent_infer_ms"]["p90"]
# Extract environment info (already at top level)
if "environment" in result:
env = result["environment"]
flat["cpuCores"] = env.get("cpuCores", 0)
if "memory" in env:
flat["memory_gb"] = env["memory"].get("deviceMemory", 0)
# Calculate duration
if "completedAt" in result and "startedAt" in result:
flat["duration_s"] = (result["completedAt"] - result["startedAt"]) / 1000
return flat
def enrich_with_hf_metadata(df: pd.DataFrame) -> pd.DataFrame:
"""Enrich benchmark data with HuggingFace model metadata (downloads, likes).
Args:
df: DataFrame containing benchmark results
token: HuggingFace API token (optional)
Returns:
DataFrame with added downloads and likes columns
"""
if df.empty or "modelId" not in df.columns:
return df
# Get unique model IDs
model_ids = df["modelId"].unique().tolist()
# Fetch metadata for all models
model_metadata = {}
logger.info(f"Fetching metadata for {len(model_ids)} models from HuggingFace...")
try:
for model in list_models(filter=["transformers.js"]):
if model.id in model_ids:
model_metadata[model.id] = {
"downloads": model.downloads or 0,
"likes": model.likes or 0,
}
# Break early if we have all models
if len(model_metadata) == len(model_ids):
break
except Exception as e:
logger.error(f"Error fetching HuggingFace metadata: {e}")
# Add metadata to dataframe
df["downloads"] = df["modelId"].map(lambda x: model_metadata.get(x, {}).get("downloads", 0))
df["likes"] = df["modelId"].map(lambda x: model_metadata.get(x, {}).get("likes", 0))
return df
def add_first_timer_score(df: pd.DataFrame) -> pd.DataFrame:
"""Add first-timer-friendly score to all rows in the dataframe.
The score is calculated per task, normalized from 0-100 where:
- Higher score = better for first-timers
- Based on: downloads (25%), likes (15%), load time (30%), inference time (30%)
Args:
df: DataFrame containing benchmark results
Returns:
DataFrame with added 'first_timer_score' column
"""
if df.empty:
return df
# Filter only successful benchmarks
filtered = df[df["status"] == "completed"].copy() if "status" in df.columns else df.copy()
if filtered.empty:
# Add empty score column for failed benchmarks
df["first_timer_score"] = None
return df
# Check if task column exists
if "task" not in filtered.columns:
df["first_timer_score"] = None
return df
# Calculate score per task
for task in filtered["task"].unique():
task_mask = filtered["task"] == task
task_df = filtered[task_mask].copy()
if task_df.empty:
continue
# Normalize metrics within this task (0-1 scale)
# Downloads score (0-1, higher is better)
if "downloads" in task_df.columns:
max_downloads = task_df["downloads"].max()
downloads_score = task_df["downloads"] / max_downloads if max_downloads > 0 else 0
else:
downloads_score = 0
# Likes score (0-1, higher is better)
if "likes" in task_df.columns:
max_likes = task_df["likes"].max()
likes_score = task_df["likes"] / max_likes if max_likes > 0 else 0
else:
likes_score = 0
# Load time score (0-1, lower time is better)
if "load_ms_p50" in task_df.columns:
max_load = task_df["load_ms_p50"].max()
load_score = 1 - (task_df["load_ms_p50"] / max_load) if max_load > 0 else 0
else:
load_score = 0
# Inference time score (0-1, lower time is better)
if "first_infer_ms_p50" in task_df.columns:
max_infer = task_df["first_infer_ms_p50"].max()
infer_score = 1 - (task_df["first_infer_ms_p50"] / max_infer) if max_infer > 0 else 0
else:
infer_score = 0
# Calculate weighted score and scale to 0-100
weighted_score = (
(downloads_score * 0.25) +
(likes_score * 0.15) +
(load_score * 0.30) +
(infer_score * 0.30)
) * 100
# Assign scores back to the filtered dataframe
filtered.loc[task_mask, "first_timer_score"] = weighted_score
# Merge scores back to original dataframe
if "first_timer_score" in filtered.columns:
df = df.merge(
filtered[["id", "first_timer_score"]],
on="id",
how="left"
)
else:
df["first_timer_score"] = None
return df
def filter_excluded_models(df: pd.DataFrame) -> pd.DataFrame:
"""Filter out models that should be excluded from recommendations.
This function removes test models and other non-production models that
should not be recommended to users.
Args:
df: DataFrame containing model data with a 'modelId' column
Returns:
DataFrame with excluded models removed
"""
if df.empty or "modelId" not in df.columns:
return df
# Exclude tiny-random test models (e.g., Xenova/tiny-random-RoFormerForMaskedLM)
# These are small test models not meant for production use
filtered = df[~df["modelId"].str.contains("tiny-random", case=False, na=False)]
return filtered
def get_first_timer_friendly_models(df: pd.DataFrame, limit_per_task: int = 3) -> pd.DataFrame:
"""Identify first-timer-friendly models based on popularity and performance, grouped by task.
A model is considered first-timer-friendly if it:
- Has high downloads (popular)
- Has fast load times (easy to start)
- Has fast inference times (quick results)
- Successfully completed benchmarks
Args:
df: DataFrame containing benchmark results
limit_per_task: Maximum number of models to return per task
Returns:
DataFrame with top first-timer-friendly models per task
"""
if df.empty:
return pd.DataFrame()
# Filter only successful benchmarks
filtered = df[df["status"] == "completed"].copy() if "status" in df.columns else df.copy()
# Exclude test models and other non-production models
filtered = filter_excluded_models(filtered)
if filtered.empty:
return pd.DataFrame()
# Check if task column exists
if "task" not in filtered.columns:
logger.warning("Task column not found in dataframe")
return pd.DataFrame()
# Calculate first-timer-friendliness score per task
all_results = []
for task in filtered["task"].unique():
task_df = filtered[filtered["task"] == task].copy()
if task_df.empty:
continue
# Normalize metrics within this task (lower is better for times, higher is better for popularity)
# Downloads score (0-1, higher is better)
if "downloads" in task_df.columns:
max_downloads = task_df["downloads"].max()
task_df["downloads_score"] = task_df["downloads"] / max_downloads if max_downloads > 0 else 0
else:
task_df["downloads_score"] = 0
# Likes score (0-1, higher is better)
if "likes" in task_df.columns:
max_likes = task_df["likes"].max()
task_df["likes_score"] = task_df["likes"] / max_likes if max_likes > 0 else 0
else:
task_df["likes_score"] = 0
# Load time score (0-1, lower time is better)
if "load_ms_p50" in task_df.columns:
max_load = task_df["load_ms_p50"].max()
task_df["load_score"] = 1 - (task_df["load_ms_p50"] / max_load) if max_load > 0 else 0
else:
task_df["load_score"] = 0
# Inference time score (0-1, lower time is better)
if "first_infer_ms_p50" in task_df.columns:
max_infer = task_df["first_infer_ms_p50"].max()
task_df["infer_score"] = 1 - (task_df["first_infer_ms_p50"] / max_infer) if max_infer > 0 else 0
else:
task_df["infer_score"] = 0
# Calculate weighted first-timer-friendliness score
# Weights: popularity (40%), load time (30%), inference time (30%)
task_df["first_timer_score"] = (
(task_df["downloads_score"] * 0.25) +
(task_df["likes_score"] * 0.15) +
(task_df["load_score"] * 0.30) +
(task_df["infer_score"] * 0.30)
)
# Group by model and take best score for each model within this task
# Filter out NaN scores before getting idxmax
idx_max_series = task_df.groupby("modelId")["first_timer_score"].idxmax()
# Drop NaN indices
valid_indices = idx_max_series.dropna()
if valid_indices.empty:
continue
best_per_model = task_df.loc[valid_indices]
# Sort by first-timer score and take top N for this task
top_for_task = best_per_model.sort_values("first_timer_score", ascending=False).head(limit_per_task)
# Drop intermediate scoring columns
score_cols = ["downloads_score", "likes_score", "load_score", "infer_score", "first_timer_score"]
top_for_task = top_for_task.drop(columns=[col for col in score_cols if col in top_for_task.columns])
all_results.append(top_for_task)
if not all_results:
return pd.DataFrame()
# Combine all results
result = pd.concat(all_results, ignore_index=True)
# Sort by task name for better organization
if "task" in result.columns:
result = result.sort_values("task")
return result
def get_webgpu_beginner_friendly_models(
df: pd.DataFrame,
limit_per_task: int = 5
) -> pd.DataFrame:
"""Get top beginner-friendly models that are WebGPU compatible, grouped by task.
A model is included if it:
- Has high first_timer_score (popular, fast to load, fast inference)
- Has successful WebGPU benchmark results (device=webgpu, status=completed)
Args:
df: DataFrame containing benchmark results
limit_per_task: Maximum number of models to return per task (default: 5)
Returns:
DataFrame with top WebGPU-compatible beginner-friendly models per task
"""
if df.empty:
return pd.DataFrame()
# Filter for WebGPU benchmarks that completed successfully
webgpu_filter = (
(df["device"] == "webgpu") &
(df["status"] == "completed")
)
# Check if required columns exist
if "device" not in df.columns or "status" not in df.columns:
logger.warning("Required columns (device, status) not found in dataframe")
return pd.DataFrame()
filtered = df[webgpu_filter].copy()
# Exclude test models and other non-production models
filtered = filter_excluded_models(filtered)
if filtered.empty:
logger.warning("No successful WebGPU benchmarks found")
return pd.DataFrame()
# Check if required columns exist
if "task" not in filtered.columns or "first_timer_score" not in filtered.columns:
logger.warning("Required columns (task, first_timer_score) not found in filtered dataframe")
return pd.DataFrame()
# Group by task and get top models
all_results = []
for task in filtered["task"].unique():
task_df = filtered[filtered["task"] == task].copy()
if task_df.empty:
continue
# Remove rows with NaN first_timer_score
task_df = task_df.dropna(subset=["first_timer_score"])
if task_df.empty:
continue
# For each model, get the benchmark with the highest first_timer_score
idx_max_series = task_df.groupby("modelId")["first_timer_score"].idxmax()
valid_indices = idx_max_series.dropna()
if valid_indices.empty:
continue
best_per_model = task_df.loc[valid_indices]
# Sort by first_timer_score (descending) and take top N
top_for_task = best_per_model.sort_values(
"first_timer_score",
ascending=False
).head(limit_per_task)
all_results.append(top_for_task)
if not all_results:
logger.warning("No models found after filtering and grouping")
return pd.DataFrame()
# Combine all results
result = pd.concat(all_results, ignore_index=True)
# Sort by task, then by first_timer_score (descending)
if "task" in result.columns and "first_timer_score" in result.columns:
result = result.sort_values(
["task", "first_timer_score"],
ascending=[True, False]
)
return result
def _get_usage_example(task_type: str, repo_id: str) -> tuple[str, str | None]:
"""Get usage example code snippet for a given task type.
Args:
task_type: The task type (e.g., 'text-generation', 'image-classification')
repo_id: The model repository ID (e.g., 'Xenova/gpt2')
Returns:
Tuple of (code_snippet, description)
"""
if task_type == "fill-mask":
return f"""const unmasker = await pipeline('fill-mask', '{repo_id}');
const output = await unmasker('The goal of life is [MASK].');
""", 'Perform masked language modelling (a.k.a. "fill-mask")'
elif task_type == "question-answering":
return f"""const answerer = await pipeline('question-answering', '{repo_id}');
const question = 'Who was Jim Henson?';
const context = 'Jim Henson was a nice puppet.';
const output = await answerer(question, context);
""", 'Run question answering'
elif task_type == "summarization":
return f"""const generator = await pipeline('summarization', '{repo_id}');
const text = 'The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, ' +
'and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. ' +
'During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest ' +
'man-made structure in the world, a title it held for 41 years until the Chrysler Building in New ' +
'York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to ' +
'the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the ' +
'Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second ' +
'tallest free-standing structure in France after the Millau Viaduct.';
const output = await generator(text, {{
max_new_tokens: 100,
}});
""", 'Summarization'
elif task_type == "sentiment-analysis" or task_type == "text-classification":
return f"""const classifier = await pipeline('{task_type}', '{repo_id}');
const output = await classifier('I love transformers!');
""", None
elif task_type == "text-generation":
return f"""const generator = await pipeline('text-generation', '{repo_id}');
const output = await generator('Once upon a time, there was', {{ max_new_tokens: 10 }});
""", 'Text generation'
elif task_type == "text2text-generation":
return f"""const generator = await pipeline('text2text-generation', '{repo_id}');
const output = await generator('how can I become more healthy?', {{
max_new_tokens: 100,
}});
""", 'Text-to-text generation'
elif task_type == "token-classification" or task_type == "ner":
return f"""const classifier = await pipeline('token-classification', '{repo_id}');
const output = await classifier('My name is Sarah and I live in London');
""", 'Perform named entity recognition'
elif task_type == "translation":
return f"""const translator = await pipeline('translation', '{repo_id}');
const output = await translator('Life is like a box of chocolate.', {{
src_lang: '...',
tgt_lang: '...',
}});
""", 'Multilingual translation'
elif task_type == "zero-shot-classification":
return f"""const classifier = await pipeline('zero-shot-classification', '{repo_id}');
const output = await classifier(
'I love transformers!',
['positive', 'negative']
);
""", 'Zero shot classification'
elif task_type == "feature-extraction":
return f"""const extractor = await pipeline('feature-extraction', '{repo_id}');
const output = await extractor('This is a simple test.');
""", 'Run feature extraction'
# Vision
elif task_type == "background-removal":
return f"""const segmenter = await pipeline('background-removal', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/portrait-of-woman_small.jpg';
const output = await segmenter(url);
""", 'Perform background removal'
elif task_type == "depth-estimation":
return f"""const depth_estimator = await pipeline('depth-estimation', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg';
const out = await depth_estimator(url);
""", 'Depth estimation'
elif task_type == "image-classification":
return f"""const classifier = await pipeline('image-classification', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
const output = await classifier(url);
""", 'Classify an image'
elif task_type == "image-segmentation":
return f"""const segmenter = await pipeline('image-segmentation', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg';
const output = await segmenter(url);
""", 'Perform image segmentation'
elif task_type == "image-to-image":
return f"""const processor = await pipeline('image-to-image', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
const output = await processor(url);
""", None
elif task_type == "object-detection":
return f"""const detector = await pipeline('object-detection', '{repo_id}');
const img = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg';
const output = await detector(img, {{ threshold: 0.9 }});
""", 'Run object-detection'
elif task_type == "image-feature-extraction":
return f"""const image_feature_extractor = await pipeline('image-feature-extraction', '{repo_id}');
const url = 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/cats.png';
const features = await image_feature_extractor(url);
""", 'Perform image feature extraction'
# Audio
elif task_type == "audio-classification":
return f"""const classifier = await pipeline('audio-classification', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav';
const output = await classifier(url);
""", 'Perform audio classification'
elif task_type == "automatic-speech-recognition":
return f"""const transcriber = await pipeline('automatic-speech-recognition', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav';
const output = await transcriber(url);
""", 'Transcribe audio from a URL'
elif task_type == "text-to-audio" or task_type == "text-to-speech":
return f"""const synthesizer = await pipeline('text-to-speech', '{repo_id}');
const output = await synthesizer('Hello, my dog is cute');
""", 'Generate audio from text'
# Multimodal
elif task_type == "document-question-answering":
return f"""const qa_pipeline = await pipeline('document-question-answering', '{repo_id}');
const image = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/invoice.png';
const question = 'What is the invoice number?';
const output = await qa_pipeline(image, question);
""", 'Answer questions about a document'
elif task_type == "image-to-text":
return f"""const captioner = await pipeline('image-to-text', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/cats.jpg';
const output = await captioner(url);
""", 'Generate a caption for an image'
elif task_type == "zero-shot-audio-classification":
return f"""const classifier = await pipeline('zero-shot-audio-classification', '{repo_id}');
const audio = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/dog_barking.wav';
const candidate_labels = ['dog', 'vaccum cleaner'];
const scores = await classifier(audio, candidate_labels);
""", 'Perform zero-shot audio classification'
elif task_type == "zero-shot-image-classification":
return f"""const classifier = await pipeline('zero-shot-image-classification', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/tiger.jpg';
const output = await classifier(url, ['tiger', 'horse', 'dog']);
""", 'Zero shot image classification'
elif task_type == "zero-shot-object-detection":
return f"""const detector = await pipeline('zero-shot-object-detection', '{repo_id}');
const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/astronaut.png';
const candidate_labels = ['human face', 'rocket', 'helmet', 'american flag'];
const output = await detector(url, candidate_labels);
""", 'Zero-shot object detection'
else:
logger.warning(f"No usage example found for task type: {task_type}")
return f"""const pipe = await pipeline('{task_type}', '{repo_id}');
const result = await pipe('input text or data');
console.log(result);
""", None
def format_recommended_models_as_markdown(df: pd.DataFrame) -> str:
"""Format recommended WebGPU models as markdown for llms.txt embedding.
Args:
df: DataFrame containing recommended models (output from get_webgpu_beginner_friendly_models)
Returns:
Formatted markdown string
"""
if df.empty:
return "No recommended models available."
markdown_lines = [
"# Recommended Transformers.js Models for First-Time Trials",
"",
"This guide provides curated model recommendations for each task type, selected for their:",
"- **Popularity**: Widely used with strong community support",
"- **Performance**: Fast loading and inference times",
"- **WebGPU Compatibility**: GPU-accelerated in modern browsers",
"",
"**Important:** These recommendations are designed for initial experimentation and learning. "
"Many other models are available for each task. "
"**You should evaluate and choose the best model for your specific use case, performance requirements, and constraints.**",
"",
"## About the Model Recommendations",
"",
"The models below are selected for their popularity and ease of use, making them ideal for initial experimentation. "
"**This list does not cover all available models** - you should evaluate and select the best model for your specific use case and requirements.",
"",
]
# Group by task
if "task" not in df.columns:
return "No task information available."
for task in sorted(df["task"].unique()):
task_df = df[df["task"] == task].copy()
if task_df.empty:
continue
# Add task header
markdown_lines.append(f"## {task.title()}")
markdown_lines.append("")
# Sort by first_timer_score descending
if "first_timer_score" in task_df.columns:
task_df = task_df.sort_values("first_timer_score", ascending=False)
# Get the first/best model for the usage example
first_row = task_df.iloc[0]
first_model_id = first_row.get("modelId", "")
# Add usage example using the top model
if first_model_id:
code_snippet, description = _get_usage_example(task, first_model_id)
if description:
markdown_lines.append(f"**Usage Example:** {description}")
else:
markdown_lines.append("**Usage Example:**")
markdown_lines.append("")
markdown_lines.append("```javascript")
markdown_lines.append(code_snippet.strip())
markdown_lines.append("```")
markdown_lines.append("")
# Add section header for model recommendations
markdown_lines.append("### Recommended Models for First-Time Trials")
markdown_lines.append("")
# Add each model
for idx, row in task_df.iterrows():
model_id = row.get("modelId", "Unknown")
score = row.get("first_timer_score", None)
downloads = row.get("downloads", 0)
likes = row.get("likes", 0)
load_time = row.get("load_ms_p50", None)
infer_time = row.get("first_infer_ms_p50", None)
# Model entry
markdown_lines.append(f"#### {model_id}")
markdown_lines.append("")
# WebGPU compatibility
markdown_lines.append("**WebGPU Compatible:** ✅ Yes")
markdown_lines.append("")
# Metrics
metrics = []
if load_time is not None:
metrics.append(f"Load: {load_time:.1f}ms")
if infer_time is not None:
metrics.append(f"Inference: {infer_time:.1f}ms")
if downloads:
if downloads >= 1_000_000:
downloads_str = f"{downloads / 1_000_000:.1f}M"
elif downloads >= 1_000:
downloads_str = f"{downloads / 1_000:.1f}k"
else:
downloads_str = str(downloads)
metrics.append(f"Downloads: {downloads_str}")
if likes:
metrics.append(f"Likes: {likes}")
if metrics:
markdown_lines.append(f"**Metrics:** {' | '.join(metrics)}")
markdown_lines.append("")
markdown_lines.append("---")
markdown_lines.append("")
# Add footer
markdown_lines.extend([
"## About These Recommendations",
"",
"### Selection Criteria",
"",
"Models in this guide are selected based on:",
"- **Popularity**: High download counts and community engagement on HuggingFace Hub",
"- **Performance**: Fast loading and inference times based on benchmark results",
"- **Compatibility**: Verified WebGPU support for GPU-accelerated browser execution",
"",
"### Understanding Benchmark Metrics",
"",
"**Important:** All performance metrics (load time, inference time, etc.) are measured in a controlled benchmark environment. "
"These metrics are useful for **comparing models against each other**, but they may not reflect the actual performance you'll experience in your specific environment. "
"Factors that affect real-world performance include:",
"- Hardware specifications (CPU, GPU, memory)",
"- Browser type and version",
"- Operating system",
"- Network conditions (for model loading)",
"- Concurrent processes and system load",
"",
"**We recommend** benchmarking models in your own environment with your actual use case to get accurate performance measurements.",
"",
"### For Production Use",
"",
"These recommendations are optimized for first-time trials and learning. "
"For production applications, consider:",
"- Evaluating multiple models for your specific use case",
"- Testing with your actual data and performance requirements",
"- Reviewing the full benchmark results for comprehensive comparisons",
"- Exploring specialized models that may better fit your needs",
"",
"Visit the full leaderboard to explore all available models and their benchmark results.",
])
return "\n".join(markdown_lines)
def get_unique_values(df: pd.DataFrame, column: str) -> List[str]:
"""Get unique values from a column for dropdown choices.
Args:
df: DataFrame to extract values from
column: Column name
Returns:
List of unique values with "All" as first item
"""
if df.empty or column not in df.columns:
return ["All"]
values = df[column].dropna().unique().tolist()
return ["All"] + sorted([str(v) for v in values])
|