File size: 9,083 Bytes
595b3e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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()