Spaces:
Running
Running
Upload 3 files
Browse files- README.md +105 -0
- app.js +440 -0
- index.html +156 -0
README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: AI-Powered Image Classification
|
| 3 |
+
emoji: 🖼️
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
+
sdk: static
|
| 7 |
+
app_file: index.html
|
| 8 |
+
pinned: false
|
| 9 |
+
models:
|
| 10 |
+
- Xenova/resnet-18
|
| 11 |
+
- Xenova/vit-base-patch16-224
|
| 12 |
+
- Xenova/clip-vit-base-patch32
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# AI-Powered Image Classification
|
| 16 |
+
|
| 17 |
+
[Open the demo](https://huggingface.co/spaces/Yunpeng-26/ai-powered-image-classification) · [Student guide](https://yunpeng-26-ai-powered-image-classification.static.hf.space/how-to-start.html) · [Download the guide PDF](https://yunpeng-26-ai-powered-image-classification.static.hf.space/how-to-start.pdf)
|
| 18 |
+
|
| 19 |
+
A small teaching mini-lab for a Creative Technology / introductory AI course.
|
| 20 |
+
|
| 21 |
+
The project shows how a pretrained model can turn image classification into a capability that can be used directly inside an application. Students do not need to train a neural network for the baseline task.
|
| 22 |
+
|
| 23 |
+
## The learning story
|
| 24 |
+
|
| 25 |
+
1. Choose one of 100 examples (four are shown at random), or upload an image.
|
| 26 |
+
2. Choose a browser-compatible classifier: ResNet-18, ViT-B/16, or CLIP.
|
| 27 |
+
3. Inspect the predicted labels and confidence scores.
|
| 28 |
+
4. Keep the image fixed and switch models to compare decisions.
|
| 29 |
+
5. For CLIP, change the candidate labels and observe how the result changes.
|
| 30 |
+
6. Use the comparison log to discuss changes in the top label, score, and runtime.
|
| 31 |
+
|
| 32 |
+
## Model and data
|
| 33 |
+
|
| 34 |
+
- Task: image classification
|
| 35 |
+
- Fixed label space: ImageNet-1K for ResNet and ViT
|
| 36 |
+
- Flexible label space: user-supplied candidate descriptions for CLIP
|
| 37 |
+
- Example bank: **100 local images**, shown four at a time in the original two-by-two gallery. **Shuffle** draws another four from the same bank.
|
| 38 |
+
- Image mix: 29 animals, 29 transport examples, 30 foods, and 12 other examples. These are teaching examples, not a labeled ImageNet benchmark or an accuracy test set.
|
| 39 |
+
- Sources: the original 30 examples are retained; the 70 new CC0 photographs and the 22 earlier CC0 photographs are documented under [Image sources](#image-sources) below.
|
| 40 |
+
|
| 41 |
+
| Classifier | Hugging Face model | What to explore |
|
| 42 |
+
| --- | --- | --- |
|
| 43 |
+
| ResNet-18 | [Xenova/resnet-18](https://huggingface.co/Xenova/resnet-18) | Recommended starting point: a compact CNN with 1,000 ImageNet labels |
|
| 44 |
+
| ViT-B/16 | [Xenova/vit-base-patch16-224](https://huggingface.co/Xenova/vit-base-patch16-224) | Compare a Vision Transformer with the CNN on the same image |
|
| 45 |
+
| CLIP ViT-B/32 | [Xenova/clip-vit-base-patch32](https://huggingface.co/Xenova/clip-vit-base-patch32) | Rank your own candidate descriptions using a vision-language model |
|
| 46 |
+
|
| 47 |
+
For CLIP, enter 2-20 short candidate descriptions separated by commas or new lines. Include a description of the object you want to test: CLIP ranks only the candidates you provide. Its scores should not be interpreted as directly comparable with ResNet or ViT scores.
|
| 48 |
+
|
| 49 |
+
Top-k controls how many labels appear in the result table. Each successful run also records its image, model, top prediction, score, and elapsed time in the comparison log, which keeps the latest 12 runs for the current page session.
|
| 50 |
+
|
| 51 |
+
All three models are already trained. This demo performs inference only; no training code or GPU server is required.
|
| 52 |
+
|
| 53 |
+
## One project, one demo
|
| 54 |
+
|
| 55 |
+
- `index.html` — page structure and teaching flow
|
| 56 |
+
- `style.css` — presentation
|
| 57 |
+
- `app.js` — interaction, image handling, and result rendering
|
| 58 |
+
- `classifier.js` — the online Hugging Face model call
|
| 59 |
+
- `examples/` — 100 example images
|
| 60 |
+
- `how-to-start.html` / `how-to-start.pdf` — the existing student guide for duplicating, editing, testing, and submitting a Space
|
| 61 |
+
|
| 62 |
+
## Runtime
|
| 63 |
+
|
| 64 |
+
The Space is static. `classifier.js` imports Transformers.js from jsDelivr and creates a pipeline with a Hugging Face model ID. The first run for each model downloads its quantized model files from Hugging Face into the browser cache; later runs can reuse that cache. The selected image is processed in the browser and is not uploaded to an inference server.
|
| 65 |
+
|
| 66 |
+
This is the same online model-loading pattern used by the reference object-detection Space, with the task and models changed to image classification.
|
| 67 |
+
|
| 68 |
+
Reference object-detection Space: <https://huggingface.co/spaces/wr92-course/test>
|
| 69 |
+
|
| 70 |
+
## Student assignment
|
| 71 |
+
|
| 72 |
+
Students can duplicate this Space as a working starting point, then build their own AI mini app. They may extend image classification or adapt the project to a different AI task. A submission should make the app's purpose, input, output, and AI model or service easy to understand and demonstrate at least one working example.
|
| 73 |
+
|
| 74 |
+
In the App tab, select **How to Start** for the browser-based steps to create an account, explore the demo, duplicate the Space, edit files, build an app, explain it, and submit a public Space URL. Students are not required to train an AI model from scratch.
|
| 75 |
+
|
| 76 |
+
Higher-scoring work should deepen the selected AI task: compare or replace models, change label spaces, test another image domain, study failure cases, or improve speed and evaluation.
|
| 77 |
+
|
| 78 |
+
## Classroom check before use
|
| 79 |
+
|
| 80 |
+
Before presenting, verify that:
|
| 81 |
+
|
| 82 |
+
- every image in the 100-example bank can be selected through Shuffle;
|
| 83 |
+
- Shuffle changes the four visible examples drawn from the 100-image bank;
|
| 84 |
+
- an uploaded image can be classified;
|
| 85 |
+
- ResNet-18 and ViT-B/16 return ImageNet labels;
|
| 86 |
+
- CLIP responds to changes in candidate labels;
|
| 87 |
+
- the top-k slider changes the number of displayed predictions;
|
| 88 |
+
- the score bars and comparison log update after every successful run;
|
| 89 |
+
- the first uncached run shows download progress;
|
| 90 |
+
- the student guide opens and its PDF download works;
|
| 91 |
+
- the page works at a narrow/mobile width.
|
| 92 |
+
|
| 93 |
+
## Local preview
|
| 94 |
+
|
| 95 |
+
Run a local web server in this folder, for example:
|
| 96 |
+
|
| 97 |
+
```bash
|
| 98 |
+
python -m http.server 7860
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
Then open `http://localhost:7860`. Opening `index.html` directly with `file://` may block module loading.
|
| 102 |
+
|
| 103 |
+
## Image sources
|
| 104 |
+
|
| 105 |
+
The original 30 images remain unchanged. The 70 newly added images are CC0 photographs from Wikimedia Commons. See [the complete source and license list](examples/SOURCES.md) for all 92 CC0 photographs in the 100-image bank.
|
app.js
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { MODEL_OPTIONS, classifyImage, getModelOption } from './classifier.js';
|
| 2 |
+
|
| 3 |
+
const imageInput = document.querySelector('#image-input');
|
| 4 |
+
const modelSelect = document.querySelector('#model-select');
|
| 5 |
+
const modelNote = document.querySelector('#model-note');
|
| 6 |
+
const clipControls = document.querySelector('#clip-controls');
|
| 7 |
+
const candidateLabels = document.querySelector('#candidate-labels');
|
| 8 |
+
const topK = document.querySelector('#top-k');
|
| 9 |
+
const topKValue = document.querySelector('#top-k-value');
|
| 10 |
+
const runButton = document.querySelector('#run-button');
|
| 11 |
+
const statusEl = document.querySelector('#status');
|
| 12 |
+
const progressWrap = document.querySelector('#progress-wrap');
|
| 13 |
+
const progressBar = document.querySelector('#progress-bar');
|
| 14 |
+
const previewWrap = document.querySelector('#preview-wrap');
|
| 15 |
+
const previewImage = document.querySelector('#preview-image');
|
| 16 |
+
const summary = document.querySelector('#summary');
|
| 17 |
+
const tbody = document.querySelector('#predictions-body');
|
| 18 |
+
const runtimeBadge = document.querySelector('#runtime-badge');
|
| 19 |
+
const sampleGrid = document.querySelector('#sample-grid');
|
| 20 |
+
const shuffleButton = document.querySelector('#shuffle-examples');
|
| 21 |
+
const runMeta = document.querySelector('#run-meta');
|
| 22 |
+
const metaModel = document.querySelector('#meta-model');
|
| 23 |
+
const metaImage = document.querySelector('#meta-image');
|
| 24 |
+
const metaRuntime = document.querySelector('#meta-runtime');
|
| 25 |
+
const historyBody = document.querySelector('#history-body');
|
| 26 |
+
const clearHistory = document.querySelector('#clear-history');
|
| 27 |
+
|
| 28 |
+
const state = { dataUrl: null, sourceLabel: null, running: false, history: [] };
|
| 29 |
+
|
| 30 |
+
const EXAMPLES = [
|
| 31 |
+
{ src: './examples/chelsea.png', label: 'Cat' },
|
| 32 |
+
{ src: './examples/coffee.png', label: 'Coffee' },
|
| 33 |
+
{ src: './examples/rocket.jpg', label: 'Rocket' },
|
| 34 |
+
{ src: './examples/astronaut.png', label: 'Astronaut' },
|
| 35 |
+
{ src: './examples/cheetah.jpg', label: 'Cheetah' },
|
| 36 |
+
{ src: './examples/lion.jpg', label: 'Lion' },
|
| 37 |
+
{ src: './examples/tower.jpg', label: 'Tower' },
|
| 38 |
+
{ src: './examples/bus.png', label: 'Bus illustration' },
|
| 39 |
+
{ src: './examples/dog.jpg', label: 'Dog' },
|
| 40 |
+
{ src: './examples/horse.jpg', label: 'Horse' },
|
| 41 |
+
{ src: './examples/elephant.jpg', label: 'Elephant' },
|
| 42 |
+
{ src: './examples/rabbit.jpg', label: 'Rabbit' },
|
| 43 |
+
{ src: './examples/bird.jpg', label: 'Parrot' },
|
| 44 |
+
{ src: './examples/cow.jpg', label: 'Cow' },
|
| 45 |
+
{ src: './examples/airplane.jpg', label: 'Airplane' },
|
| 46 |
+
{ src: './examples/car.jpg', label: 'Car' },
|
| 47 |
+
{ src: './examples/bicycle.jpg', label: 'Bicycle' },
|
| 48 |
+
{ src: './examples/train.jpg', label: 'Train' },
|
| 49 |
+
{ src: './examples/motorcycle.jpg', label: 'Motorcycle' },
|
| 50 |
+
{ src: './examples/boat.jpg', label: 'Boat' },
|
| 51 |
+
{ src: './examples/truck.jpg', label: 'Truck' },
|
| 52 |
+
{ src: './examples/pizza.jpg', label: 'Pizza' },
|
| 53 |
+
{ src: './examples/hamburger.jpg', label: 'Hamburger' },
|
| 54 |
+
{ src: './examples/apple.jpg', label: 'Apple' },
|
| 55 |
+
{ src: './examples/banana.jpg', label: 'Banana' },
|
| 56 |
+
{ src: './examples/orange.jpg', label: 'Orange' },
|
| 57 |
+
{ src: './examples/sandwich.jpg', label: 'Sandwich' },
|
| 58 |
+
{ src: './examples/bread.jpg', label: 'Bread' },
|
| 59 |
+
{ src: './examples/strawberry.jpg', label: 'Strawberry' },
|
| 60 |
+
{ src: './examples/ice_cream.jpg', label: 'Ice cream' },
|
| 61 |
+
{ src: './examples/tiger.jpg', label: 'Tiger' },
|
| 62 |
+
{ src: './examples/zebra.jpg', label: 'Zebra' },
|
| 63 |
+
{ src: './examples/giraffe.jpg', label: 'Giraffe' },
|
| 64 |
+
{ src: './examples/panda.jpg', label: 'Giant panda' },
|
| 65 |
+
{ src: './examples/koala.jpg', label: 'Koala' },
|
| 66 |
+
{ src: './examples/kangaroo.jpg', label: 'Kangaroo' },
|
| 67 |
+
{ src: './examples/camel.jpg', label: 'Camel' },
|
| 68 |
+
{ src: './examples/goat.jpg', label: 'Goat' },
|
| 69 |
+
{ src: './examples/sheep.jpg', label: 'Sheep' },
|
| 70 |
+
{ src: './examples/pig.jpg', label: 'Pig' },
|
| 71 |
+
{ src: './examples/duck.jpg', label: 'Duck' },
|
| 72 |
+
{ src: './examples/chicken.jpg', label: 'Chicken' },
|
| 73 |
+
{ src: './examples/flamingo.jpg', label: 'Flamingo' },
|
| 74 |
+
{ src: './examples/penguin.jpg', label: 'Penguin' },
|
| 75 |
+
{ src: './examples/owl.jpg', label: 'Owl' },
|
| 76 |
+
{ src: './examples/eagle.jpg', label: 'Eagle' },
|
| 77 |
+
{ src: './examples/butterfly.jpg', label: 'Butterfly' },
|
| 78 |
+
{ src: './examples/bee.jpg', label: 'Bee' },
|
| 79 |
+
{ src: './examples/turtle.jpg', label: 'Turtle' },
|
| 80 |
+
{ src: './examples/frog.jpg', label: 'Frog' },
|
| 81 |
+
{ src: './examples/city_bus.jpg', label: 'City bus' },
|
| 82 |
+
{ src: './examples/taxi.jpg', label: 'Taxi' },
|
| 83 |
+
{ src: './examples/tram.jpg', label: 'Tram' },
|
| 84 |
+
{ src: './examples/subway.jpg', label: 'Subway train' },
|
| 85 |
+
{ src: './examples/helicopter.jpg', label: 'Helicopter' },
|
| 86 |
+
{ src: './examples/sailboat.jpg', label: 'Sailboat' },
|
| 87 |
+
{ src: './examples/ship.jpg', label: 'Ship' },
|
| 88 |
+
{ src: './examples/canoe.jpg', label: 'Canoe' },
|
| 89 |
+
{ src: './examples/tractor.jpg', label: 'Tractor' },
|
| 90 |
+
{ src: './examples/fire_truck.jpg', label: 'Fire truck' },
|
| 91 |
+
{ src: './examples/ambulance.jpg', label: 'Ambulance' },
|
| 92 |
+
{ src: './examples/police_car.jpg', label: 'Police car' },
|
| 93 |
+
{ src: './examples/van.jpg', label: 'Van' },
|
| 94 |
+
{ src: './examples/scooter.jpg', label: 'Scooter' },
|
| 95 |
+
{ src: './examples/hot_air_balloon.jpg', label: 'Hot-air balloon' },
|
| 96 |
+
{ src: './examples/excavator.jpg', label: 'Excavator' },
|
| 97 |
+
{ src: './examples/bulldozer.jpg', label: 'Bulldozer' },
|
| 98 |
+
{ src: './examples/forklift.jpg', label: 'Forklift' },
|
| 99 |
+
{ src: './examples/jet.jpg', label: 'Jet aircraft' },
|
| 100 |
+
{ src: './examples/cable_car.jpg', label: 'Cable car' },
|
| 101 |
+
{ src: './examples/pear.jpg', label: 'Pear' },
|
| 102 |
+
{ src: './examples/peach.jpg', label: 'Peach' },
|
| 103 |
+
{ src: './examples/pineapple.jpg', label: 'Pineapple' },
|
| 104 |
+
{ src: './examples/watermelon.jpg', label: 'Watermelon' },
|
| 105 |
+
{ src: './examples/grapes.jpg', label: 'Grapes' },
|
| 106 |
+
{ src: './examples/mango.jpg', label: 'Mango' },
|
| 107 |
+
{ src: './examples/lemon.jpg', label: 'Lemon' },
|
| 108 |
+
{ src: './examples/avocado.jpg', label: 'Avocado' },
|
| 109 |
+
{ src: './examples/carrot.jpg', label: 'Carrot' },
|
| 110 |
+
{ src: './examples/broccoli.jpg', label: 'Broccoli' },
|
| 111 |
+
{ src: './examples/tomato.jpg', label: 'Tomato' },
|
| 112 |
+
{ src: './examples/corn.jpg', label: 'Corn' },
|
| 113 |
+
{ src: './examples/donut.jpg', label: 'Doughnut' },
|
| 114 |
+
{ src: './examples/cookie.jpg', label: 'Cookie' },
|
| 115 |
+
{ src: './examples/cupcake.jpg', label: 'Cupcake' },
|
| 116 |
+
{ src: './examples/sushi.jpg', label: 'Sushi' },
|
| 117 |
+
{ src: './examples/pasta.jpg', label: 'Pasta' },
|
| 118 |
+
{ src: './examples/salad.jpg', label: 'Salad' },
|
| 119 |
+
{ src: './examples/popcorn.jpg', label: 'Popcorn' },
|
| 120 |
+
{ src: './examples/egg.jpg', label: 'Egg' },
|
| 121 |
+
{ src: './examples/laptop.jpg', label: 'Laptop' },
|
| 122 |
+
{ src: './examples/camera.jpg', label: 'Camera' },
|
| 123 |
+
{ src: './examples/guitar.jpg', label: 'Guitar' },
|
| 124 |
+
{ src: './examples/piano.jpg', label: 'Piano' },
|
| 125 |
+
{ src: './examples/clock.jpg', label: 'Clock' },
|
| 126 |
+
{ src: './examples/chair.jpg', label: 'Chair' },
|
| 127 |
+
{ src: './examples/lamp.jpg', label: 'Lamp' },
|
| 128 |
+
{ src: './examples/backpack.jpg', label: 'Backpack' },
|
| 129 |
+
{ src: './examples/umbrella.jpg', label: 'Umbrella' },
|
| 130 |
+
{ src: './examples/book.jpg', label: 'Book' },
|
| 131 |
+
];
|
| 132 |
+
|
| 133 |
+
function chooseRandom(items, count) {
|
| 134 |
+
return [...items]
|
| 135 |
+
.map((item) => ({ item, sort: Math.random() }))
|
| 136 |
+
.sort((a, b) => a.sort - b.sort)
|
| 137 |
+
.slice(0, count)
|
| 138 |
+
.map(({ item }) => item);
|
| 139 |
+
}
|
| 140 |
+
|
| 141 |
+
function resetResults() {
|
| 142 |
+
summary.textContent = 'No predictions yet.';
|
| 143 |
+
tbody.innerHTML = '<tr class="empty-row"><td colspan="3">Run the classifier to see details.</td></tr>';
|
| 144 |
+
runMeta.hidden = true;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
function setBadge(text, mode = '') {
|
| 148 |
+
runtimeBadge.textContent = text;
|
| 149 |
+
runtimeBadge.className = `badge ${mode}`.trim();
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
function setRunning(running) {
|
| 153 |
+
state.running = running;
|
| 154 |
+
runButton.disabled = running || !state.dataUrl;
|
| 155 |
+
modelSelect.disabled = running;
|
| 156 |
+
imageInput.disabled = running;
|
| 157 |
+
topK.disabled = running;
|
| 158 |
+
candidateLabels.disabled = running;
|
| 159 |
+
shuffleButton.disabled = running;
|
| 160 |
+
document.querySelectorAll('.sample-card').forEach((card) => { card.disabled = running; });
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
function setSelectedImage(dataUrl, label, readyMessage) {
|
| 164 |
+
const image = new Image();
|
| 165 |
+
image.onload = () => {
|
| 166 |
+
state.dataUrl = dataUrl;
|
| 167 |
+
state.sourceLabel = label;
|
| 168 |
+
previewImage.src = dataUrl;
|
| 169 |
+
previewImage.alt = `${label} selected for classification`;
|
| 170 |
+
previewWrap.classList.remove('empty');
|
| 171 |
+
setRunning(false);
|
| 172 |
+
statusEl.textContent = readyMessage;
|
| 173 |
+
setBadge('Image ready');
|
| 174 |
+
resetResults();
|
| 175 |
+
};
|
| 176 |
+
image.onerror = () => {
|
| 177 |
+
statusEl.textContent = 'Could not read that image.';
|
| 178 |
+
setBadge('Image error', 'error');
|
| 179 |
+
};
|
| 180 |
+
image.src = dataUrl;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
function blobToDataUrl(blob) {
|
| 184 |
+
return new Promise((resolve, reject) => {
|
| 185 |
+
const reader = new FileReader();
|
| 186 |
+
reader.onload = () => resolve(reader.result);
|
| 187 |
+
reader.onerror = reject;
|
| 188 |
+
reader.readAsDataURL(blob);
|
| 189 |
+
});
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
async function loadExample(example, button) {
|
| 193 |
+
try {
|
| 194 |
+
statusEl.textContent = `Loading example: ${example.label}…`;
|
| 195 |
+
setBadge('Loading sample', 'running');
|
| 196 |
+
const response = await fetch(example.src);
|
| 197 |
+
if (!response.ok) throw new Error(`Could not load ${example.src}`);
|
| 198 |
+
const dataUrl = await blobToDataUrl(await response.blob());
|
| 199 |
+
imageInput.value = '';
|
| 200 |
+
document.querySelectorAll('.sample-card').forEach((card) => card.classList.remove('selected'));
|
| 201 |
+
button.classList.add('selected');
|
| 202 |
+
setSelectedImage(dataUrl, example.label, `${example.label} example ready. Click Run AI Classification.`);
|
| 203 |
+
} catch (error) {
|
| 204 |
+
console.error(error);
|
| 205 |
+
statusEl.textContent = 'Could not load that example. Try another image.';
|
| 206 |
+
setBadge('Sample error', 'error');
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
function renderModelChoices() {
|
| 211 |
+
for (const option of MODEL_OPTIONS) {
|
| 212 |
+
const item = document.createElement('option');
|
| 213 |
+
item.value = option.key;
|
| 214 |
+
item.textContent = option.label;
|
| 215 |
+
modelSelect.appendChild(item);
|
| 216 |
+
}
|
| 217 |
+
modelSelect.value = MODEL_OPTIONS[0].key;
|
| 218 |
+
modelNote.textContent = MODEL_OPTIONS[0].note;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
function renderExampleChoices() {
|
| 222 |
+
sampleGrid.innerHTML = '';
|
| 223 |
+
for (const example of chooseRandom(EXAMPLES, 4)) {
|
| 224 |
+
const button = document.createElement('button');
|
| 225 |
+
button.type = 'button';
|
| 226 |
+
button.className = 'sample-card';
|
| 227 |
+
button.title = `Use sample: ${example.label}`;
|
| 228 |
+
|
| 229 |
+
const image = document.createElement('img');
|
| 230 |
+
image.src = example.src;
|
| 231 |
+
image.alt = example.label;
|
| 232 |
+
image.loading = 'lazy';
|
| 233 |
+
|
| 234 |
+
const label = document.createElement('span');
|
| 235 |
+
label.textContent = example.label;
|
| 236 |
+
|
| 237 |
+
const source = document.createElement('small');
|
| 238 |
+
source.textContent = 'Tested teaching sample';
|
| 239 |
+
|
| 240 |
+
button.append(image, label, source);
|
| 241 |
+
button.addEventListener('click', () => loadExample(example, button));
|
| 242 |
+
sampleGrid.appendChild(button);
|
| 243 |
+
}
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
function setProgress(data) {
|
| 247 |
+
let percent = null;
|
| 248 |
+
if (typeof data?.progress === 'number') percent = data.progress;
|
| 249 |
+
else if (data?.total && typeof data?.loaded === 'number') percent = (data.loaded / data.total) * 100;
|
| 250 |
+
|
| 251 |
+
if (percent !== null && Number.isFinite(percent)) {
|
| 252 |
+
progressWrap.hidden = false;
|
| 253 |
+
progressBar.style.width = `${Math.max(0, Math.min(100, percent))}%`;
|
| 254 |
+
}
|
| 255 |
+
if (data?.status === 'progress' && data?.file) {
|
| 256 |
+
statusEl.textContent = `Downloading model: ${data.file}`;
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
function parseCandidateLabels() {
|
| 261 |
+
return candidateLabels.value.split(/[,\n]/).map((label) => label.trim()).filter(Boolean);
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
function renderPredictions(predictions) {
|
| 265 |
+
tbody.innerHTML = '';
|
| 266 |
+
const option = getModelOption(modelSelect.value);
|
| 267 |
+
const scoreContext = option.task === 'zero-shot-image-classification'
|
| 268 |
+
? 'Scores compare only the candidate labels supplied above.'
|
| 269 |
+
: 'Scores are calculated over the 1,000 ImageNet classes.';
|
| 270 |
+
|
| 271 |
+
predictions.forEach((prediction, index) => {
|
| 272 |
+
const row = document.createElement('tr');
|
| 273 |
+
const rankCell = document.createElement('td');
|
| 274 |
+
rankCell.textContent = index + 1;
|
| 275 |
+
|
| 276 |
+
const labelCell = document.createElement('td');
|
| 277 |
+
labelCell.textContent = prediction.label;
|
| 278 |
+
|
| 279 |
+
const score = Math.max(0, Math.min(1, Number(prediction.score)));
|
| 280 |
+
const scoreCell = document.createElement('td');
|
| 281 |
+
const scoreWrap = document.createElement('div');
|
| 282 |
+
scoreWrap.className = 'score-wrap';
|
| 283 |
+
const scoreText = document.createElement('span');
|
| 284 |
+
scoreText.textContent = `${(score * 100).toFixed(2)}%`;
|
| 285 |
+
const scoreTrack = document.createElement('span');
|
| 286 |
+
scoreTrack.className = 'score-track';
|
| 287 |
+
const scoreFill = document.createElement('span');
|
| 288 |
+
scoreFill.className = 'score-fill';
|
| 289 |
+
scoreFill.style.width = `${score * 100}%`;
|
| 290 |
+
scoreTrack.appendChild(scoreFill);
|
| 291 |
+
scoreWrap.append(scoreText, scoreTrack);
|
| 292 |
+
scoreCell.appendChild(scoreWrap);
|
| 293 |
+
|
| 294 |
+
row.append(rankCell, labelCell, scoreCell);
|
| 295 |
+
tbody.appendChild(row);
|
| 296 |
+
});
|
| 297 |
+
|
| 298 |
+
const best = predictions[0];
|
| 299 |
+
summary.textContent = best
|
| 300 |
+
? `${option.label} predicts “${best.label}” at ${(Number(best.score) * 100).toFixed(1)}%. ${scoreContext}`
|
| 301 |
+
: `${option.label} returned no predictions.`;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
function renderHistory() {
|
| 305 |
+
clearHistory.disabled = state.history.length === 0;
|
| 306 |
+
if (!state.history.length) {
|
| 307 |
+
historyBody.innerHTML = '<tr class="empty-row"><td colspan="6">Run two models on the same image to start a comparison.</td></tr>';
|
| 308 |
+
return;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
historyBody.innerHTML = '';
|
| 312 |
+
state.history.forEach((item, index) => {
|
| 313 |
+
const row = document.createElement('tr');
|
| 314 |
+
const values = [
|
| 315 |
+
index + 1,
|
| 316 |
+
item.image,
|
| 317 |
+
item.model,
|
| 318 |
+
item.label,
|
| 319 |
+
`${(item.score * 100).toFixed(1)}%`,
|
| 320 |
+
`${item.seconds.toFixed(1)}s`,
|
| 321 |
+
];
|
| 322 |
+
values.forEach((value) => {
|
| 323 |
+
const cell = document.createElement('td');
|
| 324 |
+
cell.textContent = value;
|
| 325 |
+
row.appendChild(cell);
|
| 326 |
+
});
|
| 327 |
+
historyBody.appendChild(row);
|
| 328 |
+
});
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
function recordRun(option, predictions, seconds) {
|
| 332 |
+
const best = predictions[0];
|
| 333 |
+
if (!best) return;
|
| 334 |
+
|
| 335 |
+
state.history.unshift({
|
| 336 |
+
image: state.sourceLabel || 'Selected image',
|
| 337 |
+
model: option.label,
|
| 338 |
+
label: best.label,
|
| 339 |
+
score: Number(best.score),
|
| 340 |
+
seconds,
|
| 341 |
+
});
|
| 342 |
+
state.history = state.history.slice(0, 12);
|
| 343 |
+
renderHistory();
|
| 344 |
+
|
| 345 |
+
metaModel.textContent = option.label;
|
| 346 |
+
metaImage.textContent = state.sourceLabel || 'Selected image';
|
| 347 |
+
metaRuntime.textContent = `${seconds.toFixed(1)}s`;
|
| 348 |
+
runMeta.hidden = false;
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
topK.addEventListener('input', () => { topKValue.value = topK.value; });
|
| 352 |
+
|
| 353 |
+
modelSelect.addEventListener('change', () => {
|
| 354 |
+
const option = getModelOption(modelSelect.value);
|
| 355 |
+
modelNote.textContent = option.note;
|
| 356 |
+
clipControls.hidden = option.task !== 'zero-shot-image-classification';
|
| 357 |
+
setBadge(`Model: ${option.label}`);
|
| 358 |
+
resetResults();
|
| 359 |
+
if (state.dataUrl) statusEl.textContent = `Model changed to ${option.label}. Run again to compare predictions.`;
|
| 360 |
+
});
|
| 361 |
+
|
| 362 |
+
imageInput.addEventListener('change', () => {
|
| 363 |
+
const file = imageInput.files?.[0];
|
| 364 |
+
if (!file) return;
|
| 365 |
+
if (!file.type.startsWith('image/')) {
|
| 366 |
+
statusEl.textContent = 'Please choose an image file.';
|
| 367 |
+
setBadge('Invalid file', 'error');
|
| 368 |
+
return;
|
| 369 |
+
}
|
| 370 |
+
if (file.size > 12 * 1024 * 1024) {
|
| 371 |
+
statusEl.textContent = 'Please choose an image smaller than 12 MB.';
|
| 372 |
+
setBadge('Image too large', 'error');
|
| 373 |
+
return;
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
document.querySelectorAll('.sample-card').forEach((card) => card.classList.remove('selected'));
|
| 377 |
+
const reader = new FileReader();
|
| 378 |
+
reader.onload = () => setSelectedImage(reader.result, 'Uploaded image', 'Your image is ready. Click Run AI Classification.');
|
| 379 |
+
reader.onerror = () => {
|
| 380 |
+
statusEl.textContent = 'Could not read that image.';
|
| 381 |
+
setBadge('Image error', 'error');
|
| 382 |
+
};
|
| 383 |
+
reader.readAsDataURL(file);
|
| 384 |
+
});
|
| 385 |
+
|
| 386 |
+
clearHistory.addEventListener('click', () => {
|
| 387 |
+
state.history = [];
|
| 388 |
+
renderHistory();
|
| 389 |
+
});
|
| 390 |
+
|
| 391 |
+
shuffleButton.addEventListener('click', () => {
|
| 392 |
+
renderExampleChoices();
|
| 393 |
+
statusEl.textContent = state.dataUrl
|
| 394 |
+
? 'New examples shown. Your current image is still selected.'
|
| 395 |
+
: 'Choose an example, or upload your own image.';
|
| 396 |
+
});
|
| 397 |
+
|
| 398 |
+
runButton.addEventListener('click', async () => {
|
| 399 |
+
if (!state.dataUrl || state.running) return;
|
| 400 |
+
|
| 401 |
+
const option = getModelOption(modelSelect.value);
|
| 402 |
+
const labels = parseCandidateLabels();
|
| 403 |
+
if (option.task === 'zero-shot-image-classification' && (labels.length < 2 || labels.length > 20)) {
|
| 404 |
+
statusEl.textContent = 'CLIP needs 2–20 candidate labels.';
|
| 405 |
+
setBadge('Check labels', 'error');
|
| 406 |
+
return;
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
setRunning(true);
|
| 410 |
+
setBadge(`Loading ${option.label}`, 'running');
|
| 411 |
+
progressWrap.hidden = false;
|
| 412 |
+
progressBar.style.width = '4%';
|
| 413 |
+
const started = performance.now();
|
| 414 |
+
|
| 415 |
+
try {
|
| 416 |
+
statusEl.textContent = `Preparing ${option.label}. The first run may download model files.`;
|
| 417 |
+
const predictions = await classifyImage(state.dataUrl, {
|
| 418 |
+
modelKey: option.key,
|
| 419 |
+
topK: Number(topK.value),
|
| 420 |
+
candidateLabels: labels,
|
| 421 |
+
progressCallback: setProgress,
|
| 422 |
+
});
|
| 423 |
+
renderPredictions(predictions);
|
| 424 |
+
const seconds = (performance.now() - started) / 1000;
|
| 425 |
+
recordRun(option, predictions, seconds);
|
| 426 |
+
statusEl.textContent = `Done in ${seconds.toFixed(1)}s with ${option.label}. Change the model or image to compare.`;
|
| 427 |
+
setBadge(`${predictions.length} predictions`, 'ready');
|
| 428 |
+
} catch (error) {
|
| 429 |
+
console.error(error);
|
| 430 |
+
statusEl.textContent = `${option.label} failed to run. Check the network connection or try ResNet-18.`;
|
| 431 |
+
setBadge('Error', 'error');
|
| 432 |
+
} finally {
|
| 433 |
+
progressWrap.hidden = true;
|
| 434 |
+
setRunning(false);
|
| 435 |
+
}
|
| 436 |
+
});
|
| 437 |
+
|
| 438 |
+
renderModelChoices();
|
| 439 |
+
renderExampleChoices();
|
| 440 |
+
renderHistory();
|
index.html
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<meta name="description" content="AI-Powered Image Classification mini-lab using pretrained Hugging Face models">
|
| 7 |
+
<title>AI-Powered Image Classification</title>
|
| 8 |
+
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>🖼️</text></svg>">
|
| 9 |
+
<link rel="stylesheet" href="./style.css?v=4">
|
| 10 |
+
</head>
|
| 11 |
+
<body>
|
| 12 |
+
<main class="page-shell">
|
| 13 |
+
<nav class="guide-linkbar" aria-label="Course guide">
|
| 14 |
+
<span>Building your own AI mini app?</span>
|
| 15 |
+
<a href="./how-to-start.html">How to Start <span aria-hidden="true">→</span></a>
|
| 16 |
+
</nav>
|
| 17 |
+
|
| 18 |
+
<header class="hero">
|
| 19 |
+
<p class="eyebrow">Creative Technology · Computer Vision</p>
|
| 20 |
+
<h1>AI-Powered Image Classification</h1>
|
| 21 |
+
<p class="lede">Pick a tested example or upload your own image. Then choose a pretrained classifier and let AI answer: <strong>what is this image mainly about?</strong></p>
|
| 22 |
+
<div class="lesson-strip" aria-label="Mini-lab overview">
|
| 23 |
+
<span><b>Task</b> Image classification</span>
|
| 24 |
+
<span><b>Data</b> ImageNet labels or custom candidates</span>
|
| 25 |
+
<span><b>Models</b> ResNet · ViT · CLIP</span>
|
| 26 |
+
</div>
|
| 27 |
+
</header>
|
| 28 |
+
|
| 29 |
+
<section class="workspace" aria-label="Image classification mini-lab">
|
| 30 |
+
<div class="controls card">
|
| 31 |
+
<div class="step">
|
| 32 |
+
<span class="step-number">1</span>
|
| 33 |
+
<div>
|
| 34 |
+
<p class="control-label sample-title">Choose an image</p>
|
| 35 |
+
<p class="helper">Start with a tested example, or try a photo of your own.</p>
|
| 36 |
+
</div>
|
| 37 |
+
</div>
|
| 38 |
+
|
| 39 |
+
<div class="sample-head example-head">
|
| 40 |
+
<div>
|
| 41 |
+
<p class="input-kicker">Start here · Example images</p>
|
| 42 |
+
<p class="helper">Four images are drawn at random from 100 example images.</p>
|
| 43 |
+
</div>
|
| 44 |
+
<button id="shuffle-examples" class="shuffle-button" type="button">Shuffle</button>
|
| 45 |
+
</div>
|
| 46 |
+
<div id="sample-grid" class="sample-grid" aria-label="Example images"></div>
|
| 47 |
+
<div class="or-row"><span>or try your own</span></div>
|
| 48 |
+
<label class="control-label" for="image-input">Upload an image</label>
|
| 49 |
+
<p class="helper">PNG, JPEG, or WebP works best.</p>
|
| 50 |
+
<input id="image-input" class="file-input" type="file" accept="image/png,image/jpeg,image/webp">
|
| 51 |
+
|
| 52 |
+
<div class="divider"></div>
|
| 53 |
+
|
| 54 |
+
<div class="step">
|
| 55 |
+
<span class="step-number">2</span>
|
| 56 |
+
<div class="grow">
|
| 57 |
+
<label class="control-label" for="model-select">Choose classifier</label>
|
| 58 |
+
<select id="model-select" class="model-select"></select>
|
| 59 |
+
<p id="model-note" class="helper"></p>
|
| 60 |
+
<div id="clip-controls" class="clip-controls" hidden>
|
| 61 |
+
<label class="control-label" for="candidate-labels">CLIP candidate labels</label>
|
| 62 |
+
<textarea id="candidate-labels" rows="3" spellcheck="false">a cat, a cup of coffee, a rocket, an astronaut, a dog, a car</textarea>
|
| 63 |
+
<p class="helper">Use 2–20 short descriptions, separated by commas or new lines.</p>
|
| 64 |
+
</div>
|
| 65 |
+
</div>
|
| 66 |
+
</div>
|
| 67 |
+
|
| 68 |
+
<div class="divider"></div>
|
| 69 |
+
|
| 70 |
+
<div class="step">
|
| 71 |
+
<span class="step-number">3</span>
|
| 72 |
+
<div class="grow">
|
| 73 |
+
<div class="slider-row">
|
| 74 |
+
<label class="control-label" for="top-k">Predictions to display</label>
|
| 75 |
+
<output id="top-k-value" for="top-k">5</output>
|
| 76 |
+
</div>
|
| 77 |
+
<input id="top-k" type="range" min="1" max="10" value="5" step="1">
|
| 78 |
+
<p class="helper">Top-k changes how many high-scoring labels are shown.</p>
|
| 79 |
+
</div>
|
| 80 |
+
</div>
|
| 81 |
+
|
| 82 |
+
<button id="run-button" class="run-button" type="button" disabled>Run AI Classification</button>
|
| 83 |
+
<p id="status" class="status" aria-live="polite">Choose an example or upload your own image.</p>
|
| 84 |
+
<div id="progress-wrap" class="progress-wrap" hidden>
|
| 85 |
+
<div id="progress-bar" class="progress-bar"></div>
|
| 86 |
+
</div>
|
| 87 |
+
</div>
|
| 88 |
+
|
| 89 |
+
<div class="result card">
|
| 90 |
+
<div class="result-head">
|
| 91 |
+
<div>
|
| 92 |
+
<p class="section-kicker">AI prediction</p>
|
| 93 |
+
<h2>What did the classifier predict?</h2>
|
| 94 |
+
</div>
|
| 95 |
+
<span id="runtime-badge" class="badge">Waiting</span>
|
| 96 |
+
</div>
|
| 97 |
+
<div id="preview-wrap" class="preview-wrap empty">
|
| 98 |
+
<img id="preview-image" alt="Selected image">
|
| 99 |
+
<p id="preview-placeholder">Choose an image first. It will appear here before classification.</p>
|
| 100 |
+
</div>
|
| 101 |
+
|
| 102 |
+
<div id="summary" class="summary">No predictions yet.</div>
|
| 103 |
+
|
| 104 |
+
<div id="run-meta" class="run-meta" hidden>
|
| 105 |
+
<span><b>Model</b><span id="meta-model">—</span></span>
|
| 106 |
+
<span><b>Image</b><span id="meta-image">—</span></span>
|
| 107 |
+
<span><b>Runtime</b><span id="meta-runtime">—</span></span>
|
| 108 |
+
</div>
|
| 109 |
+
|
| 110 |
+
<div class="table-wrap">
|
| 111 |
+
<table>
|
| 112 |
+
<thead><tr><th>Rank</th><th>Label</th><th>Score</th></tr></thead>
|
| 113 |
+
<tbody id="predictions-body">
|
| 114 |
+
<tr class="empty-row"><td colspan="3">Run the classifier to see details.</td></tr>
|
| 115 |
+
</tbody>
|
| 116 |
+
</table>
|
| 117 |
+
</div>
|
| 118 |
+
</div>
|
| 119 |
+
</section>
|
| 120 |
+
|
| 121 |
+
<section class="explain card">
|
| 122 |
+
<p class="section-kicker">What you are doing</p>
|
| 123 |
+
<h2 class="explain-title">Use a learned AI capability instead of rebuilding the traditional task from scratch.</h2>
|
| 124 |
+
<p class="explain-copy">ResNet and ViT choose from 1,000 fixed ImageNet classes. CLIP compares the image with descriptions that you provide. Keep the image fixed, change one setting, and explain why the output changes.</p>
|
| 125 |
+
<div class="flow" aria-label="Image classification flow">
|
| 126 |
+
<span>Example or your image</span><span class="arrow">→</span>
|
| 127 |
+
<span>Selected pretrained classifier</span><span class="arrow">→</span>
|
| 128 |
+
<span>Labels + scores</span>
|
| 129 |
+
</div>
|
| 130 |
+
<p class="extension-note"><strong>Compare models:</strong> keep the image fixed, switch classifier, and observe how labels, scores, and speed change.</p>
|
| 131 |
+
</section>
|
| 132 |
+
|
| 133 |
+
<section class="history card" aria-labelledby="history-title">
|
| 134 |
+
<div class="history-head">
|
| 135 |
+
<div>
|
| 136 |
+
<p class="section-kicker">Comparison log</p>
|
| 137 |
+
<h2 id="history-title" class="explain-title">Keep one image fixed and compare model decisions.</h2>
|
| 138 |
+
<p class="history-copy">Each successful run is added below. This makes the classroom comparison visible without reloading the page.</p>
|
| 139 |
+
</div>
|
| 140 |
+
<button id="clear-history" class="secondary-button" type="button" disabled>Clear log</button>
|
| 141 |
+
</div>
|
| 142 |
+
<div class="table-wrap">
|
| 143 |
+
<table>
|
| 144 |
+
<thead><tr><th>Run</th><th>Image</th><th>Model</th><th>Top prediction</th><th>Score</th><th>Time</th></tr></thead>
|
| 145 |
+
<tbody id="history-body">
|
| 146 |
+
<tr class="empty-row"><td colspan="6">Run two models on the same image to start a comparison.</td></tr>
|
| 147 |
+
</tbody>
|
| 148 |
+
</table>
|
| 149 |
+
</div>
|
| 150 |
+
</section>
|
| 151 |
+
|
| 152 |
+
<footer>Hosted as a Hugging Face Static Space · Powered by Transformers.js</footer>
|
| 153 |
+
</main>
|
| 154 |
+
<script type="module" src="./app.js?v=5"></script>
|
| 155 |
+
</body>
|
| 156 |
+
</html>
|