Spaces:
Runtime error
Runtime error
Commit
·
82f2103
1
Parent(s):
3515e2f
Create app.py
Browse filesCreated app.py file
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, Blip2ForConditionalGeneration, VisionEncoderDecoderModel
|
| 3 |
+
import torch
|
| 4 |
+
import open_clip
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
|
| 7 |
+
torch.hub.download_url_to_file('https://huggingface.co/datasets/nielsr/textcaps-sample/resolve/main/stop_sign.png', 'stop_sign.png')
|
| 8 |
+
torch.hub.download_url_to_file('https://cdn.openai.com/dall-e-2/demos/text2im/astronaut/horse/photo/0.jpg', 'astronaut.jpg')
|
| 9 |
+
blip2_processor_8_bit = AutoProcessor.from_pretrained("Salesforce/blip2-opt-6.7b")
|
| 10 |
+
blip2_model_8_bit = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-6.7b", device_map="auto", load_in_8bit=True)
|
| 11 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False):
|
| 13 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 14 |
+
|
| 15 |
+
if use_float_16:
|
| 16 |
+
inputs = inputs.to(torch.float16)
|
| 17 |
+
|
| 18 |
+
generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50)
|
| 19 |
+
|
| 20 |
+
if tokenizer is not None:
|
| 21 |
+
generated_caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 22 |
+
else:
|
| 23 |
+
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 24 |
+
|
| 25 |
+
return generated_caption
|
| 26 |
+
|
| 27 |
+
def generate_captions(image):
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
caption_blip2_8_bit = generate_caption(blip2_processor_8_bit, blip2_model_8_bit, image, use_float_16=True).strip()
|
| 31 |
+
|
| 32 |
+
return caption_blip2_8_bit
|
| 33 |
+
|
| 34 |
+
examples = [["cats.jpg"], ["stop_sign.png"], ["astronaut.jpg"]]
|
| 35 |
+
outputs = [gr.outputs.Textbox(label="Caption generated by BLIP-2 OPT 6.7b")]
|
| 36 |
+
|
| 37 |
+
title = "Interactive demo: Image captioning BLIP2 model"
|
| 38 |
+
description = "Gradio Demo of BLIP-2, 4 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."
|
| 39 |
+
article = "<p style='text-align: center'><a href='https://huggingface.co/docs/transformers/main/model_doc/blip' target='_blank'>BLIP docs</a> | <a href='https://huggingface.co/docs/transformers/main/model_doc/git' target='_blank'>GIT docs</a></p>"
|
| 40 |
+
|
| 41 |
+
interface = gr.Interface(fn=generate_captions,
|
| 42 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 43 |
+
outputs=outputs,
|
| 44 |
+
examples=examples,
|
| 45 |
+
title=title,
|
| 46 |
+
description=description,
|
| 47 |
+
article=article,
|
| 48 |
+
enable_queue=True)
|
| 49 |
+
interface.launch(debug=True)
|