Spaces:
Running
Download README.md from Mollysc/Topic5APIC: direct link, hf CLI and curl.
- Browser
- Download file 6.95 kB
-
https://huggingface.co/spaces/Mollysc/Topic5APIC/resolve/main/README.md
- Command line
-
hf download hf://spaces/Mollysc/Topic5APIC/README.md
-
curl -L -o README.md https://huggingface.co/spaces/Mollysc/Topic5APIC/resolve/main/README.md
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
- Choose an example or upload a PNG, JPEG, or WebP image up to 12 MB.
- Start with ViT-GPT2, then select Run AI Captioning.
- Allow the model to load on the first run. Read the generated caption and inspect the image.
- Keep the image fixed and try Florence-2 or SmolVLM.
- Compare objects, attributes, actions, relationships, omissions, and unsupported details.
- 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 | A visual encoder and a language decoder; short captions without a text instruction |
| Florence-2 Base | onnx-community/Florence-2-base-ft | Short captions with <CAPTION> and detailed descriptions with <MORE_DETAILED_CAPTION> |
| SmolVLM 256M | 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.
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:
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 — an early encoder–decoder captioning model.
- Show, Attend and Tell — visual attention.
- Florence-2 paper — unified vision tasks and task prompts.
- SmolVLM model card — the instruction-following model used here.
- Object Hallucination in Image Captioning — why plausible captions can mention unsupported objects.
- Transformers.js documentation — browser inference library.