Topic5APIC / README.md
cltsangwork's picture
Upload 6 files
4bbb9ea verified
|
Raw History Blame Contribute Delete
7.44 kB
---
title: AI-Powered Image Captioning
emoji: 🖼️
colorFrom: blue
colorTo: indigo
sdk: static
app_file: index.html
pinned: false
models:
- Xenova/vit-gpt2-image-captioning
- onnx-community/Florence-2-base-ft
- HuggingFaceTB/SmolVLM-256M-Instruct
---
# AI-Powered Image Captioning
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.
## Start with a working example
1. Choose an example or upload a PNG, JPEG, or WebP image up to 12 MB.
2. Select **Run all three captioners**. One image is shared by all three columns.
3. Allow the models to load on the first run. Each column shows its caption, timings, and its own options.
4. Compare objects, attributes, actions, relationships, omissions, and unsupported details side by side.
5. Keep the image fixed and change one captioner's options, such as Florence-2's description style or SmolVLM's instruction, then run again.
6. Download the comparison log before reloading to retain the captions and exact settings.
The **How to Start** page explains the workflow, file structure, Space duplication, and student submission. No model training or API key is required.
## Models and controls
| Captioner | Model | What to explore |
|---|---|---|
| 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 |
| 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>` |
| 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 |
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.
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.
## Application files
- `index.html` — course page, shared image, three captioner columns, and comparison table.
- `style.css` — the existing image-classification site's shared style, kept unchanged.
- `captioning.css` — the side-by-side captioner layout, caption controls, and guide styles.
- `models.js` — model IDs, revisions, and validated generation settings.
- `captioner.js` — load → prepare → generate → decode; loaded models stay cached for reruns.
- `caption-worker.js` — inference in a worker; stopping a run terminates that worker.
- `app.js` — input validation, image selection, sequential captioning, and log export.
- `how-to-start.html` — student guide.
- `examples/manifest.json` — gallery items and their sources.
- `examples/SOURCES.md` — source and license records for the 86 images.
## Runtime and image handling
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.
The first run loads roughly a few hundred MB per model, and the three captioners are generated one after another in the same worker. A loaded model stays cached in memory so a rerun does not reinitialize it; stopping a run terminates the worker and releases every model. Because all three can be resident at once, a recent desktop browser with free memory is recommended. Browser cache availability, memory, device speed, and network access affect runtime. An interrupted or failed captioner can be retried, and a model is stopped if it exceeds ten minutes.
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.
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.
## Example images
The 86 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).
## Deploy or preview
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.
For a local preview, run the following in this folder:
```bash
python -m http.server 7860
```
Open `http://localhost:7860`. Do not open the HTML directly with `file://`; browser modules and workers need an HTTP origin.
## Classroom assignment
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.
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.
## Reading
- [Show and Tell](https://arxiv.org/abs/1411.4555) — an early encoder–decoder captioning model.
- [Show, Attend and Tell](https://proceedings.mlr.press/v37/xuc15.html) — visual attention.
- [Florence-2 paper](https://arxiv.org/abs/2311.06242) — unified vision tasks and task prompts.
- [SmolVLM model card](https://huggingface.co/HuggingFaceTB/SmolVLM-256M-Instruct) — the instruction-following model used here.
- [Object Hallucination in Image Captioning](https://aclanthology.org/D18-1437/) — why plausible captions can mention unsupported objects.
- [Transformers.js documentation](https://huggingface.co/docs/transformers.js) — browser inference library.