Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoProcessor, BlipForConditionalGeneration
|
| 3 |
|
| 4 |
-
|
| 5 |
-
import
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
blip_model_large.to(device)
|
| 13 |
-
|
| 14 |
-
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False):
|
| 15 |
-
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 16 |
-
|
| 17 |
-
if use_float_16:
|
| 18 |
-
inputs = inputs.to(torch.float16)
|
| 19 |
-
|
| 20 |
-
generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50)
|
| 21 |
-
|
| 22 |
-
if tokenizer is not None:
|
| 23 |
-
generated_caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 24 |
-
else:
|
| 25 |
-
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 26 |
-
|
| 27 |
-
return generated_caption
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def generate_captions(image):
|
| 31 |
-
|
| 32 |
-
caption_blip_large = generate_caption(blip_processor_large, blip_model_large, image)
|
| 33 |
-
|
| 34 |
-
return caption_blip_large
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
examples = [["australia.jpg"], ["biden.png"], ["elon.jpg"], ["horns.jpg"], ["man.jpg"], ["nun.jpg"], ["painting.jpg"], ["pentagon.jpg"], ["pollock.jpg"], ["radcliffe.jpg"], ["split.jpg"], ["waves.jpg"], ["yeti.jpg"]]
|
| 39 |
outputs = [
|
| 40 |
-
gr.outputs.Textbox(label="Caption including detected generator (if applicable)"),
|
| 41 |
]
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
interface = gr.Interface(fn=generate_captions,
|
| 47 |
-
inputs=gr.inputs.Image(type="pil"),
|
| 48 |
-
outputs=outputs,
|
| 49 |
-
examples=examples,
|
| 50 |
-
title=title,
|
| 51 |
-
description=description,
|
| 52 |
-
enable_queue=True)
|
| 53 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 6 |
|
| 7 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 8 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 9 |
|
| 10 |
+
def caption_image(raw_image):
|
| 11 |
+
inputs = processor(raw_image, return_tensors="pt")
|
| 12 |
+
out = model.generate(**inputs)
|
| 13 |
+
return processor.decode(out[0], skip_special_tokens=True)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
outputs = [
|
| 16 |
+
gr.outputs.Textbox(label="Caption, including detected generator (if applicable)"),
|
| 17 |
]
|
| 18 |
|
| 19 |
+
demo = gr.Interface(fn=caption_image, inputs="image", outputs=outputs)
|
| 20 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|