Spaces:
Running
Running
Build image captioning mini-lab with three browser models
Browse files- .gitattributes +13 -0
- README.md +88 -4
- app.js +244 -0
- caption-worker.js +16 -0
- captioner.js +85 -0
- captioning.css +19 -0
- examples/SOURCES.md +22 -0
- examples/airplane.jpg +3 -0
- examples/bicycle.jpg +3 -0
- examples/bird.jpg +3 -0
- examples/boat.jpg +3 -0
- examples/city_bus.jpg +3 -0
- examples/cow.jpg +3 -0
- examples/dog.jpg +0 -0
- examples/elephant.jpg +3 -0
- examples/giraffe.jpg +3 -0
- examples/horse.jpg +3 -0
- examples/hot_air_balloon.jpg +0 -0
- examples/ice_cream.jpg +0 -0
- examples/manifest.json +114 -0
- examples/pizza.jpg +3 -0
- examples/sandwich.jpg +3 -0
- examples/train.jpg +3 -0
- examples/umbrella.jpg +3 -0
- how-to-start.html +13 -0
- index.html +101 -17
- models.js +51 -0
- style.css +271 -16
.gitattributes
CHANGED
|
@@ -33,3 +33,16 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
examples/airplane.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
examples/bicycle.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
examples/bird.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
examples/boat.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
examples/city_bus.jpg filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
examples/cow.jpg filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
examples/elephant.jpg filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
examples/giraffe.jpg filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
examples/horse.jpg filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
examples/pizza.jpg filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
examples/sandwich.jpg filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
examples/train.jpg filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
examples/umbrella.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,10 +1,94 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
|
|
|
| 7 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: AI-Powered Image Captioning
|
| 3 |
+
emoji: 🖼️
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: static
|
| 7 |
+
app_file: index.html
|
| 8 |
pinned: false
|
| 9 |
+
models:
|
| 10 |
+
- Xenova/vit-gpt2-image-captioning
|
| 11 |
+
- onnx-community/Florence-2-base-ft
|
| 12 |
+
- HuggingFaceTB/SmolVLM-256M-Instruct
|
| 13 |
---
|
| 14 |
|
| 15 |
+
# AI-Powered Image Captioning
|
| 16 |
+
|
| 17 |
+
A teaching mini-lab for Creative Technology and introductory computer vision. Choose a picture, generate an English description, and compare the sentence with the visual evidence.
|
| 18 |
+
|
| 19 |
+
## Start with a working example
|
| 20 |
+
|
| 21 |
+
1. Choose an example or upload a PNG, JPEG, or WebP image up to 12 MB.
|
| 22 |
+
2. Start with **ViT-GPT2**, then select **Run AI Captioning**.
|
| 23 |
+
3. Allow the model to load on the first run. Read the generated caption and inspect the image.
|
| 24 |
+
4. Keep the image fixed and try **Florence-2** or **SmolVLM**.
|
| 25 |
+
5. Compare objects, attributes, actions, relationships, omissions, and unsupported details.
|
| 26 |
+
6. Download the comparison log before reloading to retain the captions and exact settings.
|
| 27 |
+
|
| 28 |
+
The **How to Start** page explains the workflow, file structure, Space duplication, and student submission. No model training or API key is required.
|
| 29 |
+
|
| 30 |
+
## Models and controls
|
| 31 |
+
|
| 32 |
+
| Captioner | Model | What to explore |
|
| 33 |
+
|---|---|---|
|
| 34 |
+
| ViT-GPT2 | [Xenova/vit-gpt2-image-captioning](https://huggingface.co/Xenova/vit-gpt2-image-captioning) | A visual encoder and a language decoder; short captions without a text instruction |
|
| 35 |
+
| Florence-2 Base | [onnx-community/Florence-2-base-ft](https://huggingface.co/onnx-community/Florence-2-base-ft) | Short captions with `<CAPTION>` and detailed descriptions with `<MORE_DETAILED_CAPTION>` |
|
| 36 |
+
| SmolVLM 256M | [HuggingFaceTB/SmolVLM-256M-Instruct](https://huggingface.co/HuggingFaceTB/SmolVLM-256M-Instruct) | An image plus a natural-language instruction; the prompt can change the requested description |
|
| 37 |
+
|
| 38 |
+
The app pins Transformers.js **4.2.0** and model revisions in `models.js`. It uses quantized `q8` ONNX weights, WebAssembly, greedy decoding, and a maximum output of 32–128 new tokens. Graph optimization is disabled specifically for the older ViT-GPT2 quantized decoder to avoid an ONNX Runtime QDQ optimizer incompatibility; other models use the default optimizer. A token limit is not an exact word count; the model may stop earlier or be cut off at the limit. Model outputs are displayed as generated, without replacing them with reference captions.
|
| 39 |
+
|
| 40 |
+
BLIP remains a useful course/Python model, but is not included as an unsupported browser option. This static demo uses the three browser-compatible model families above. A compatible model file is necessary but does not guarantee useful captions for every image.
|
| 41 |
+
|
| 42 |
+
## Application files
|
| 43 |
+
|
| 44 |
+
- `index.html` — course page, controls, caption output, and comparison table.
|
| 45 |
+
- `style.css` — the existing image-classification site's shared style, kept unchanged.
|
| 46 |
+
- `captioning.css` — additions for caption-specific controls and the guide.
|
| 47 |
+
- `models.js` — model IDs, revisions, and validated generation settings.
|
| 48 |
+
- `captioner.js` — load → prepare → generate → decode.
|
| 49 |
+
- `caption-worker.js` — inference in a worker; stopping a run terminates that worker.
|
| 50 |
+
- `app.js` — input validation, image selection, captions, and log export.
|
| 51 |
+
- `how-to-start.html` — student guide.
|
| 52 |
+
- `examples/manifest.json` — gallery items and their sources.
|
| 53 |
+
- `examples/SOURCES.md` — source and license records for the 16 images.
|
| 54 |
+
|
| 55 |
+
## Runtime and image handling
|
| 56 |
+
|
| 57 |
+
This is a Hugging Face **Static Space**. The library is loaded from jsDelivr; model files are loaded from Hugging Face. Inference runs in the browser, not on an inference server. Images are decoded locally, converted to RGB JPEG on a white background, and resized to a maximum side of 1,600 pixels before the model's own preprocessing. Images exceeding 40 million pixels are rejected to limit memory use.
|
| 58 |
+
|
| 59 |
+
The first run loads roughly a few hundred MB per model. Model files can be reused from the browser cache. Only one model is kept active in memory; switching releases the previous model. Browser cache availability, memory, device speed, and network access affect runtime. An interrupted or failed run can be retried, and a run is stopped if it exceeds ten minutes.
|
| 60 |
+
|
| 61 |
+
The page reports loading and generation separately. Generation time includes preprocessing and decoding after loading; it is a classroom observation, not a hardware-independent speed benchmark. The JSON log includes the model revision, task prompt or instruction, generation settings, library version, image dimensions, and timings. Uploaded image pixels are not included in that exported log.
|
| 62 |
+
|
| 63 |
+
The latest 12 successful runs are held in page memory. The app does not store a user's uploaded images or captions on a server. Reloading clears the comparison log, but may leave downloaded model files in the browser cache.
|
| 64 |
+
|
| 65 |
+
## Example images
|
| 66 |
+
|
| 67 |
+
The 16 CC0 photographs are reused from the course image-classification example bank. Four are shown at a time; **Shuffle** changes the displayed set. Gallery names identify the pictures, not the expected generated sentence. These examples are not a caption benchmark and do not imply measured accuracy. See [image sources](examples/SOURCES.md).
|
| 68 |
+
|
| 69 |
+
## Deploy or preview
|
| 70 |
+
|
| 71 |
+
Create or duplicate a **Static** Hugging Face Space. Upload the files in this folder at the repository root, retaining `examples/` and the README metadata. No build command, Python environment, private token in source, or bundled weights are needed.
|
| 72 |
+
|
| 73 |
+
For a local preview, run the following in this folder:
|
| 74 |
+
|
| 75 |
+
```bash
|
| 76 |
+
python -m http.server 7860
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
Open `http://localhost:7860`. Do not open the HTML directly with `file://`; browser modules and workers need an HTTP origin.
|
| 80 |
+
|
| 81 |
+
## Classroom assignment
|
| 82 |
+
|
| 83 |
+
Build a small application around a pretrained captioner. Submit a working Space URL, explain the model's input and output, show one successful and one difficult example, and compare one controlled change. Retain the actual generated descriptions and cite image sources. Training or fine-tuning is optional.
|
| 84 |
+
|
| 85 |
+
More detail and smoother language do not necessarily mean a more accurate caption. Inspect objects, counts, attributes, actions, and relationships. This app does not report a fabricated confidence percentage or claim a caption is correct automatically.
|
| 86 |
+
|
| 87 |
+
## Reading
|
| 88 |
+
|
| 89 |
+
- [Show and Tell](https://arxiv.org/abs/1411.4555) — an early encoder–decoder captioning model.
|
| 90 |
+
- [Show, Attend and Tell](https://proceedings.mlr.press/v37/xuc15.html) — visual attention.
|
| 91 |
+
- [Florence-2 paper](https://arxiv.org/abs/2311.06242) — unified vision tasks and task prompts.
|
| 92 |
+
- [SmolVLM model card](https://huggingface.co/HuggingFaceTB/SmolVLM-256M-Instruct) — the instruction-following model used here.
|
| 93 |
+
- [Object Hallucination in Image Captioning](https://aclanthology.org/D18-1437/) — why plausible captions can mention unsupported objects.
|
| 94 |
+
- [Transformers.js documentation](https://huggingface.co/docs/transformers.js) — browser inference library.
|
app.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { MODEL_OPTIONS, getModelOption, defaultInstruction, normalizeOptions } from './models.js';
|
| 2 |
+
|
| 3 |
+
const $ = id => document.getElementById(id);
|
| 4 |
+
const ui = Object.fromEntries([
|
| 5 |
+
'image-input', 'model-select', 'model-note', 'detail-select', 'detail-note', 'instruction-controls',
|
| 6 |
+
'instruction', 'max-tokens', 'token-value', 'run-button', 'stop-button', 'status', 'runtime-badge',
|
| 7 |
+
'progress-wrap', 'progress-bar', 'sample-grid', 'shuffle-examples', 'preview-image', 'preview-wrap',
|
| 8 |
+
'image-source', 'summary', 'run-meta', 'meta-model', 'meta-image', 'meta-runtime',
|
| 9 |
+
'copy-caption', 'download-caption', 'history-body', 'clear-history', 'download-history',
|
| 10 |
+
].map(id => [id, $(id)]));
|
| 11 |
+
|
| 12 |
+
const state = { image: null, running: false, loadingImage: false, selection: 0, request: 0, history: [], result: null, examples: [], visible: [] };
|
| 13 |
+
let worker = null;
|
| 14 |
+
let watchdog = null;
|
| 15 |
+
|
| 16 |
+
function badge(text, mode = '') {
|
| 17 |
+
ui['runtime-badge'].textContent = text;
|
| 18 |
+
ui['runtime-badge'].className = `badge ${mode}`.trim();
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
function refreshControls() {
|
| 22 |
+
const busy = state.running || state.loadingImage;
|
| 23 |
+
for (const id of ['image-input', 'model-select', 'max-tokens', 'instruction']) ui[id].disabled = busy;
|
| 24 |
+
ui['detail-select'].disabled = busy || ui['model-select'].value === 'vit';
|
| 25 |
+
ui['shuffle-examples'].disabled = busy || !state.examples.length;
|
| 26 |
+
ui['run-button'].disabled = busy || !state.image;
|
| 27 |
+
ui['stop-button'].hidden = !state.running;
|
| 28 |
+
ui['copy-caption'].disabled = busy || !state.result;
|
| 29 |
+
ui['download-caption'].disabled = busy || !state.result;
|
| 30 |
+
for (const button of ui['sample-grid'].querySelectorAll('button')) button.disabled = busy;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
function resetResult() {
|
| 34 |
+
state.result = null;
|
| 35 |
+
ui.summary.textContent = 'No caption yet.';
|
| 36 |
+
ui['run-meta'].hidden = true;
|
| 37 |
+
refreshControls();
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
function modelControls(resetPrompt = true) {
|
| 41 |
+
const option = getModelOption(ui['model-select'].value);
|
| 42 |
+
ui['model-note'].textContent = option.note;
|
| 43 |
+
ui['detail-note'].textContent = option.detailNote;
|
| 44 |
+
if (option.key === 'vit') ui['detail-select'].value = 'short';
|
| 45 |
+
ui['instruction-controls'].hidden = option.key !== 'smol';
|
| 46 |
+
if (resetPrompt) ui.instruction.value = defaultInstruction(ui['detail-select'].value);
|
| 47 |
+
refreshControls();
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
function downloadFile(filename, content, type) {
|
| 51 |
+
const url = URL.createObjectURL(new Blob([content], { type }));
|
| 52 |
+
const link = document.createElement('a');
|
| 53 |
+
link.href = url; link.download = filename; document.body.append(link); link.click(); link.remove();
|
| 54 |
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
function renderHistory() {
|
| 58 |
+
ui['clear-history'].disabled = !state.history.length;
|
| 59 |
+
ui['download-history'].disabled = !state.history.length;
|
| 60 |
+
ui['history-body'].replaceChildren();
|
| 61 |
+
if (!state.history.length) {
|
| 62 |
+
const row = document.createElement('tr'); row.className = 'empty-row';
|
| 63 |
+
const cell = document.createElement('td'); cell.colSpan = 6; cell.textContent = 'Run two models on the same image to start a comparison.';
|
| 64 |
+
row.append(cell); ui['history-body'].append(row); return;
|
| 65 |
+
}
|
| 66 |
+
for (const item of state.history) {
|
| 67 |
+
const row = document.createElement('tr');
|
| 68 |
+
for (const value of [item.run, item.image.label, item.model, item.caption, item.config.instruction ? 'Instruction' : item.config.style, `${item.generationSeconds.toFixed(1)}s`]) {
|
| 69 |
+
const cell = document.createElement('td'); cell.textContent = value; row.append(cell);
|
| 70 |
+
}
|
| 71 |
+
ui['history-body'].append(row);
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
async function prepareImage(blob) {
|
| 76 |
+
if (!['image/png', 'image/jpeg', 'image/webp'].includes(blob.type)) throw new Error('Choose a PNG, JPEG, or WebP image.');
|
| 77 |
+
if (blob.size > 12 * 1024 * 1024) throw new Error('Choose an image smaller than 12 MB.');
|
| 78 |
+
const url = URL.createObjectURL(blob);
|
| 79 |
+
try {
|
| 80 |
+
const image = new Image(); image.src = url; await image.decode();
|
| 81 |
+
if (!image.naturalWidth || !image.naturalHeight || image.naturalWidth * image.naturalHeight > 40000000) throw new Error('Choose an image with fewer than 40 million pixels.');
|
| 82 |
+
const scale = Math.min(1, 1600 / Math.max(image.naturalWidth, image.naturalHeight));
|
| 83 |
+
const canvas = document.createElement('canvas');
|
| 84 |
+
canvas.width = Math.max(1, Math.round(image.naturalWidth * scale));
|
| 85 |
+
canvas.height = Math.max(1, Math.round(image.naturalHeight * scale));
|
| 86 |
+
const context = canvas.getContext('2d');
|
| 87 |
+
context.fillStyle = '#fff'; context.fillRect(0, 0, canvas.width, canvas.height);
|
| 88 |
+
context.drawImage(image, 0, 0, canvas.width, canvas.height);
|
| 89 |
+
return { dataUrl: canvas.toDataURL('image/jpeg', .94), width: canvas.width, height: canvas.height, originalWidth: image.naturalWidth, originalHeight: image.naturalHeight };
|
| 90 |
+
} finally { URL.revokeObjectURL(url); }
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
async function selectImage(loadBlob, label, source = null, key = null) {
|
| 94 |
+
if (state.running) return;
|
| 95 |
+
const selection = ++state.selection;
|
| 96 |
+
state.loadingImage = true; refreshControls();
|
| 97 |
+
ui.status.textContent = `Loading ${label}…`; badge('Loading image', 'running');
|
| 98 |
+
try {
|
| 99 |
+
const prepared = await prepareImage(await loadBlob());
|
| 100 |
+
if (selection !== state.selection) return;
|
| 101 |
+
state.image = { ...prepared, label, source, key };
|
| 102 |
+
ui['preview-image'].src = prepared.dataUrl;
|
| 103 |
+
ui['preview-image'].alt = `${label} selected for captioning`;
|
| 104 |
+
ui['preview-wrap'].classList.remove('empty');
|
| 105 |
+
ui['image-source'].replaceChildren(); ui['image-source'].hidden = !source;
|
| 106 |
+
if (source) {
|
| 107 |
+
const link = document.createElement('a'); link.href = source; link.target = '_blank'; link.rel = 'noopener noreferrer';
|
| 108 |
+
link.textContent = 'Example source · CC0'; ui['image-source'].append(link);
|
| 109 |
+
}
|
| 110 |
+
for (const card of ui['sample-grid'].children) {
|
| 111 |
+
const selected = card.dataset.src === key;
|
| 112 |
+
card.classList.toggle('selected', selected); card.setAttribute('aria-pressed', String(selected));
|
| 113 |
+
}
|
| 114 |
+
resetResult(); badge('Image ready'); ui.status.textContent = `${label} is ready. Click Run AI Captioning.`;
|
| 115 |
+
} catch (error) {
|
| 116 |
+
if (selection !== state.selection) return;
|
| 117 |
+
ui.status.textContent = `${error.message} ${state.image ? 'Your previous image remains selected.' : 'Try another image.'}`;
|
| 118 |
+
badge('Image error', 'error');
|
| 119 |
+
} finally {
|
| 120 |
+
if (selection === state.selection) { state.loadingImage = false; refreshControls(); }
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
function renderExamples(initial = false) {
|
| 125 |
+
const available = initial ? state.examples : state.examples.filter(item => !state.visible.includes(item.src));
|
| 126 |
+
const shuffled = [...available];
|
| 127 |
+
if (!initial) for (let i = shuffled.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; }
|
| 128 |
+
const selection = shuffled.slice(0, 4);
|
| 129 |
+
state.visible = selection.map(item => item.src);
|
| 130 |
+
ui['sample-grid'].replaceChildren();
|
| 131 |
+
for (const example of selection) {
|
| 132 |
+
const button = document.createElement('button'); button.type = 'button'; button.className = 'sample-card'; button.dataset.src = example.src;
|
| 133 |
+
button.title = `Use sample: ${example.label}`; button.setAttribute('aria-pressed', String(state.image?.key === example.src));
|
| 134 |
+
button.classList.toggle('selected', state.image?.key === example.src);
|
| 135 |
+
const image = document.createElement('img'); image.src = example.src; image.alt = example.label;
|
| 136 |
+
const label = document.createElement('span'); label.textContent = example.label;
|
| 137 |
+
const detail = document.createElement('small'); detail.textContent = 'CC0 teaching image';
|
| 138 |
+
button.append(image, label, detail);
|
| 139 |
+
button.addEventListener('click', () => {
|
| 140 |
+
ui['image-input'].value = '';
|
| 141 |
+
selectImage(async () => {
|
| 142 |
+
const response = await fetch(example.src);
|
| 143 |
+
if (!response.ok) throw new Error('This example could not be loaded.');
|
| 144 |
+
return response.blob();
|
| 145 |
+
}, example.label, example.source, example.src);
|
| 146 |
+
});
|
| 147 |
+
ui['sample-grid'].append(button);
|
| 148 |
+
}
|
| 149 |
+
refreshControls();
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
function finishRun() {
|
| 153 |
+
clearTimeout(watchdog); watchdog = null;
|
| 154 |
+
state.running = false; ui['progress-wrap'].hidden = true; refreshControls();
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
function stopWorker() {
|
| 158 |
+
worker?.terminate(); worker = null;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
function failRun(message) {
|
| 162 |
+
stopWorker(); finishRun(); badge('Run failed', 'error');
|
| 163 |
+
ui.status.textContent = `Could not generate a caption. ${message} Check the connection or retry with ViT-GPT2.`;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
function getWorker() {
|
| 167 |
+
if (worker) return worker;
|
| 168 |
+
worker = new Worker(new URL('./caption-worker.js', import.meta.url), { type: 'module' });
|
| 169 |
+
worker.onerror = event => failRun(event.message || 'The inference worker stopped.');
|
| 170 |
+
worker.onmessageerror = () => failRun('The model result could not be read.');
|
| 171 |
+
worker.onmessage = ({ data }) => {
|
| 172 |
+
if (data.id !== state.request || !state.running) return;
|
| 173 |
+
if (data.type === 'progress') {
|
| 174 |
+
const progress = data.progress;
|
| 175 |
+
if (progress.phase === 'library') ui.status.textContent = 'Loading the captioning library…';
|
| 176 |
+
if (progress.phase === 'download' && progress.file) {
|
| 177 |
+
ui.status.textContent = `Loading model file: ${progress.file}. The first run can take several minutes.`;
|
| 178 |
+
if (Number.isFinite(progress.progress)) ui['progress-bar'].style.width = `${Math.max(0, Math.min(100, progress.progress))}%`;
|
| 179 |
+
}
|
| 180 |
+
if (progress.phase === 'generate') {
|
| 181 |
+
badge('Generating', 'running'); ui['progress-wrap'].hidden = true;
|
| 182 |
+
ui.status.textContent = 'Generating the caption. You can stop this run if it takes too long.';
|
| 183 |
+
}
|
| 184 |
+
} else if (data.type === 'error') failRun(data.message);
|
| 185 |
+
else if (data.type === 'result') {
|
| 186 |
+
const { dataUrl, ...imageInfo } = state.image;
|
| 187 |
+
const result = { ...data.result, image: imageInfo, run: state.request, timestamp: new Date().toISOString() };
|
| 188 |
+
state.result = result; state.history.unshift(result); state.history = state.history.slice(0, 12);
|
| 189 |
+
ui.summary.textContent = result.caption;
|
| 190 |
+
ui['meta-model'].textContent = result.model; ui['meta-image'].textContent = imageInfo.label;
|
| 191 |
+
ui['meta-runtime'].textContent = `${result.generationSeconds.toFixed(1)}s`; ui['run-meta'].hidden = false;
|
| 192 |
+
renderHistory(); finishRun(); badge('Caption ready', 'ready');
|
| 193 |
+
ui.status.textContent = `Done. Loading: ${result.loadSeconds.toFixed(1)}s · generation: ${result.generationSeconds.toFixed(1)}s. Check the sentence against the image.`;
|
| 194 |
+
}
|
| 195 |
+
};
|
| 196 |
+
return worker;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
ui['run-button'].addEventListener('click', () => {
|
| 200 |
+
if (!state.image || state.running || state.loadingImage) return;
|
| 201 |
+
let options;
|
| 202 |
+
try { options = normalizeOptions({ modelKey: ui['model-select'].value, style: ui['detail-select'].value, maxTokens: ui['max-tokens'].value, instruction: ui.instruction.value }); }
|
| 203 |
+
catch (error) { ui.status.textContent = error.message; badge('Check settings', 'error'); return; }
|
| 204 |
+
state.running = true; state.request++; resetResult(); refreshControls();
|
| 205 |
+
badge('Loading model', 'running'); ui.status.textContent = 'Preparing the model. Files are loaded from Hugging Face on the first run.';
|
| 206 |
+
ui['progress-wrap'].hidden = false; ui['progress-bar'].style.width = '0%';
|
| 207 |
+
watchdog = setTimeout(() => failRun('The run exceeded 10 minutes.'), 600000);
|
| 208 |
+
try { getWorker().postMessage({ type: 'caption', id: state.request, image: state.image.dataUrl, options }); }
|
| 209 |
+
catch (error) { failRun(error.message); }
|
| 210 |
+
});
|
| 211 |
+
|
| 212 |
+
ui['stop-button'].addEventListener('click', () => {
|
| 213 |
+
stopWorker(); finishRun(); badge('Stopped');
|
| 214 |
+
ui.status.textContent = 'Run stopped. You can choose another model or try again; cached files can be reused.';
|
| 215 |
+
});
|
| 216 |
+
ui['image-input'].addEventListener('change', () => {
|
| 217 |
+
const file = ui['image-input'].files?.[0]; if (file) selectImage(() => Promise.resolve(file), file.name);
|
| 218 |
+
});
|
| 219 |
+
ui['shuffle-examples'].addEventListener('click', () => {
|
| 220 |
+
renderExamples(); ui.status.textContent = state.image ? 'New examples shown. Your current image remains selected.' : 'Choose an example or upload your own image.';
|
| 221 |
+
});
|
| 222 |
+
ui['model-select'].addEventListener('change', () => {
|
| 223 |
+
modelControls(); resetResult(); badge('Model changed');
|
| 224 |
+
ui.status.textContent = 'Run the selected model on your image to compare descriptions.';
|
| 225 |
+
});
|
| 226 |
+
ui['detail-select'].addEventListener('change', () => { modelControls(); resetResult(); });
|
| 227 |
+
ui['max-tokens'].addEventListener('input', () => { ui['token-value'].value = ui['max-tokens'].value; resetResult(); });
|
| 228 |
+
ui.instruction.addEventListener('input', resetResult);
|
| 229 |
+
ui['clear-history'].addEventListener('click', () => { state.history = []; renderHistory(); });
|
| 230 |
+
ui['download-history'].addEventListener('click', () => downloadFile('caption-comparison.json', JSON.stringify({ task: 'image-captioning', exportedAt: new Date().toISOString(), runs: state.history }, null, 2), 'application/json'));
|
| 231 |
+
ui['download-caption'].addEventListener('click', () => { if (state.result) downloadFile('caption.txt', state.result.caption + '\n', 'text/plain;charset=utf-8'); });
|
| 232 |
+
ui['copy-caption'].addEventListener('click', async () => {
|
| 233 |
+
if (!state.result) return;
|
| 234 |
+
try { await navigator.clipboard.writeText(state.result.caption); ui.status.textContent = 'Caption copied.'; }
|
| 235 |
+
catch { ui.status.textContent = 'Clipboard access is unavailable. Select the caption text or use Save caption.'; }
|
| 236 |
+
});
|
| 237 |
+
|
| 238 |
+
for (const model of MODEL_OPTIONS) { const option = document.createElement('option'); option.value = model.key; option.textContent = model.label; ui['model-select'].append(option); }
|
| 239 |
+
modelControls(); renderHistory();
|
| 240 |
+
fetch('./examples/manifest.json').then(response => {
|
| 241 |
+
if (!response.ok) throw new Error('Example list unavailable'); return response.json();
|
| 242 |
+
}).then(examples => { state.examples = examples; renderExamples(true); }).catch(() => {
|
| 243 |
+
ui.status.textContent = 'Examples could not be loaded. You can still upload your own image.';
|
| 244 |
+
});
|
caption-worker.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { captionImage } from './captioner.js';
|
| 2 |
+
|
| 3 |
+
let busy = false;
|
| 4 |
+
self.onmessage = async ({ data }) => {
|
| 5 |
+
if (busy || data.type !== 'caption') return;
|
| 6 |
+
busy = true;
|
| 7 |
+
const { id, image, options } = data;
|
| 8 |
+
try {
|
| 9 |
+
const result = await captionImage(image, options, progress => self.postMessage({ type: 'progress', id, progress }));
|
| 10 |
+
self.postMessage({ type: 'result', id, result });
|
| 11 |
+
} catch (error) {
|
| 12 |
+
self.postMessage({ type: 'error', id, message: String(error?.message ?? error).slice(0, 500) });
|
| 13 |
+
} finally {
|
| 14 |
+
busy = false;
|
| 15 |
+
}
|
| 16 |
+
};
|
captioner.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { getModelOption, normalizeOptions, TRANSFORMERS_VERSION } from './models.js';
|
| 2 |
+
|
| 3 |
+
let libraryPromise;
|
| 4 |
+
let active = null;
|
| 5 |
+
|
| 6 |
+
async function getLibrary() {
|
| 7 |
+
// Lazy loading keeps the page usable even when the model CDN cannot be reached.
|
| 8 |
+
libraryPromise ??= import('https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.2.0')
|
| 9 |
+
.catch(error => { libraryPromise = null; throw error; });
|
| 10 |
+
const library = await libraryPromise;
|
| 11 |
+
library.env.allowLocalModels = false;
|
| 12 |
+
library.env.useBrowserCache = true;
|
| 13 |
+
library.env.backends.onnx.wasm.numThreads = 1;
|
| 14 |
+
return library;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
async function loadCaptioner(option, report) {
|
| 18 |
+
if (active?.key === option.key) return active;
|
| 19 |
+
if (active) {
|
| 20 |
+
await (active.pipeline ?? active.model).dispose();
|
| 21 |
+
active = null;
|
| 22 |
+
}
|
| 23 |
+
report({ phase: 'library' });
|
| 24 |
+
const lib = await getLibrary();
|
| 25 |
+
const settings = {
|
| 26 |
+
revision: option.revision, dtype: 'q8', device: 'wasm',
|
| 27 |
+
// The old ViT-GPT2 quantized decoder hits an ORT 1.26 QDQ optimizer error.
|
| 28 |
+
// Keep its original graph; other models use the default optimizer.
|
| 29 |
+
...(option.key === 'vit' ? { session_options: { graphOptimizationLevel: 'disabled' } } : {}),
|
| 30 |
+
progress_callback: progress => report({ phase: 'download', ...progress }),
|
| 31 |
+
};
|
| 32 |
+
if (option.key === 'vit') {
|
| 33 |
+
const pipeline = await lib.pipeline('image-to-text', option.modelId, settings);
|
| 34 |
+
active = { key: option.key, pipeline, lib };
|
| 35 |
+
} else {
|
| 36 |
+
// Load sequentially so a processor failure does not leave a large model allocated.
|
| 37 |
+
const processor = await lib.AutoProcessor.from_pretrained(option.modelId, { revision: option.revision });
|
| 38 |
+
const Model = option.key === 'florence' ? lib.Florence2ForConditionalGeneration : lib.AutoModelForImageTextToText;
|
| 39 |
+
const model = await Model.from_pretrained(option.modelId, settings);
|
| 40 |
+
active = { key: option.key, processor, model, lib };
|
| 41 |
+
}
|
| 42 |
+
return active;
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
export async function captionImage(imageUrl, options, report = () => {}) {
|
| 46 |
+
const config = normalizeOptions(options);
|
| 47 |
+
const option = getModelOption(config.modelKey);
|
| 48 |
+
const began = performance.now();
|
| 49 |
+
const engine = await loadCaptioner(option, report);
|
| 50 |
+
const loaded = performance.now();
|
| 51 |
+
report({ phase: 'generate' });
|
| 52 |
+
const generation = { max_new_tokens: config.maxTokens, do_sample: false, num_beams: 1 };
|
| 53 |
+
let text;
|
| 54 |
+
if (option.key === 'vit') {
|
| 55 |
+
const output = await engine.pipeline(imageUrl, generation);
|
| 56 |
+
text = output[0]?.generated_text;
|
| 57 |
+
} else {
|
| 58 |
+
const image = await engine.lib.RawImage.read(imageUrl);
|
| 59 |
+
if (option.key === 'florence') {
|
| 60 |
+
const prompts = engine.processor.construct_prompts(config.task);
|
| 61 |
+
const inputs = await engine.processor(image, prompts);
|
| 62 |
+
const ids = await engine.model.generate({ ...inputs, ...generation });
|
| 63 |
+
const raw = engine.processor.batch_decode(ids, { skip_special_tokens: false })[0];
|
| 64 |
+
const parsed = engine.processor.post_process_generation(raw, config.task, image.size);
|
| 65 |
+
text = parsed[config.task];
|
| 66 |
+
} else {
|
| 67 |
+
const messages = [{ role: 'user', content: [{ type: 'image' }, { type: 'text', text: config.instruction }] }];
|
| 68 |
+
const prompt = engine.processor.apply_chat_template(messages, { add_generation_prompt: true, tokenize: false });
|
| 69 |
+
const inputs = await engine.processor(prompt, image);
|
| 70 |
+
const ids = await engine.model.generate({ ...inputs, ...generation });
|
| 71 |
+
// Decoder-only output contains the input prompt; export only newly generated tokens.
|
| 72 |
+
const generated = ids.tolist()[0].slice(inputs.input_ids.dims[1]);
|
| 73 |
+
text = engine.processor.batch_decode([generated], { skip_special_tokens: true })[0];
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
if (typeof text !== 'string' || !text.trim()) throw new Error('The model returned an empty caption. Try another image or a higher token limit.');
|
| 77 |
+
const ended = performance.now();
|
| 78 |
+
return {
|
| 79 |
+
caption: text.trim(), model: option.label, modelId: option.modelId, revision: option.revision,
|
| 80 |
+
library: `Transformers.js ${TRANSFORMERS_VERSION}`, device: 'wasm', dtype: 'q8',
|
| 81 |
+
graphOptimization: option.key === 'vit' ? 'disabled' : 'default',
|
| 82 |
+
config, generation, loadSeconds: (loaded - began) / 1000,
|
| 83 |
+
generationSeconds: (ended - loaded) / 1000, totalSeconds: (ended - began) / 1000,
|
| 84 |
+
};
|
| 85 |
+
}
|
captioning.css
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/* Caption-specific controls. The shared course style.css is unchanged. */
|
| 2 |
+
[hidden] { display: none !important; }
|
| 3 |
+
.token-controls { margin-top: 18px; }
|
| 4 |
+
#token-value { min-width: 48px; text-align: right; color: #334155; font-variant-numeric: tabular-nums; }
|
| 5 |
+
#max-tokens { width: 100%; margin-top: 13px; accent-color: #111827; }
|
| 6 |
+
.caption-output { font-size: 1.1rem; white-space: pre-wrap; overflow-wrap: anywhere; }
|
| 7 |
+
.caption-actions { display: flex; flex-wrap: wrap; gap: 10px; margin: 0 0 16px; }
|
| 8 |
+
.history-actions { display: flex; flex-wrap: wrap; gap: 8px; }
|
| 9 |
+
.stop-button { width: 100%; margin-top: 10px; }
|
| 10 |
+
#image-source { font-size: .78rem; }
|
| 11 |
+
#image-source a { color: #52627a; }
|
| 12 |
+
#history-body td:nth-child(4) { min-width: 230px; line-height: 1.5; overflow-wrap: anywhere; }
|
| 13 |
+
button:focus-visible { outline: 3px solid #52627a; outline-offset: 3px; }
|
| 14 |
+
.guide-section { margin-top: 20px; padding: 24px; }
|
| 15 |
+
.guide-section h2 { margin-top: 0; }
|
| 16 |
+
.guide-section p, .guide-section li { color: #475569; line-height: 1.65; }
|
| 17 |
+
.guide-section code { overflow-wrap: anywhere; }
|
| 18 |
+
.guide-section a { color: #174f9e; }
|
| 19 |
+
.guide-section pre { padding: 16px; background: #f7f8fa; overflow-x: auto; border-radius: 12px; }
|
examples/SOURCES.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Example image sources
|
| 2 |
+
|
| 3 |
+
These 16 CC0 photographs are reused from the course image-classification example bank. They are teaching inputs, not a caption benchmark or model predictions. Gallery names identify images; they are not reference captions. Original source records are preserved below.
|
| 4 |
+
|
| 5 |
+
| Local file | Gallery label | Category | Commons source | License |
|
| 6 |
+
|---|---|---|---|---|
|
| 7 |
+
| dog.jpg | Dog | Animals | [File page](https://commons.wikimedia.org/wiki/File:Golden_Retriever_%28Pet_Dog%29.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 8 |
+
| cow.jpg | Cow | Animals | [File page](https://commons.wikimedia.org/wiki/File:Cow_and_Calf.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 9 |
+
| horse.jpg | Horse | Animals | [File page](https://commons.wikimedia.org/wiki/File:Horse-2166921.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 10 |
+
| elephant.jpg | Elephant | Animals | [File page](https://commons.wikimedia.org/wiki/File:Elephant_-_Jardim_Zool%C3%B3gico_de_Bras%C3%ADlia_-_DSC09798.JPG) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 11 |
+
| bird.jpg | Parrot | Animals | [File page](https://commons.wikimedia.org/wiki/File:%281218241%29_%22Parrot_bird%22_Psittaciformes_-_Amazon%2C_Brazil.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 12 |
+
| bicycle.jpg | Bicycle | Transport | [File page](https://commons.wikimedia.org/wiki/File:Bicycle_10.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 13 |
+
| train.jpg | Train | Transport | [File page](https://commons.wikimedia.org/wiki/File:Rrogozhine_passenger_train.JPG) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 14 |
+
| boat.jpg | Boat | Transport | [File page](https://commons.wikimedia.org/wiki/File:Boat_of_Malaysia_Bomba.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 15 |
+
| city_bus.jpg | City bus | Transport | [File page](https://commons.wikimedia.org/wiki/File:Osaka-City-Bus_5046.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 16 |
+
| airplane.jpg | Airplane | Transport | [File page](https://commons.wikimedia.org/wiki/File:Aleutian_Airways_plane_at_Anchorage_Airport.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 17 |
+
| pizza.jpg | Pizza | Food | [File page](https://commons.wikimedia.org/wiki/File:Homemade_Pizza.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 18 |
+
| sandwich.jpg | Sandwich | Food | [File page](https://commons.wikimedia.org/wiki/File:Sandwich_at_Goodman%27s_Restaurant_in_Berkeley_Heights_NJ.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 19 |
+
| ice_cream.jpg | Ice cream | Food | [File page](https://commons.wikimedia.org/wiki/File:Ice_cream_flavor.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 20 |
+
| giraffe.jpg | Giraffe | Animals | [File page](https://commons.wikimedia.org/wiki/File:Giraffe_posing_in_the_grass.jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 21 |
+
| umbrella.jpg | Umbrella | Other | [File page](https://commons.wikimedia.org/wiki/File:Clear_Umbrella_Rain_Liverpool_(Unsplash).jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
| 22 |
+
| hot_air_balloon.jpg | Hot-air balloon | Transport | [File page](https://commons.wikimedia.org/wiki/File:Colorful_hot_air_balloon_in_flight_(Unsplash).jpg) | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) |
|
examples/airplane.jpg
ADDED
|
Git LFS Details
|
examples/bicycle.jpg
ADDED
|
Git LFS Details
|
examples/bird.jpg
ADDED
|
Git LFS Details
|
examples/boat.jpg
ADDED
|
Git LFS Details
|
examples/city_bus.jpg
ADDED
|
Git LFS Details
|
examples/cow.jpg
ADDED
|
Git LFS Details
|
examples/dog.jpg
ADDED
|
examples/elephant.jpg
ADDED
|
Git LFS Details
|
examples/giraffe.jpg
ADDED
|
Git LFS Details
|
examples/horse.jpg
ADDED
|
Git LFS Details
|
examples/hot_air_balloon.jpg
ADDED
|
examples/ice_cream.jpg
ADDED
|
examples/manifest.json
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"src": "./examples/dog.jpg",
|
| 4 |
+
"label": "Dog",
|
| 5 |
+
"category": "Animals",
|
| 6 |
+
"source": "https://commons.wikimedia.org/wiki/File:Golden_Retriever_%28Pet_Dog%29.jpg",
|
| 7 |
+
"license": "CC0 1.0"
|
| 8 |
+
},
|
| 9 |
+
{
|
| 10 |
+
"src": "./examples/train.jpg",
|
| 11 |
+
"label": "Train",
|
| 12 |
+
"category": "Transport",
|
| 13 |
+
"source": "https://commons.wikimedia.org/wiki/File:Rrogozhine_passenger_train.JPG",
|
| 14 |
+
"license": "CC0 1.0"
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"src": "./examples/pizza.jpg",
|
| 18 |
+
"label": "Pizza",
|
| 19 |
+
"category": "Food",
|
| 20 |
+
"source": "https://commons.wikimedia.org/wiki/File:Homemade_Pizza.jpg",
|
| 21 |
+
"license": "CC0 1.0"
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"src": "./examples/boat.jpg",
|
| 25 |
+
"label": "Boat",
|
| 26 |
+
"category": "Transport",
|
| 27 |
+
"source": "https://commons.wikimedia.org/wiki/File:Boat_of_Malaysia_Bomba.jpg",
|
| 28 |
+
"license": "CC0 1.0"
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"src": "./examples/cow.jpg",
|
| 32 |
+
"label": "Cow",
|
| 33 |
+
"category": "Animals",
|
| 34 |
+
"source": "https://commons.wikimedia.org/wiki/File:Cow_and_Calf.jpg",
|
| 35 |
+
"license": "CC0 1.0"
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"src": "./examples/horse.jpg",
|
| 39 |
+
"label": "Horse",
|
| 40 |
+
"category": "Animals",
|
| 41 |
+
"source": "https://commons.wikimedia.org/wiki/File:Horse-2166921.jpg",
|
| 42 |
+
"license": "CC0 1.0"
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"src": "./examples/elephant.jpg",
|
| 46 |
+
"label": "Elephant",
|
| 47 |
+
"category": "Animals",
|
| 48 |
+
"source": "https://commons.wikimedia.org/wiki/File:Elephant_-_Jardim_Zool%C3%B3gico_de_Bras%C3%ADlia_-_DSC09798.JPG",
|
| 49 |
+
"license": "CC0 1.0"
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"src": "./examples/bird.jpg",
|
| 53 |
+
"label": "Parrot",
|
| 54 |
+
"category": "Animals",
|
| 55 |
+
"source": "https://commons.wikimedia.org/wiki/File:%281218241%29_%22Parrot_bird%22_Psittaciformes_-_Amazon%2C_Brazil.jpg",
|
| 56 |
+
"license": "CC0 1.0"
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"src": "./examples/bicycle.jpg",
|
| 60 |
+
"label": "Bicycle",
|
| 61 |
+
"category": "Transport",
|
| 62 |
+
"source": "https://commons.wikimedia.org/wiki/File:Bicycle_10.jpg",
|
| 63 |
+
"license": "CC0 1.0"
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"src": "./examples/city_bus.jpg",
|
| 67 |
+
"label": "City bus",
|
| 68 |
+
"category": "Transport",
|
| 69 |
+
"source": "https://commons.wikimedia.org/wiki/File:Osaka-City-Bus_5046.jpg",
|
| 70 |
+
"license": "CC0 1.0"
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"src": "./examples/airplane.jpg",
|
| 74 |
+
"label": "Airplane",
|
| 75 |
+
"category": "Transport",
|
| 76 |
+
"source": "https://commons.wikimedia.org/wiki/File:Aleutian_Airways_plane_at_Anchorage_Airport.jpg",
|
| 77 |
+
"license": "CC0 1.0"
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"src": "./examples/sandwich.jpg",
|
| 81 |
+
"label": "Sandwich",
|
| 82 |
+
"category": "Food",
|
| 83 |
+
"source": "https://commons.wikimedia.org/wiki/File:Sandwich_at_Goodman%27s_Restaurant_in_Berkeley_Heights_NJ.jpg",
|
| 84 |
+
"license": "CC0 1.0"
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"src": "./examples/ice_cream.jpg",
|
| 88 |
+
"label": "Ice cream",
|
| 89 |
+
"category": "Food",
|
| 90 |
+
"source": "https://commons.wikimedia.org/wiki/File:Ice_cream_flavor.jpg",
|
| 91 |
+
"license": "CC0 1.0"
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"src": "./examples/giraffe.jpg",
|
| 95 |
+
"label": "Giraffe",
|
| 96 |
+
"category": "Animals",
|
| 97 |
+
"source": "https://commons.wikimedia.org/wiki/File:Giraffe_posing_in_the_grass.jpg",
|
| 98 |
+
"license": "CC0 1.0"
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"src": "./examples/umbrella.jpg",
|
| 102 |
+
"label": "Umbrella",
|
| 103 |
+
"category": "Other",
|
| 104 |
+
"source": "https://commons.wikimedia.org/wiki/File:Clear_Umbrella_Rain_Liverpool_(Unsplash).jpg",
|
| 105 |
+
"license": "CC0 1.0"
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"src": "./examples/hot_air_balloon.jpg",
|
| 109 |
+
"label": "Hot-air balloon",
|
| 110 |
+
"category": "Transport",
|
| 111 |
+
"source": "https://commons.wikimedia.org/wiki/File:Colorful_hot_air_balloon_in_flight_(Unsplash).jpg",
|
| 112 |
+
"license": "CC0 1.0"
|
| 113 |
+
}
|
| 114 |
+
]
|
examples/pizza.jpg
ADDED
|
Git LFS Details
|
examples/sandwich.jpg
ADDED
|
Git LFS Details
|
examples/train.jpg
ADDED
|
Git LFS Details
|
examples/umbrella.jpg
ADDED
|
Git LFS Details
|
how-to-start.html
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>How to Start · Image Captioning</title><link rel="stylesheet" href="./style.css"><link rel="stylesheet" href="./captioning.css"></head>
|
| 3 |
+
<body><main class="page-shell">
|
| 4 |
+
<nav class="guide-linkbar"><span>Image Captioning · Student guide</span><a href="./index.html">← Back to the demo</a></nav>
|
| 5 |
+
<header class="hero"><p class="eyebrow">Creative Technology · Build your own AI mini app</p><h1>How to Start</h1><p class="lede">Run a pretrained captioner, understand its output, and build one useful improvement.</p></header>
|
| 6 |
+
<section class="card guide-section"><h2>1. Explore the demo</h2><ol><li>Choose one of the example images or upload a PNG, JPEG, or WebP image.</li><li>Start with ViT-GPT2 and select <strong>Run AI Captioning</strong>.</li><li>Allow time for the first model download. Subsequent runs can reuse cached files. Use <strong>Stop this run</strong> if needed.</li><li>Read the caption and check its objects, actions, and relationships against the image.</li><li>Keep the same image and try Florence-2 or SmolVLM. Compare the actual sentences in the log.</li></ol></section>
|
| 7 |
+
<section class="card guide-section"><h2>2. Change one thing</h2><p>Florence-2 supports short and detailed captioning tasks. SmolVLM accepts a description instruction. ViT-GPT2 produces a short caption without an instruction. The output-token limit is not a target word count.</p><p>Choose one experiment: compare models, change the requested detail, or crop an image. Record both successes and failures. Download the comparison log before reloading the page.</p></section>
|
| 8 |
+
<section class="card guide-section"><h2>3. Make your own Hugging Face Space</h2><ol><li>Sign in to Hugging Face and open this project's Space page.</li><li>Use the Space menu to duplicate it into your own account. Alternatively, create a new <strong>Static</strong> Space and upload this folder's files with their subfolders.</li><li>Keep <code>sdk: static</code> and <code>app_file: index.html</code> in the README metadata.</li><li>Wait for the Space to build, open its App tab, and test one example and one uploaded image.</li></ol><p>This project needs no API key, paid inference endpoint, or training job. Internet access is required to obtain the library and model files.</p></section>
|
| 9 |
+
<section class="card guide-section"><h2>4. Find the code that matters</h2><div class="table-wrap"><table><thead><tr><th>File</th><th>What to change</th></tr></thead><tbody><tr><td>index.html</td><td>The task explanation and page structure.</td></tr><tr><td>app.js</td><td>Image selection, result display, and comparison records.</td></tr><tr><td>models.js</td><td>Model IDs, pinned revisions, and task settings.</td></tr><tr><td>captioner.js</td><td>Load the model, prepare inputs, generate tokens, decode the caption.</td></tr><tr><td>caption-worker.js</td><td>Run inference separately so the page stays responsive.</td></tr><tr><td>examples/</td><td>Teaching images, manifest, and source records.</td></tr></tbody></table></div><p>When changing a model, check its Transformers.js support and input format. A Python model ID cannot always be substituted directly.</p></section>
|
| 10 |
+
<section class="card guide-section"><h2>5. Explain and submit</h2><ul><li>A working public Space URL and a short explanation of the app's purpose.</li><li>The model ID and an explanation of the image → features → text process.</li><li>At least one successful example and one difficult or incorrect description.</li><li>One controlled comparison with its images, settings, and unchanged generated captions.</li><li>Image sources and a note on limitations. Training a model is optional.</li></ul></section>
|
| 11 |
+
<section class="card guide-section"><h2>If something does not work</h2><p><strong>Model download fails:</strong> check access to Hugging Face and jsDelivr, then retry. The first run loads roughly a few hundred MB per model.</p><p><strong>Generation is slow:</strong> use a recent desktop browser, close memory-heavy tabs, choose a shorter output limit, or stop the run. Loading time is shown separately from generation time.</p><p><strong>The sentence is wrong:</strong> save the actual output and identify the unsupported detail. Try another model or a clearer image; a fluent sentence is not a guarantee of correctness.</p><p><strong>Local files do not load:</strong> serve the folder over HTTP instead of opening index.html with file://.</p><pre><code>python -m http.server 7860</code></pre><p>Open <code>http://localhost:7860</code>. Uploaded images are processed in your browser. They are not sent to an inference server.</p><p><a href="./examples/SOURCES.md">Example image sources</a> · <a href="https://huggingface.co/docs/transformers.js">Transformers.js documentation</a></p></section>
|
| 12 |
+
<footer>AI-Powered Image Captioning · Student guide</footer>
|
| 13 |
+
</main></body></html>
|
index.html
CHANGED
|
@@ -1,19 +1,103 @@
|
|
| 1 |
<!doctype html>
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
| 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="An image captioning mini-lab using pretrained Hugging Face models.">
|
| 7 |
+
<title>AI-Powered Image Captioning</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">
|
| 10 |
+
<link rel="stylesheet" href="./captioning.css">
|
| 11 |
+
</head>
|
| 12 |
+
<body>
|
| 13 |
+
<main class="page-shell">
|
| 14 |
+
<nav class="guide-linkbar" aria-label="Course guide">
|
| 15 |
+
<span>Building your own AI mini app?</span>
|
| 16 |
+
<a href="./how-to-start.html">How to Start <span aria-hidden="true">→</span></a>
|
| 17 |
+
</nav>
|
| 18 |
+
<header class="hero">
|
| 19 |
+
<p class="eyebrow">Creative Technology · Computer Vision</p>
|
| 20 |
+
<h1>AI-Powered Image Captioning</h1>
|
| 21 |
+
<p class="lede">Choose an example or upload your own image. Then choose a pretrained captioner and let AI answer: <strong>what is happening in this image?</strong></p>
|
| 22 |
+
<div class="lesson-strip" aria-label="Mini-lab overview">
|
| 23 |
+
<span><b>Task</b> Image captioning</span>
|
| 24 |
+
<span><b>Output</b> A description in English</span>
|
| 25 |
+
<span><b>Models</b> ViT-GPT2 · Florence-2 · SmolVLM</span>
|
| 26 |
+
</div>
|
| 27 |
+
</header>
|
| 28 |
+
<section class="workspace" aria-label="Image captioning mini-lab">
|
| 29 |
+
<div class="controls card">
|
| 30 |
+
<div class="step"><span class="step-number">1</span><div>
|
| 31 |
+
<p class="control-label sample-title">Choose an image</p>
|
| 32 |
+
<p class="helper">Start with an example, or try a photo of your own.</p>
|
| 33 |
+
</div></div>
|
| 34 |
+
<div class="sample-head example-head"><div>
|
| 35 |
+
<p class="input-kicker">Start here · Example images</p>
|
| 36 |
+
<p class="helper">Four images from a bank of 16 teaching examples.</p>
|
| 37 |
+
</div><button id="shuffle-examples" class="shuffle-button" type="button" disabled>Shuffle</button></div>
|
| 38 |
+
<div id="sample-grid" class="sample-grid" aria-label="Example images"></div>
|
| 39 |
+
<div class="or-row"><span>or try your own</span></div>
|
| 40 |
+
<label class="control-label" for="image-input">Upload an image</label>
|
| 41 |
+
<p class="helper">PNG, JPEG, or WebP · up to 12 MB.</p>
|
| 42 |
+
<input id="image-input" class="file-input" type="file" accept="image/png,image/jpeg,image/webp">
|
| 43 |
+
<div class="divider"></div>
|
| 44 |
+
<div class="step"><span class="step-number">2</span><div class="grow">
|
| 45 |
+
<label class="control-label" for="model-select">Choose captioner</label>
|
| 46 |
+
<select id="model-select" class="model-select"></select>
|
| 47 |
+
<p id="model-note" class="helper"></p>
|
| 48 |
+
</div></div>
|
| 49 |
+
<div class="divider"></div>
|
| 50 |
+
<div class="step"><span class="step-number">3</span><div class="grow">
|
| 51 |
+
<label class="control-label" for="detail-select">Description style</label>
|
| 52 |
+
<select id="detail-select" class="model-select"><option value="short">Short caption</option><option value="detailed">Detailed description</option></select>
|
| 53 |
+
<p id="detail-note" class="helper"></p>
|
| 54 |
+
<div id="instruction-controls" class="clip-controls" hidden>
|
| 55 |
+
<label class="control-label" for="instruction">Description instruction</label>
|
| 56 |
+
<textarea id="instruction" rows="3" maxlength="400" spellcheck="false">Describe this image in one short sentence.</textarea>
|
| 57 |
+
<p class="helper">Keep the image fixed and change the instruction to compare.</p>
|
| 58 |
+
</div>
|
| 59 |
+
<div class="token-controls">
|
| 60 |
+
<div class="slider-row"><label class="control-label" for="max-tokens">Maximum new tokens</label><output id="token-value" for="max-tokens">64</output></div>
|
| 61 |
+
<input id="max-tokens" type="range" min="32" max="128" value="64" step="16">
|
| 62 |
+
<p class="helper">An output limit, not an exact word count. A low limit may cut a sentence short.</p>
|
| 63 |
+
</div>
|
| 64 |
+
</div></div>
|
| 65 |
+
<button id="run-button" class="run-button" type="button" disabled>Run AI Captioning</button>
|
| 66 |
+
<button id="stop-button" class="secondary-button stop-button" type="button" hidden>Stop this run</button>
|
| 67 |
+
<p id="status" class="status" role="status" aria-live="polite">Choose an example or upload your own image.</p>
|
| 68 |
+
<div id="progress-wrap" class="progress-wrap" hidden><div id="progress-bar" class="progress-bar"></div></div>
|
| 69 |
+
</div>
|
| 70 |
+
<div class="result card">
|
| 71 |
+
<div class="result-head"><div><p class="section-kicker">AI description</p><h2>How does the model describe it?</h2></div><span id="runtime-badge" class="badge">Waiting</span></div>
|
| 72 |
+
<div id="preview-wrap" class="preview-wrap empty"><img id="preview-image" alt="Selected image"><p id="preview-placeholder">Choose an image first. It will appear here before captioning.</p></div>
|
| 73 |
+
<p id="image-source" class="helper" hidden></p>
|
| 74 |
+
<div id="summary" class="summary caption-output" aria-live="polite">No caption yet.</div>
|
| 75 |
+
<div id="run-meta" class="run-meta" hidden>
|
| 76 |
+
<span><b>Model</b><span id="meta-model">—</span></span>
|
| 77 |
+
<span><b>Image</b><span id="meta-image">—</span></span>
|
| 78 |
+
<span><b>Generation time</b><span id="meta-runtime">—</span></span>
|
| 79 |
+
</div>
|
| 80 |
+
<div class="caption-actions"><button id="copy-caption" class="secondary-button" disabled>Copy caption</button><button id="download-caption" class="secondary-button" disabled>Save caption</button></div>
|
| 81 |
+
<div class="table-wrap"><table><thead><tr><th>Check the description</th><th>Look for visual evidence</th></tr></thead><tbody>
|
| 82 |
+
<tr><td>Objects and attributes</td><td>Are the named objects and their details visible?</td></tr>
|
| 83 |
+
<tr><td>Actions and relationships</td><td>Does the sentence explain what is actually happening?</td></tr>
|
| 84 |
+
<tr><td>Missing or added details</td><td>What is omitted? What has the model added without evidence?</td></tr>
|
| 85 |
+
</tbody></table></div>
|
| 86 |
+
</div>
|
| 87 |
+
</section>
|
| 88 |
+
<section class="explain card">
|
| 89 |
+
<p class="section-kicker">What you are doing</p>
|
| 90 |
+
<h2 class="explain-title">Use a learned AI capability instead of rebuilding the traditional task from scratch.</h2>
|
| 91 |
+
<p class="explain-copy">A visual encoder represents the image. A language decoder generates a description. Different models may describe different details, and a fluent sentence can still be wrong.</p>
|
| 92 |
+
<div class="flow" aria-label="Image captioning flow"><span>Example or your image</span><span class="arrow">→</span><span>Selected pretrained captioner</span><span class="arrow">→</span><span>A description in words</span></div>
|
| 93 |
+
<p class="extension-note"><strong>Compare models:</strong> keep the image fixed, switch captioner, and check the objects, actions, and relationships in each sentence.</p>
|
| 94 |
+
</section>
|
| 95 |
+
<section class="history card" aria-labelledby="history-title">
|
| 96 |
+
<div class="history-head"><div><p class="section-kicker">Comparison log</p><h2 id="history-title" class="explain-title">Keep one image fixed and compare descriptions.</h2><p class="history-copy">The latest 12 successful runs stay here until you clear the log or reload. Generation time excludes model loading; download the log to keep the full settings.</p></div><div class="history-actions"><button id="download-history" class="secondary-button" disabled>Download log</button><button id="clear-history" class="secondary-button" disabled>Clear log</button></div></div>
|
| 97 |
+
<div class="table-wrap"><table><thead><tr><th>Run</th><th>Image</th><th>Model</th><th>Caption</th><th>Style</th><th>Time</th></tr></thead><tbody id="history-body"><tr class="empty-row"><td colspan="6">Run two models on the same image to start a comparison.</td></tr></tbody></table></div>
|
| 98 |
+
</section>
|
| 99 |
+
<footer>Hosted as a Hugging Face Static Space · Powered by Transformers.js</footer>
|
| 100 |
+
</main>
|
| 101 |
+
<script type="module" src="./app.js"></script>
|
| 102 |
+
</body>
|
| 103 |
</html>
|
models.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export const TRANSFORMERS_VERSION = '4.2.0';
|
| 2 |
+
export const MODEL_OPTIONS = Object.freeze([
|
| 3 |
+
{
|
| 4 |
+
key: 'vit', label: 'ViT-GPT2',
|
| 5 |
+
modelId: 'Xenova/vit-gpt2-image-captioning',
|
| 6 |
+
revision: '215b4edcb7ec1fad5905a18a03f7b2007f6fabd0',
|
| 7 |
+
note: 'Start here: a visual encoder and a language decoder generate a short caption.',
|
| 8 |
+
detailNote: 'This model generates a short caption. Use Florence-2 or SmolVLM to change the description style.',
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
key: 'florence', label: 'Florence-2 Base',
|
| 12 |
+
modelId: 'onnx-community/Florence-2-base-ft',
|
| 13 |
+
revision: 'e88a44eaf3791a35eae0c5a47b3dbcd36e67eb6f',
|
| 14 |
+
note: 'A vision-language model with task prompts for short and detailed descriptions.',
|
| 15 |
+
detailNote: 'Short and detailed styles use the model’s corresponding captioning task prompts.',
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
key: 'smol', label: 'SmolVLM 256M',
|
| 19 |
+
modelId: 'HuggingFaceTB/SmolVLM-256M-Instruct',
|
| 20 |
+
revision: '7e3e67edbbed1bf9888184d9df282b700a323964',
|
| 21 |
+
note: 'A compact instruction-following model. Explore how the wording of your request changes the description.',
|
| 22 |
+
detailNote: 'Choose a style to fill in an instruction, or write your own request below.',
|
| 23 |
+
},
|
| 24 |
+
]);
|
| 25 |
+
|
| 26 |
+
export function getModelOption(key) {
|
| 27 |
+
const model = MODEL_OPTIONS.find(item => item.key === key);
|
| 28 |
+
if (!model) throw new Error('Unknown captioning model.');
|
| 29 |
+
return model;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
export function defaultInstruction(style) {
|
| 33 |
+
return style === 'detailed'
|
| 34 |
+
? 'Describe the visible objects, actions, and relationships in this image in a few sentences.'
|
| 35 |
+
: 'Describe this image in one short sentence.';
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
export function normalizeOptions(options) {
|
| 39 |
+
const model = getModelOption(options.modelKey);
|
| 40 |
+
const maxTokens = Number(options.maxTokens);
|
| 41 |
+
if (!Number.isInteger(maxTokens) || maxTokens < 32 || maxTokens > 128) throw new Error('Choose an output limit from 32 to 128 tokens.');
|
| 42 |
+
if (!['short', 'detailed'].includes(options.style)) throw new Error('Choose a valid description style.');
|
| 43 |
+
const style = model.key === 'vit' ? 'short' : options.style;
|
| 44 |
+
const instruction = String(options.instruction ?? '').trim();
|
| 45 |
+
if (model.key === 'smol' && (!instruction || instruction.length > 400)) throw new Error('Enter an instruction between 1 and 400 characters.');
|
| 46 |
+
return {
|
| 47 |
+
modelKey: model.key, maxTokens, style,
|
| 48 |
+
instruction: model.key === 'smol' ? instruction : null,
|
| 49 |
+
task: model.key === 'florence' ? (style === 'short' ? '<CAPTION>' : '<MORE_DETAILED_CAPTION>') : null,
|
| 50 |
+
};
|
| 51 |
+
}
|
style.css
CHANGED
|
@@ -1,28 +1,283 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
body {
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
}
|
| 5 |
|
| 6 |
h1 {
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
}
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
}
|
| 17 |
|
| 18 |
.card {
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
}
|
|
|
|
| 1 |
+
:root {
|
| 2 |
+
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
|
| 3 |
+
color: #111827;
|
| 4 |
+
background: #f6f7fb;
|
| 5 |
+
font-synthesis: none;
|
| 6 |
+
}
|
| 7 |
+
|
| 8 |
+
* { box-sizing: border-box; }
|
| 9 |
+
|
| 10 |
body {
|
| 11 |
+
margin: 0;
|
| 12 |
+
min-height: 100vh;
|
| 13 |
+
background: radial-gradient(circle at top, #ffffff 0%, #f6f7fb 45%, #eef1f7 100%);
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
button, input, select, textarea { font: inherit; }
|
| 17 |
+
|
| 18 |
+
.page-shell {
|
| 19 |
+
width: min(1180px, calc(100% - 32px));
|
| 20 |
+
margin: 0 auto;
|
| 21 |
+
padding: 28px 0 24px;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
.guide-linkbar {
|
| 25 |
+
display: flex;
|
| 26 |
+
align-items: center;
|
| 27 |
+
justify-content: space-between;
|
| 28 |
+
flex-wrap: wrap;
|
| 29 |
+
gap: 10px;
|
| 30 |
+
margin: 0 0 24px;
|
| 31 |
+
padding: 11px 15px;
|
| 32 |
+
border: 1px solid #d6e3f4;
|
| 33 |
+
border-radius: 14px;
|
| 34 |
+
background: #edf5ff;
|
| 35 |
+
color: #334965;
|
| 36 |
+
font-size: .9rem;
|
| 37 |
+
font-weight: 700;
|
| 38 |
+
}
|
| 39 |
+
.guide-linkbar a { display: inline-flex; align-items: center; gap: 5px; border-radius: 8px; color: #174f9e; font-weight: 800; text-decoration: none; }
|
| 40 |
+
.guide-linkbar a:hover { text-decoration: underline; }
|
| 41 |
+
.guide-linkbar a:focus-visible { outline: 3px solid #f7b844; outline-offset: 3px; }
|
| 42 |
+
|
| 43 |
+
.hero { width: 100%; margin-bottom: 20px; }
|
| 44 |
+
.eyebrow, .section-kicker {
|
| 45 |
+
margin: 0 0 10px;
|
| 46 |
+
font-size: .78rem;
|
| 47 |
+
font-weight: 750;
|
| 48 |
+
letter-spacing: .08em;
|
| 49 |
+
text-transform: uppercase;
|
| 50 |
+
color: #52627a;
|
| 51 |
}
|
| 52 |
|
| 53 |
h1 {
|
| 54 |
+
margin: 0;
|
| 55 |
+
font-size: clamp(2.35rem, 5vw, 4rem);
|
| 56 |
+
line-height: 1;
|
| 57 |
+
letter-spacing: -.045em;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
.lede {
|
| 61 |
+
max-width: 940px;
|
| 62 |
+
margin: 14px 0 0;
|
| 63 |
+
font-size: clamp(1.05rem, 2vw, 1.3rem);
|
| 64 |
+
line-height: 1.5;
|
| 65 |
+
color: #4b5563;
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
.lesson-strip { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 14px; }
|
| 69 |
+
.lesson-strip span {
|
| 70 |
+
padding: 8px 11px;
|
| 71 |
+
border: 1px solid #dfe4ec;
|
| 72 |
+
border-radius: 999px;
|
| 73 |
+
background: rgba(255,255,255,.76);
|
| 74 |
+
color: #52627a;
|
| 75 |
+
font-size: .86rem;
|
| 76 |
}
|
| 77 |
+
.lesson-strip b { margin-right: 4px; color: #111827; }
|
| 78 |
|
| 79 |
+
.workspace {
|
| 80 |
+
display: grid;
|
| 81 |
+
grid-template-columns: minmax(280px, .72fr) minmax(0, 1.45fr);
|
| 82 |
+
gap: 20px;
|
| 83 |
+
align-items: start;
|
| 84 |
}
|
| 85 |
|
| 86 |
.card {
|
| 87 |
+
border: 1px solid #e0e5ed;
|
| 88 |
+
border-radius: 22px;
|
| 89 |
+
background: rgba(255,255,255,.93);
|
| 90 |
+
box-shadow: 0 18px 50px rgba(26,39,63,.08);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.controls, .result { padding: 24px; }
|
| 94 |
+
.result { position: sticky; top: 16px; }
|
| 95 |
+
.step { display: flex; gap: 13px; align-items: flex-start; }
|
| 96 |
+
.step-number {
|
| 97 |
+
display: grid;
|
| 98 |
+
place-items: center;
|
| 99 |
+
width: 28px;
|
| 100 |
+
height: 28px;
|
| 101 |
+
flex: 0 0 28px;
|
| 102 |
+
border-radius: 50%;
|
| 103 |
+
background: #111827;
|
| 104 |
+
color: #fff;
|
| 105 |
+
font-size: .82rem;
|
| 106 |
+
font-weight: 750;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
.grow { flex: 1; min-width: 0; }
|
| 110 |
+
.control-label { font-weight: 700; }
|
| 111 |
+
.helper { margin: 6px 0 0; color: #667085; font-size: .92rem; line-height: 1.5; }
|
| 112 |
+
.sample-title { margin: 0; }
|
| 113 |
+
.sample-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
|
| 114 |
+
.shuffle-button {
|
| 115 |
+
flex: 0 0 auto;
|
| 116 |
+
padding: 7px 10px;
|
| 117 |
+
border: 1px solid #d8dee8;
|
| 118 |
+
border-radius: 10px;
|
| 119 |
+
background: #fff;
|
| 120 |
+
color: #344054;
|
| 121 |
+
cursor: pointer;
|
| 122 |
+
font-size: .82rem;
|
| 123 |
+
font-weight: 700;
|
| 124 |
+
}
|
| 125 |
+
.shuffle-button:hover:not(:disabled) { background: #f7f8fa; }
|
| 126 |
+
.shuffle-button:disabled { opacity: .45; cursor: not-allowed; }
|
| 127 |
+
.input-kicker {
|
| 128 |
+
margin: 0 0 4px;
|
| 129 |
+
color: #344054;
|
| 130 |
+
font-size: .78rem;
|
| 131 |
+
font-weight: 800;
|
| 132 |
+
letter-spacing: .04em;
|
| 133 |
+
text-transform: uppercase;
|
| 134 |
+
}
|
| 135 |
+
.example-head { margin-top: 18px; }
|
| 136 |
+
|
| 137 |
+
.sample-grid {
|
| 138 |
+
display: grid;
|
| 139 |
+
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 140 |
+
gap: 10px;
|
| 141 |
+
margin-top: 12px;
|
| 142 |
+
}
|
| 143 |
+
.sample-card {
|
| 144 |
+
min-width: 0;
|
| 145 |
+
padding: 0;
|
| 146 |
+
overflow: hidden;
|
| 147 |
+
border: 1px solid #e0e5ed;
|
| 148 |
+
border-radius: 12px;
|
| 149 |
+
background: #fff;
|
| 150 |
+
cursor: pointer;
|
| 151 |
+
text-align: left;
|
| 152 |
+
transition: border-color .15s ease, transform .15s ease, box-shadow .15s ease;
|
| 153 |
+
}
|
| 154 |
+
.sample-card:hover { transform: translateY(-1px); border-color: #aeb8c6; box-shadow: 0 8px 18px rgba(26,39,63,.08); }
|
| 155 |
+
.sample-card.selected { border-color: #111827; box-shadow: 0 0 0 2px #111827; }
|
| 156 |
+
.sample-card img { display: block; width: 100%; aspect-ratio: 4/3; object-fit: cover; background: #eef1f5; }
|
| 157 |
+
.sample-card span {
|
| 158 |
+
display: block;
|
| 159 |
+
padding: 8px 9px 9px;
|
| 160 |
+
overflow: hidden;
|
| 161 |
+
color: #475467;
|
| 162 |
+
font-size: .78rem;
|
| 163 |
+
font-weight: 700;
|
| 164 |
+
text-overflow: ellipsis;
|
| 165 |
+
white-space: nowrap;
|
| 166 |
+
}
|
| 167 |
+
.sample-card small { display: block; padding: 0 9px 9px; color: #8a94a4; font-size: .67rem; line-height: 1.2; letter-spacing: .01em; }
|
| 168 |
+
|
| 169 |
+
.or-row { display: flex; align-items: center; gap: 10px; margin: 18px 0 14px; color: #98a2b3; font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; }
|
| 170 |
+
.or-row::before, .or-row::after { content: ''; height: 1px; flex: 1; background: #eceff4; }
|
| 171 |
+
.file-input { width: 100%; margin-top: 16px; padding: 11px; border: 1px dashed #bac3d2; border-radius: 14px; background: #fafbfc; }
|
| 172 |
+
.divider { height: 1px; margin: 22px 0; background: #eceff4; }
|
| 173 |
+
.model-select, textarea {
|
| 174 |
+
width: 100%;
|
| 175 |
+
margin-top: 10px;
|
| 176 |
+
padding: 11px 12px;
|
| 177 |
+
border: 1px solid #cfd7e3;
|
| 178 |
+
border-radius: 12px;
|
| 179 |
+
background: #fff;
|
| 180 |
+
color: #111827;
|
| 181 |
+
}
|
| 182 |
+
.model-select:focus, textarea:focus { outline: 2px solid #111827; outline-offset: 2px; }
|
| 183 |
+
.clip-controls { margin-top: 18px; }
|
| 184 |
+
textarea { resize: vertical; line-height: 1.45; }
|
| 185 |
+
.slider-row { display: flex; justify-content: space-between; gap: 12px; }
|
| 186 |
+
#top-k-value { min-width: 48px; text-align: right; color: #334155; font-variant-numeric: tabular-nums; }
|
| 187 |
+
#top-k { width: 100%; margin-top: 13px; accent-color: #111827; }
|
| 188 |
+
|
| 189 |
+
.run-button {
|
| 190 |
+
width: 100%;
|
| 191 |
+
margin-top: 24px;
|
| 192 |
+
padding: 13px 18px;
|
| 193 |
+
border: 0;
|
| 194 |
+
border-radius: 14px;
|
| 195 |
+
background: #111827;
|
| 196 |
+
color: #fff;
|
| 197 |
+
font-weight: 750;
|
| 198 |
+
cursor: pointer;
|
| 199 |
+
transition: transform .15s ease, opacity .15s ease;
|
| 200 |
+
}
|
| 201 |
+
.run-button:not(:disabled):hover { transform: translateY(-1px); }
|
| 202 |
+
.run-button:disabled { opacity: .38; cursor: not-allowed; }
|
| 203 |
+
.status { min-height: 1.4em; margin: 14px 0 0; color: #52627a; font-size: .9rem; }
|
| 204 |
+
.progress-wrap { height: 5px; margin-top: 10px; overflow: hidden; border-radius: 999px; background: #edf0f5; }
|
| 205 |
+
.progress-bar { width: 0; height: 100%; border-radius: inherit; background: #111827; transition: width .2s ease; }
|
| 206 |
+
|
| 207 |
+
.result-head { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; margin-bottom: 16px; }
|
| 208 |
+
.result h2 { margin: 0; font-size: 1.35rem; letter-spacing: -.02em; }
|
| 209 |
+
.badge { padding: 7px 10px; border-radius: 999px; background: #eef2f7; color: #536176; font-size: .82rem; font-weight: 700; white-space: nowrap; }
|
| 210 |
+
.badge.ready { background: #e8f7ef; color: #17623c; }
|
| 211 |
+
.badge.running { background: #fff4df; color: #805600; }
|
| 212 |
+
.badge.error { background: #fdecec; color: #9b2c2c; }
|
| 213 |
+
|
| 214 |
+
.preview-wrap {
|
| 215 |
+
position: relative;
|
| 216 |
+
display: grid;
|
| 217 |
+
place-items: center;
|
| 218 |
+
min-height: 360px;
|
| 219 |
+
overflow: hidden;
|
| 220 |
+
border-radius: 16px;
|
| 221 |
+
background: #101318;
|
| 222 |
+
}
|
| 223 |
+
#preview-image { display: block; max-width: 100%; max-height: 560px; object-fit: contain; }
|
| 224 |
+
.preview-wrap.empty #preview-image { display: none; }
|
| 225 |
+
#preview-placeholder { margin: 0; padding: 32px; color: #9aa4b2; text-align: center; }
|
| 226 |
+
.preview-wrap:not(.empty) #preview-placeholder { display: none; }
|
| 227 |
+
.summary { margin: 16px 0; padding: 13px 15px; border-radius: 13px; background: #f7f8fa; color: #344054; font-weight: 650; line-height: 1.5; }
|
| 228 |
+
.run-meta {
|
| 229 |
+
display: grid;
|
| 230 |
+
grid-template-columns: repeat(3, minmax(0, 1fr));
|
| 231 |
+
gap: 8px;
|
| 232 |
+
margin: -5px 0 16px;
|
| 233 |
+
}
|
| 234 |
+
.run-meta[hidden] { display: none; }
|
| 235 |
+
.run-meta > span { min-width: 0; padding: 9px 10px; border: 1px solid #e8ebf0; border-radius: 11px; background: #fff; }
|
| 236 |
+
.run-meta b { display: block; margin-bottom: 3px; color: #7a8595; font-size: .7rem; letter-spacing: .05em; text-transform: uppercase; }
|
| 237 |
+
.run-meta span span { display: block; overflow: hidden; color: #344054; font-size: .82rem; font-weight: 700; text-overflow: ellipsis; white-space: nowrap; }
|
| 238 |
+
.table-wrap { overflow-x: auto; border: 1px solid #e8ebf0; border-radius: 14px; }
|
| 239 |
+
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
|
| 240 |
+
th, td { padding: 11px 13px; border-bottom: 1px solid #edf0f4; text-align: left; vertical-align: top; }
|
| 241 |
+
th { background: #fafbfc; color: #52627a; font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; }
|
| 242 |
+
tbody tr:last-child td { border-bottom: 0; }
|
| 243 |
+
.empty-row td { color: #7d899a; text-align: center; }
|
| 244 |
+
.score-wrap { display: grid; grid-template-columns: 58px minmax(70px, 1fr); gap: 9px; align-items: center; font-variant-numeric: tabular-nums; }
|
| 245 |
+
.score-track { display: block; height: 7px; overflow: hidden; border-radius: 999px; background: #edf0f5; }
|
| 246 |
+
.score-fill { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #111827, #5e6c84); }
|
| 247 |
+
|
| 248 |
+
.explain { margin-top: 16px; padding: 18px 24px; }
|
| 249 |
+
.explain-title { margin: 0; font-size: clamp(1.15rem, 1.8vw, 1.45rem); line-height: 1.25; letter-spacing: -.02em; }
|
| 250 |
+
.explain-copy { margin: 10px 0 14px; color: #536176; line-height: 1.55; }
|
| 251 |
+
.flow { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; font-weight: 700; }
|
| 252 |
+
.flow span:not(.arrow) { padding: 8px 11px; border-radius: 10px; background: #f2f4f7; }
|
| 253 |
+
.arrow { color: #98a2b3; }
|
| 254 |
+
.extension-note { margin: 12px 0 0; padding-top: 11px; border-top: 1px solid #eceff4; color: #5d6879; font-size: .92rem; line-height: 1.5; }
|
| 255 |
+
.history { margin-top: 16px; padding: 20px 24px 24px; }
|
| 256 |
+
.history-head { display: flex; justify-content: space-between; gap: 20px; align-items: flex-start; margin-bottom: 15px; }
|
| 257 |
+
.history-copy { margin: 8px 0 0; color: #5d6879; font-size: .92rem; line-height: 1.5; }
|
| 258 |
+
.secondary-button { flex: 0 0 auto; padding: 9px 12px; border: 1px solid #cfd7e3; border-radius: 11px; background: #fff; color: #344054; font-weight: 700; cursor: pointer; }
|
| 259 |
+
.secondary-button:hover:not(:disabled) { border-color: #8f9bad; }
|
| 260 |
+
.secondary-button:disabled { opacity: .45; cursor: not-allowed; }
|
| 261 |
+
.runtime-note { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1px; margin-top: 16px; overflow: hidden; background: #e5e9f0; }
|
| 262 |
+
.runtime-note div { padding: 16px 18px; background: rgba(255,255,255,.96); }
|
| 263 |
+
.runtime-note b { display: block; margin-bottom: 5px; color: #111827; font-size: .88rem; }
|
| 264 |
+
.runtime-note span { color: #667085; font-size: .84rem; line-height: 1.45; }
|
| 265 |
+
footer { padding: 14px 2px 0; color: #7a8595; font-size: .84rem; }
|
| 266 |
+
|
| 267 |
+
@media (max-width: 820px) {
|
| 268 |
+
.page-shell { width: min(100% - 20px, 720px); padding: 22px 0 20px; }
|
| 269 |
+
.workspace { grid-template-columns: 1fr; }
|
| 270 |
+
.result { position: static; }
|
| 271 |
+
.preview-wrap { min-height: 280px; }
|
| 272 |
+
.runtime-note { grid-template-columns: 1fr; }
|
| 273 |
}
|
| 274 |
|
| 275 |
+
@media (max-width: 520px) {
|
| 276 |
+
.card { border-radius: 17px; }
|
| 277 |
+
.controls, .result, .explain, .history { padding: 18px; }
|
| 278 |
+
.result-head { align-items: center; }
|
| 279 |
+
.run-meta { grid-template-columns: 1fr; }
|
| 280 |
+
.history-head { display: block; }
|
| 281 |
+
.secondary-button { margin-top: 12px; }
|
| 282 |
+
th, td { padding: 9px 10px; }
|
| 283 |
}
|