Spaces:
Running
Running
Upload 2 files
Browse files- app.py +69 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, Blip2ForConditionalGeneration, VisionEncoderDecoderModel
|
| 3 |
+
import torch
|
| 4 |
+
import open_clip
|
| 5 |
+
|
| 6 |
+
from huggingface_hub import hf_hub_download
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
git_processor_large_coco = AutoProcessor.from_pretrained("microsoft/git-large-coco")
|
| 10 |
+
git_model_large_coco = AutoModelForCausalLM.from_pretrained("microsoft/git-large-coco")
|
| 11 |
+
|
| 12 |
+
git_processor_large_textcaps = AutoProcessor.from_pretrained("microsoft/git-large-r-textcaps")
|
| 13 |
+
git_model_large_textcaps = AutoModelForCausalLM.from_pretrained("microsoft/git-large-r-textcaps")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
|
| 19 |
+
git_model_large_coco.to(device)
|
| 20 |
+
git_model_large_textcaps.to(device)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False):
|
| 24 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 25 |
+
|
| 26 |
+
if use_float_16:
|
| 27 |
+
inputs = inputs.to(torch.float16)
|
| 28 |
+
|
| 29 |
+
generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50)
|
| 30 |
+
|
| 31 |
+
if tokenizer is not None:
|
| 32 |
+
generated_caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 33 |
+
else:
|
| 34 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 35 |
+
|
| 36 |
+
return generated_caption
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def generate_caption_coca(model, transform, image):
|
| 40 |
+
im = transform(image).unsqueeze(0).to(device)
|
| 41 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
| 42 |
+
generated = model.generate(im, seq_len=20)
|
| 43 |
+
return open_clip.decode(generated[0].detach()).split("<end_of_text>")[0].replace("<start_of_text>", "")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def generate_captions(image):
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
caption_git_large_coco = generate_caption(git_processor_large_coco, git_model_large_coco, image)
|
| 50 |
+
|
| 51 |
+
caption_git_large_textcaps = generate_caption(git_processor_large_textcaps, git_model_large_textcaps, image)
|
| 52 |
+
|
| 53 |
+
return caption_git_large_coco, caption_git_large_textcaps
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
outputs = [gr.outputs.Textbox(label="Caption generated by GIT-large fine-tuned on COCO"), gr.outputs.Textbox(label="Caption generated by GIT-large fine-tuned on TextCaps")]
|
| 57 |
+
|
| 58 |
+
title = "Interactive demo: comparing image captioning models"
|
| 59 |
+
description = "Gradio Demo to compare GIT state-of-the-art vision+language models. To use it, simply upload your image and click 'submit', or click one of the examples to load them. Read more at the links below."
|
| 60 |
+
article = ""
|
| 61 |
+
|
| 62 |
+
interface = gr.Interface(fn=generate_captions,
|
| 63 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 64 |
+
outputs=outputs,
|
| 65 |
+
title=title,
|
| 66 |
+
description=description,
|
| 67 |
+
article=article,
|
| 68 |
+
enable_queue=True)
|
| 69 |
+
interface.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/transformers.git@main
|
| 2 |
+
torch
|
| 3 |
+
open_clip_torch
|
| 4 |
+
accelerate
|
| 5 |
+
bitsandbytes
|