VoicesColeby's picture
Fixed fork: guard deleted-dataset crash
595b3e1 verified
Raw
History Blame Contribute Delete
9.08 kB
"""
Community fork of MariaK/Check-my-progress-Audio-Course.
Fixes the upstream RUNTIME_ERROR: the original calls certification() eagerly at
module load AND the Unit 7 ("demo") branch downloads usernames.csv from the
`huggingface-course/audio-course-u7-hands-on` dataset, which has been deleted
(404) β€” with no try/except, so the Space crashed on startup.
Changes (behaviour-preserving for Units 4/5/6):
1. Do NOT run certification() at import time (the table starts empty).
2. Guard the Unit 7 download so a deleted/gated dataset no longer crashes the
app; Unit 7 is reported as "auto-check unavailable" instead.
3. Handle an empty/blank username gracefully.
Units 4/5/6 are verified exactly as upstream (against your models on the Hub).
"""
import os
import re
import gradio as gr
import pandas as pd
import requests
from huggingface_hub import HfApi, ModelCard, hf_hub_download
from huggingface_hub.repocard import metadata_load
def pass_emoji(passed):
return "βœ…" if passed is True else "❌"
api = HfApi()
USERNAMES_DATASET_ID = "huggingface-course/audio-course-u7-hands-on"
HF_TOKEN = os.environ.get("HF_TOKEN")
def get_user_models(hf_username, task):
models = api.list_models(author=hf_username, filter=[task])
user_model_ids = [x.modelId for x in models]
match task:
case "audio-classification":
dataset = "marsyas/gtzan"
case "automatic-speech-recognition":
dataset = "PolyAI/minds14"
case "text-to-speech":
dataset = ""
case _:
print("Unsupported task")
dataset = ""
if dataset == "":
return user_model_ids
dataset_specific_models = []
for model in user_model_ids:
meta = get_metadata(model)
if meta is None:
continue
try:
if meta["datasets"] == [dataset]:
dataset_specific_models.append(model)
except Exception:
continue
return dataset_specific_models
def calculate_best_result(user_models, task):
best_model = ""
if task == "audio-classification":
best_result = -100
larger_is_better = True
elif task == "automatic-speech-recognition":
best_result = 100
larger_is_better = False
for model in user_models:
meta = get_metadata(model)
if meta is None:
continue
metric = parse_metrics(model, task)
if metric is None:
continue
if larger_is_better:
if metric > best_result:
best_result = metric
best_model = meta["model-index"][0]["name"]
else:
if metric < best_result:
best_result = metric
best_model = meta["model-index"][0]["name"]
return best_result, best_model
def get_metadata(model_id):
try:
readme_path = hf_hub_download(model_id, filename="README.md")
return metadata_load(readme_path)
except requests.exceptions.HTTPError:
return None
def extract_metric(model_card_content, task):
accuracy_pattern = r"(?:Accuracy|eval_accuracy): (\d+\.\d+)"
wer_pattern = r"Wer: (\d+\.\d+)"
pattern = accuracy_pattern if task == "audio-classification" else wer_pattern
match = re.search(pattern, model_card_content)
return float(match.group(1)) if match else None
def parse_metrics(model, task):
card = ModelCard.load(model)
return extract_metric(card.content, task)
def certification(hf_username):
hf_username = (hf_username or "").strip()
results_certification = [
{
"unit": "Unit 4: Audio Classification",
"task": "audio-classification",
"baseline_metric": 0.87,
"best_result": 0,
"best_model_id": "",
"passed_": False,
},
{
"unit": "Unit 5: Automatic Speech Recognition",
"task": "automatic-speech-recognition",
"baseline_metric": 0.37,
"best_result": 0,
"best_model_id": "",
"passed_": False,
},
{
"unit": "Unit 6: Text-to-Speech",
"task": "text-to-speech",
"baseline_metric": 0,
"best_result": 0,
"best_model_id": "",
"passed_": False,
},
{
"unit": "Unit 7: Audio applications",
"task": "demo",
"baseline_metric": 0,
"best_result": 0,
"best_model_id": "",
"passed_": False,
},
]
for unit in results_certification:
unit["passed"] = pass_emoji(unit["passed_"])
if not hf_username:
continue
match unit["task"]:
case "audio-classification":
try:
m = get_user_models(hf_username, task="audio-classification")
best_result, best_model_id = calculate_best_result(
m, task="audio-classification"
)
unit["best_result"] = best_result
unit["best_model_id"] = best_model_id
if unit["best_result"] >= unit["baseline_metric"]:
unit["passed_"] = True
unit["passed"] = pass_emoji(unit["passed_"])
except Exception:
print("No relevant models / metrics for audio classification")
case "automatic-speech-recognition":
try:
m = get_user_models(
hf_username, task="automatic-speech-recognition"
)
best_result, best_model_id = calculate_best_result(
m, task="automatic-speech-recognition"
)
unit["best_result"] = best_result
unit["best_model_id"] = best_model_id
if unit["best_result"] <= unit["baseline_metric"]:
unit["passed_"] = True
unit["passed"] = pass_emoji(unit["passed_"])
except Exception:
print("No relevant models / metrics for ASR")
case "text-to-speech":
try:
m = get_user_models(hf_username, task="text-to-speech")
if m:
unit["best_result"] = 0
unit["best_model_id"] = m[0]
unit["passed_"] = True
unit["passed"] = pass_emoji(unit["passed_"])
except Exception:
print("No relevant models for TTS")
case "demo":
# Guarded: the upstream usernames dataset was deleted (404).
try:
path = hf_hub_download(
USERNAMES_DATASET_ID,
repo_type="dataset",
filename="usernames.csv",
token=HF_TOKEN,
)
users = pd.read_csv(path)
if hf_username in users["username"].tolist():
unit["best_result"] = 0
unit["best_model_id"] = "Demo check passed"
unit["passed_"] = True
unit["passed"] = pass_emoji(unit["passed_"])
except Exception:
unit["best_model_id"] = (
"Unit 7 auto-check unavailable β€” upstream usernames dataset "
"deleted; verify your public demo via the Unit 7 assessment space"
)
case _:
print("Unknown task")
df = pd.DataFrame(results_certification)
return df[
["passed", "unit", "task", "baseline_metric", "best_result", "best_model_id"]
]
with gr.Blocks() as demo:
gr.Markdown(
"""
# πŸ† Check your progress in the Audio Course (community fork) πŸ†
> Fork of `MariaK/Check-my-progress-Audio-Course` that fixes the upstream
> startup crash (the Unit 7 check downloaded a now-deleted dataset). Units
> 4/5/6 are verified exactly as in the original, against your models on the Hub.
- Certificate of completion: **pass 3 of 4** assignments.
- Honors certificate: **pass 4 of 4**.
Your trained-model metric must be equal to or better than the baseline.
Unit 7's automatic check is unavailable upstream (deleted dataset); use the
[Unit 7 assessment space](https://huggingface.co/spaces/huggingface-course/audio-course-u7-assessment)
(or a working fork) to confirm your public demo.
Enter your Hugging Face username to check your progress:
"""
)
hf_username = gr.Textbox(
placeholder="VoicesColeby", label="Your Hugging Face Username"
)
check_progress_button = gr.Button(value="Check my progress")
output = gr.components.Dataframe(value=None)
check_progress_button.click(fn=certification, inputs=hf_username, outputs=output)
demo.launch()