Download utils.py from Vasudevakrishna/ERAv2_GenerateCaptions: direct link, hf CLI and curl.
- Browser
- Download file 461 Bytes
-
https://huggingface.co/spaces/Vasudevakrishna/ERAv2_GenerateCaptions/resolve/main/utils.py
- Command line
-
hf download hf://spaces/Vasudevakrishna/ERAv2_GenerateCaptions/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Vasudevakrishna/ERAv2_GenerateCaptions/resolve/main/utils.py
461 Bytes
| import open_clip | |
| import torch | |
| from PIL import Image | |
| model, _, transform = open_clip.create_model_and_transforms( | |
| model_name="coca_ViT-L-14", | |
| pretrained="mscoco_finetuned_laion2B-s13B-b90k" | |
| ) | |
| def get_captions(image): | |
| im = transform(image).unsqueeze(0) | |
| with torch.no_grad(), torch.cuda.amp.autocast(): | |
| generated = model.generate(im) | |
| return open_clip.decode(generated[0]).split("<end_of_text>")[0].replace("<start_of_text>", "") |