Download utils.py from isic-archive/2024_challenge_benchmark: direct link, hf CLI and curl.
- Browser
- Download file 1.97 kB
-
https://huggingface.co/spaces/isic-archive/2024_challenge_benchmark/resolve/main/utils.py
- Command line
-
hf download hf://spaces/isic-archive/2024_challenge_benchmark/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/isic-archive/2024_challenge_benchmark/resolve/main/utils.py
1.97 kB
| import json | |
| import os | |
| from datetime import datetime | |
| import pandas as pd | |
| from huggingface_hub import HfApi | |
| API = HfApi() | |
| SUBMISSIONS_REPO = "isic-archive/ISIC2024_submissions" # private | |
| RESULTS_REPO = "isic-archive/ISIC2024_results" # public or private | |
| # Use your HF token (set as a Space secret) | |
| HF_TOKEN = os.environ.get("ISIC2024_ACCESS_TOKEN") | |
| def submit_prediction(model_name: str, predictions_file) -> str: | |
| """Upload a submission to the submissions dataset.""" | |
| timestamp = datetime.now().isoformat() | |
| submission_id = f"{model_name}_{timestamp}".replace(" ", "_") | |
| # Upload the predictions file | |
| API.upload_file( | |
| path_or_fileobj=predictions_file.name, | |
| path_in_repo=f"predictions/{submission_id}.jsonl", | |
| repo_id=SUBMISSIONS_REPO, | |
| repo_type="dataset", | |
| token=HF_TOKEN, | |
| ) | |
| # Upload a metadata record | |
| metadata = { | |
| "model_name": model_name, | |
| "submitted_by": "user", # could use HF OAuth here | |
| "submission_time": timestamp, | |
| "predictions_file": f"predictions/{submission_id}.jsonl", | |
| } | |
| metadata_bytes = json.dumps(metadata).encode() | |
| API.upload_file( | |
| path_or_fileobj=metadata_bytes, | |
| path_in_repo=f"metadata/{submission_id}.json", | |
| repo_id=SUBMISSIONS_REPO, | |
| repo_type="dataset", | |
| token=HF_TOKEN, | |
| ) | |
| return f"Submitted! Your submission ID is `{submission_id}`. Results will appear on the leaderboard once evaluation is complete." | |
| def load_results() -> pd.DataFrame: | |
| """Load the latest results from the results dataset.""" | |
| try: | |
| from datasets import load_dataset | |
| ds = load_dataset(RESULTS_REPO, token=HF_TOKEN) | |
| df = ds["train"].to_pandas() | |
| # Sort by score descending | |
| df = df.sort_values("overall_score", ascending=False).reset_index(drop=True) | |
| return df | |
| except Exception: | |
| return pd.DataFrame(columns=["model_name", "overall_score"]) | |