Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ Example output:
```
there is a chamelon sitting on a branch in the woods
```

The model can also be run on the GPU using the `--gpu` flag
```bash
blip-caption IMG_5825.jpeg --gpu
```

Here's [the image I used](https://static.simonwillison.net/static/2023/IMG_5924.jpeg):

![It is ineded a chameleon](https://static.simonwillison.net/static/2023/IMG_5924.jpeg)
Expand Down
12 changes: 11 additions & 1 deletion blip_caption.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
import json
import PIL
import torch
from transformers import pipeline


Expand All @@ -11,11 +12,20 @@
nargs=-1,
required=True,
)
@click.option("gpu","--gpu", is_flag=True, default=False, help="Run the model on a GPU")
@click.option("--large", is_flag=True, help="Use the large model")
@click.option("json_", "--json", is_flag=True, help="Output as JSON")
def cli(paths, large, json_):
def cli(paths, large, gpu, json_):
device = -1
if gpu:
if torch.cuda.is_available():
device = 0
else:
click.echo("No GPU available despite specifying --gpu. Defaulting to CPU")

captioner = pipeline(
"image-to-text",
device=device,
model="Salesforce/blip-image-captioning-base"
if not large
else "Salesforce/blip-image-captioning-large",
Expand Down