ccm commited on
Commit
8a26fd0
·
verified ·
1 Parent(s): f729bc8

Deploy Week 5 Monday notebook as standalone Gradio app

Browse files
Files changed (3) hide show
  1. README.md +14 -5
  2. app.py +485 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,13 +1,22 @@
1
  ---
2
- title: 2026 24679 Styled Interface
3
- emoji: 🏆
4
- colorFrom: red
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 6.28.0
8
- python_version: '3.13'
9
  app_file: app.py
10
  pinned: false
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: SIDE A Listening Profile
3
+ emoji: 🎧
4
+ colorFrom: blue
5
  colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 6.28.0
8
+ python_version: "3.13"
9
  app_file: app.py
10
  pinned: false
11
+ short_description: Week 5 Monday — 24-679 (2026)
12
  ---
13
 
14
+ # SIDE A Listening Profile
15
+
16
+ Adapted from `04_styled_interface_extension.ipynb` for 24-679, Designing & Deploying AI/ML Systems, Week 5 Monday (September 21, 2026).
17
+
18
+ The original callback, model revisions, examples, and interface are preserved. Notebook installation commands are in `requirements.txt`; the launch blocks on the Space server. Runs on CPU.
19
+
20
+ ## Run locally
21
+
22
+ Use Python 3.13. Install `requirements.txt`, then run `python app.py`.
app.py ADDED
@@ -0,0 +1,485 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FEATURE_COLUMNS = [
2
+ "About how many hours per week do you spend listening to music?",
3
+ "Approximately how many songs are in your music library?",
4
+ "Approximately how many playlists have you created yourself?",
5
+ "How often do you share music with others?",
6
+ "Which decade of music do you listen to most?",
7
+ "How often do you attend live music events?",
8
+ "Do you usually listen to music alone or with others?",
9
+ ]
10
+ LIKERT = ["Never", "Rarely", "Sometimes", "Often", "Very Often"]
11
+ DECADES = ["1970s and before", "1980s", "1990s", "2000s", "2010s", "2020s"]
12
+ COMPANY = ["Alone", "With others", "Both equally"]
13
+ LABEL_NAMES = {0: "Other preference", 1: "Prefers lyrics"}
14
+
15
+ import sys
16
+ assert sys.version_info[:2] == (3, 13), "Use Python 3.13 for this saved model."
17
+ MODEL_REPO_ID = "ccm/2026-24679-lyrics-autogluon-classifier"
18
+ MODEL_REVISION = "7a5f112e518964b65c46a3028bd0b179e7b6253e"
19
+ ZIP_FILENAME = "autogluon_predictor_dir.zip"
20
+ # Use the instructor's fixed model revision, including its native supporting files.
21
+ from pathlib import Path
22
+ import tempfile, zipfile
23
+ from huggingface_hub import hf_hub_download
24
+ archive = hf_hub_download(repo_id=MODEL_REPO_ID, filename=ZIP_FILENAME, revision=MODEL_REVISION)
25
+ MODEL_DIR = Path(tempfile.mkdtemp(prefix="week5_model_"))
26
+ with zipfile.ZipFile(archive) as z:
27
+ for item in z.infolist():
28
+ target = (MODEL_DIR / item.filename).resolve()
29
+ if not target.is_relative_to(MODEL_DIR.resolve()):
30
+ raise ValueError("Unexpected path in model archive.")
31
+ z.extractall(MODEL_DIR)
32
+
33
+ from autogluon.tabular import TabularPredictor
34
+ PREDICTOR = TabularPredictor.load(str(MODEL_DIR))
35
+ assert PREDICTOR.label == "prefers_lyrics"
36
+ assert set(PREDICTOR.features()) == set(FEATURE_COLUMNS)
37
+ assert set(PREDICTOR.class_labels) == {0, 1}
38
+ print("Loaded:", MODEL_REPO_ID, PREDICTOR.model_best)
39
+
40
+ import math
41
+ import pandas as pd
42
+ import gradio as gr
43
+
44
+ def predict_preference(hours, songs, playlists, sharing, decade, events, company):
45
+ values = [hours, songs, playlists]
46
+ if any(v is None for v in values):
47
+ return {}, "Enter listening hours, song count, and playlist count."
48
+ if not all(math.isfinite(float(v)) and float(v) >= 0 for v in values):
49
+ return {}, "Use finite, nonnegative numbers."
50
+ if hours > 168 or songs != int(songs) or playlists != int(playlists):
51
+ return {}, "Hours must be 0-168; song and playlist counts must be whole numbers."
52
+ if sharing not in LIKERT or events not in LIKERT or decade not in DECADES or company not in COMPANY:
53
+ return {}, "Choose each survey option from its list."
54
+ # Preserve the string categories used in 2026; last year's integer mapping was different.
55
+ row = pd.DataFrame([[float(hours), int(songs), int(playlists), sharing, decade, events, company]], columns=FEATURE_COLUMNS)
56
+ probabilities = PREDICTOR.predict_proba(row).iloc[0]
57
+ scores = {LABEL_NAMES[int(k)]: float(v) for k, v in probabilities.items()}
58
+ return scores, "These are model scores, not calibrated certainty. This small class survey does not establish general population preferences."
59
+
60
+ from html import escape
61
+
62
+ def render_profile(hours, songs, playlists, sharing, decade, events, company):
63
+ scores, message = predict_preference(hours, songs, playlists, sharing, decade, events, company)
64
+ if not scores:
65
+ return f"""<section class="score-card" aria-live="polite">
66
+ <p class="eyebrow">YOUR MODEL RESULT</p><h2>One more thing.</h2>
67
+ <p class="result-note">{escape(message)}</p>
68
+ <p class="result-foot">Your previous result has been cleared.</p></section>"""
69
+ winner = max(scores, key=scores.get)
70
+ bars = "".join(
71
+ f'<div class="score-row"><div><span>{escape(label)}</span>'
72
+ f'<strong>{score:.1%}</strong></div><div class="track" aria-hidden="true">'
73
+ f'<div class="fill" style="width:{100 * score:.3f}%"></div></div></div>'
74
+ for label, score in scores.items()
75
+ )
76
+ # Preserve both classes: "Other preference" does not necessarily mean instrumental music.
77
+ return f"""<section class="score-card" aria-live="polite">
78
+ <p class="eyebrow">YOUR MODEL RESULT</p>
79
+ <p class="big-score">{scores[winner]:.0%}</p>
80
+ <h2>{escape(winner)}</h2>
81
+ <p class="result-note">Highest class score for this listening profile.</p>
82
+ <div class="score-bars">{bars}</div>
83
+ <p class="result-foot">{escape(message)}</p>
84
+ <div class="card-bottom"><span>CLASS SURVEY / 2026</span><span>SIDE A ↗</span></div>
85
+ </section>"""
86
+
87
+ # The presentation must preserve the actual model output.
88
+ example = [10, 500, 5, "Sometimes", "2010s", "Rarely", "Alone"]
89
+ scores, _ = predict_preference(*example)
90
+ assert all(f"{score:.1%}" in render_profile(*example) for score in scores.values())
91
+ assert "previous result has been cleared" in render_profile(10, None, *example[2:])
92
+
93
+
94
+ # Change these tokens first when creating your own visual identity.
95
+ THEME = gr.themes.Base(primary_hue="orange", neutral_hue="stone", font=["Arial", "sans-serif"], font_mono=["monospace"]).set(
96
+ body_background_fill="#f2eee5", body_background_fill_dark="#f2eee5",
97
+ body_text_color="#22211e", body_text_color_dark="#22211e",
98
+ block_background_fill="#f2eee5", block_background_fill_dark="#f2eee5",
99
+ block_border_width="0px", block_shadow="none", block_radius="0px",
100
+ block_label_text_color="#22211e", block_label_text_color_dark="#22211e",
101
+ input_background_fill="#fffcf6", input_background_fill_dark="#fffcf6",
102
+ input_border_color="#b3ad9e", input_border_color_dark="#b3ad9e",
103
+ input_radius="2px", input_shadow="none",
104
+ checkbox_label_background_fill="#e6e0d3", checkbox_label_background_fill_dark="#e6e0d3",
105
+ checkbox_label_background_fill_hover="#d5cebf", checkbox_label_background_fill_hover_dark="#d5cebf",
106
+ checkbox_label_background_fill_selected="#e6e0d3", checkbox_label_background_fill_selected_dark="#e6e0d3",
107
+ checkbox_background_color="#fffcf6", checkbox_background_color_dark="#fffcf6",
108
+ slider_color="#c74420", slider_color_dark="#c74420",
109
+ )
110
+
111
+ CSS_FRAME = """
112
+ /* The outer page stops growing at 1120px; auto margins center it. */
113
+ .gradio-container {
114
+ max-width:1120px !important;
115
+ margin:auto !important;
116
+ padding:0 !important;
117
+ }
118
+
119
+ /* IDs connect these rules to elem_id values in the interface cell. */
120
+ #masthead, #hero, #form-heading, #live-note, #profile-output, #page-note {
121
+ padding:0;
122
+ border:0;
123
+ }
124
+ """
125
+
126
+ CSS_HEADER = """
127
+ /* A flex row keeps the brand and subtitle apart. */
128
+ .brandline {
129
+ display:flex;
130
+ justify-content:space-between;
131
+ gap:12px;
132
+ padding-bottom:16px;
133
+ border-bottom:2px solid #22211e;
134
+ font-size:11px;
135
+ font-weight:700;
136
+ letter-spacing:.14em;
137
+ }
138
+
139
+ .brandline strong {
140
+ font-size:19px;
141
+ letter-spacing:-.04em;
142
+ white-space:nowrap;
143
+ }
144
+
145
+ .hero {
146
+ display:flex;
147
+ justify-content:space-between;
148
+ align-items:center;
149
+ gap:25px;
150
+ padding:30px 0 32px;
151
+ }
152
+
153
+ .eyebrow {
154
+ margin:0 0 15px;
155
+ font-size:10px;
156
+ font-weight:700;
157
+ letter-spacing:.17em;
158
+ line-height:1.5;
159
+ }
160
+
161
+ /* clamp(minimum, preferred, maximum) limits fluid type size. */
162
+ .hero h1 {
163
+ margin:0;
164
+ color:#22211e;
165
+ font-size:clamp(48px,7vw,88px);
166
+ line-height:.93;
167
+ letter-spacing:-.065em;
168
+ font-weight:800;
169
+ }
170
+
171
+ /* An HTML emphasis element becomes our colored title accent. */
172
+ .hero h1 em {
173
+ font-style:normal;
174
+ color:#c74420;
175
+ }
176
+
177
+ .hero .intro {
178
+ font-size:15px;
179
+ line-height:1.5;
180
+ max-width:460px;
181
+ margin:18px 0 0;
182
+ color:#575145;
183
+ }
184
+ """
185
+
186
+ CSS_RECORD = """
187
+ /* The first radial gradient sits over the repeating groove pattern. */
188
+ .record {
189
+ width:200px;
190
+ aspect-ratio:1;
191
+ flex:none;
192
+ border-radius:50%;
193
+ background:radial-gradient(circle, #f2eee5 0 3%, #c74420 3.5% 24%, transparent 24.5%), repeating-radial-gradient(circle, #242321 0 2px, #373632 3px, #242321 4px);
194
+ box-shadow:9px 10px 0 #d9d2c3;
195
+ display:grid;
196
+ place-items:center;
197
+ transform:rotate(-16deg);
198
+ }
199
+
200
+ /* Style the label inside the disc, not other text on the page. */
201
+ .record span {
202
+ font-size:12px;
203
+ font-weight:800;
204
+ color:#f2eee5;
205
+ padding-top:56px;
206
+ letter-spacing:.1em;
207
+ }
208
+ """
209
+
210
+ CSS_CONTROLS = """
211
+ /* Gradio still manages row/column wrapping; CSS adds the spacing. */
212
+ #workspace {
213
+ gap:32px;
214
+ align-items:stretch;
215
+ }
216
+
217
+ #controls {
218
+ gap:16px;
219
+ }
220
+
221
+ #controls .field {
222
+ padding:0;
223
+ }
224
+
225
+ #controls label, #controls .label-wrap {
226
+ font-size:12px;
227
+ font-weight:700;
228
+ }
229
+
230
+ /* Explicit label colors keep contrast in both browser color schemes. */
231
+ #controls .field span, #controls .field label {
232
+ color:#22211e !important;
233
+ }
234
+
235
+ #controls [role="listbox"], #controls [role="option"] {
236
+ background:#fffcf6;
237
+ color:#22211e;
238
+ }
239
+
240
+ #controls input {
241
+ font-size:15px;
242
+ color:#22211e;
243
+ }
244
+
245
+ /* Preserve a visible keyboard-focus indicator. */
246
+ #controls input:focus-visible, #controls button:focus-visible {
247
+ outline:3px solid #c74420 !important;
248
+ outline-offset:3px;
249
+ }
250
+
251
+ .form-heading {
252
+ display:flex;
253
+ gap:12px;
254
+ align-items:baseline;
255
+ padding:16px 0 4px;
256
+ border-top:2px solid #22211e;
257
+ }
258
+
259
+ .form-heading span {
260
+ color:#c74420;
261
+ font-size:12px;
262
+ font-weight:700;
263
+ }
264
+
265
+ .form-heading h2 {
266
+ margin:0;
267
+ font-size:25px;
268
+ line-height:1.2;
269
+ letter-spacing:-.04em;
270
+ white-space:nowrap;
271
+ }
272
+
273
+ .live-note {
274
+ display:flex;
275
+ align-items:center;
276
+ gap:8px;
277
+ color:#575145;
278
+ font-size:12px;
279
+ padding-top:3px;
280
+ }
281
+
282
+ /* A decorative cue next to explanatory text, not live network status. */
283
+ .live-dot {
284
+ width:7px;
285
+ height:7px;
286
+ border-radius:50%;
287
+ background:#526339;
288
+ }
289
+ """
290
+
291
+ CSS_RESULTS = """
292
+ /* A vertical flex container lets the footer use remaining space. */
293
+ .score-card {
294
+ background:#22211e;
295
+ color:#f2eee5;
296
+ padding:30px;
297
+ min-height:460px;
298
+ box-sizing:border-box;
299
+ display:flex;
300
+ flex-direction:column;
301
+ }
302
+
303
+ .score-card .eyebrow {
304
+ color:#c4bcad;
305
+ }
306
+
307
+ .big-score {
308
+ font-size:80px;
309
+ font-weight:800;
310
+ letter-spacing:-.065em;
311
+ line-height:1;
312
+ margin:10px 0 8px;
313
+ color:#f2eee5;
314
+ }
315
+
316
+ .score-card h2 {
317
+ font-size:30px;
318
+ line-height:1.1;
319
+ letter-spacing:-.035em;
320
+ margin:0 0 12px;
321
+ color:#f2eee5;
322
+ }
323
+
324
+ .result-note {
325
+ font-size:13px;
326
+ line-height:1.5;
327
+ color:#c4bcad;
328
+ margin:0 0 23px;
329
+ }
330
+
331
+ .score-bars {
332
+ margin:0 0 24px;
333
+ }
334
+
335
+ .score-row {
336
+ margin-bottom:17px;
337
+ }
338
+
339
+ .score-row span, .score-row strong {
340
+ color:#f2eee5;
341
+ }
342
+
343
+ .card-bottom span {
344
+ color:#e78965;
345
+ }
346
+
347
+ /* Only the first direct div child: the class label and its percentage. */
348
+ .score-row > div:first-child {
349
+ display:flex;
350
+ justify-content:space-between;
351
+ gap:15px;
352
+ font-size:12px;
353
+ margin-bottom:8px;
354
+ }
355
+
356
+ .track {
357
+ height:6px;
358
+ background:#4d493f;
359
+ }
360
+
361
+ /* Width comes from the model score in render_profile; CSS supplies its appearance. */
362
+ .fill {
363
+ height:100%;
364
+ background:#e78965;
365
+ }
366
+
367
+ .result-foot {
368
+ font-size:12px;
369
+ line-height:1.55;
370
+ color:#c4bcad;
371
+ margin:0 0 22px;
372
+ }
373
+
374
+ /* margin-top:auto uses spare vertical space above this footer. */
375
+ .card-bottom {
376
+ display:flex;
377
+ justify-content:space-between;
378
+ gap:12px;
379
+ border-top:1px solid #635c4e;
380
+ padding-top:15px;
381
+ margin-top:auto;
382
+ color:#e78965;
383
+ font-size:9px;
384
+ font-weight:700;
385
+ letter-spacing:.08em;
386
+ }
387
+
388
+ .page-note {
389
+ border-top:1px solid #b3ad9e;
390
+ margin-top:20px;
391
+ padding:14px 0;
392
+ color:#575145;
393
+ font-size:11px;
394
+ line-height:1.5;
395
+ }
396
+ """
397
+
398
+ CSS_RESPONSIVE = """
399
+ /* These overrides apply at 650px and below. */
400
+ @media (max-width:650px) {
401
+ .brandline {
402
+ font-size:9px;
403
+ align-items:center;
404
+ }
405
+
406
+ .hero {
407
+ padding:26px 0;
408
+ }
409
+
410
+ .record {
411
+ width:95px;
412
+ box-shadow:5px 6px 0 #d9d2c3;
413
+ }
414
+
415
+ .record span {
416
+ font-size:7px;
417
+ padding-top:28px;
418
+ }
419
+
420
+ .hero h1 {
421
+ font-size:49px;
422
+ }
423
+
424
+ .hero .intro {
425
+ font-size:13px;
426
+ }
427
+
428
+ #workspace {
429
+ gap:24px;
430
+ }
431
+
432
+ .score-card {
433
+ padding:24px;
434
+ min-height:430px;
435
+ }
436
+
437
+ }
438
+
439
+ /* On the narrowest layout, hide only the decorative record. */
440
+ @media (max-width:390px) {
441
+ .record {
442
+ display:none;
443
+ }
444
+
445
+ }
446
+ """
447
+
448
+ # Rerun this cell after editing any styling section, then rerun the launch cell.
449
+ CSS = "\n\n".join([
450
+ CSS_FRAME,
451
+ CSS_HEADER,
452
+ CSS_RECORD,
453
+ CSS_CONTROLS,
454
+ CSS_RESULTS,
455
+ CSS_RESPONSIVE,
456
+ ])
457
+
458
+ with gr.Blocks(title="SIDE A · A listening profile", fill_width=True) as demo:
459
+ gr.HTML('<div class="brandline"><strong>SIDE A ↗</strong><span>A LISTENING PROFILE / VOL. 01</span></div>', elem_id="masthead")
460
+ gr.HTML("""<header class="hero"><div><p class="eyebrow">SEVEN ANSWERS. ONE CLASS MODEL.</p>
461
+ <h1>The way<br>you <em>listen.</em></h1>
462
+ <p class="intro">A small experiment in music habits. Set your listening profile and explore what our class model predicts.</p></div>
463
+ <div class="record" aria-hidden="true"><span>SIDE A</span></div></header>""", elem_id="hero")
464
+ with gr.Row(elem_id="workspace"):
465
+ with gr.Column(scale=6, min_width=300, elem_id="controls"):
466
+ gr.HTML('<div class="form-heading"><span>01 /</span><h2>Set your profile</h2></div>', elem_id="form-heading")
467
+ hours = gr.Slider(0, 168, value=10, step=.5, label="Listening hours / week", elem_classes=["field"])
468
+ with gr.Row():
469
+ songs = gr.Number(value=500, precision=0, label="Songs in library", elem_classes=["field"])
470
+ playlists = gr.Number(value=5, precision=0, label="Playlists created", elem_classes=["field"])
471
+ with gr.Row():
472
+ sharing = gr.Dropdown(LIKERT, value="Sometimes", label="Share music", elem_classes=["field"])
473
+ events = gr.Dropdown(LIKERT, value="Rarely", label="Go to live music", elem_classes=["field"])
474
+ decade = gr.Dropdown(DECADES, value="2010s", label="Your go-to decade", elem_classes=["field"])
475
+ company = gr.Radio(COMPANY, value="Alone", label="Usually listening…", elem_classes=["field"])
476
+ gr.HTML('<p class="live-note"><span class="live-dot" aria-hidden="true"></span>Updates as you change your answers.</p>', elem_id="live-note")
477
+ with gr.Column(scale=5, min_width=300):
478
+ profile = gr.HTML(elem_id="profile-output")
479
+ inputs = [hours, songs, playlists, sharing, decade, events, company]
480
+ gr.on(fn=render_profile, inputs=inputs, outputs=profile,
481
+ trigger_mode="always_last", concurrency_limit=1, api_name="render_profile")
482
+
483
+
484
+ if __name__ == "__main__":
485
+ demo.launch(theme=THEME, css=CSS, footer_links=[], height=1000, server_name="0.0.0.0", server_port=7860)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==6.28.0
2
+ autogluon.tabular==1.6.1
3
+ scikit-learn==1.7.2
4
+ huggingface_hub