| import pathlib | |
| import gradio as gr | |
| import open_clip | |
| import torch | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| model, _, transform = open_clip.create_model_and_transforms( | |
| "coca_ViT-L-14", | |
| pretrained="mscoco_finetuned_laion2B-s13B-b90k" | |
| ) | |
| model.to(device) | |
| def output_generate(image): | |
| im = transform(image).unsqueeze(0).to(device) | |
| with torch.no_grad(), torch.cuda.amp.autocast(): | |
| generated = model.generate(im, seq_len=20) | |
| return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "") | |
| paths = sorted(pathlib.Path("images").glob("*.jpg")) | |
| iface = gr.Interface( | |
| fn=output_generate, | |
| inputs=gr.Image(label="Input image", type="pil"), | |
| outputs=gr.Text(label="Caption output"), | |
| title="CoCa: Contrastive Captioners", | |
| description=( | |
| """<br> An open source implementation of <strong>CoCa: Contrastive Captioners are Image-Text Foundation Models</strong> <a href=https://arxiv.org/abs/2205.01917>https://arxiv.org/abs/2205.01917.</a> | |
| <br> Built using <a href=https://github.com/mlfoundations/open_clip>open_clip</a> with an effort from <a href=https://laion.ai/>LAION</a>. | |
| <br> For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.<a href="https://huggingface.co/spaces/laion/CoCa?duplicate=true"> <img style="margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>""" | |
| ), | |
| article="""""", | |
| examples=[path.as_posix() for path in paths], | |
| ) | |
| iface.launch() |