--- 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. Start with **ViT-GPT2**, then select **Run AI Captioning**. 3. Allow the model to load on the first run. Read the generated caption and inspect the image. 4. Keep the image fixed and try **Florence-2** or **SmolVLM**. 5. Compare objects, attributes, actions, relationships, omissions, and unsupported details. 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 `` and detailed descriptions with `` | | 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, controls, caption output, and comparison table. - `style.css` β€” the existing image-classification site's shared style, kept unchanged. - `captioning.css` β€” additions for caption-specific controls and the guide. - `models.js` β€” model IDs, revisions, and validated generation settings. - `captioner.js` β€” load β†’ prepare β†’ generate β†’ decode. - `caption-worker.js` β€” inference in a worker; stopping a run terminates that worker. - `app.js` β€” input validation, image selection, captions, 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. 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. 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.