Spaces:
Runtime error
Runtime error
import gradio as gr | |
from transformers import AutoProcessor, BlipForConditionalGeneration | |
# from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, Blip2ForConditionalGeneration, VisionEncoderDecoderModel | |
import torch | |
blip_processor_large = AutoProcessor.from_pretrained("umm-maybe/image-generator-identifier") | |
blip_model_large = BlipForConditionalGeneration.from_pretrained("umm-maybe/image-generator-identifier") | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
blip_model_large.to(device) | |
def generate_caption(processor, model, image, tokenizer=None, use_float_16=False): | |
inputs = processor(images=image, return_tensors="pt").to(device) | |
if use_float_16: | |
inputs = inputs.to(torch.float16) | |
generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50) | |
if tokenizer is not None: | |
generated_caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
else: | |
generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0] | |
return generated_caption | |
def generate_captions(image): | |
caption_blip_large = generate_caption(blip_processor_large, blip_model_large, image) | |
return caption_blip_large | |
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"]] | |
outputs = [ | |
gr.outputs.Textbox(label="Caption including detected generator (if applicable)"), | |
] | |
title = "Generator Identification via Image Captioning" | |
description = "Gradio Demo to illustrate the use of a fine-tuned BLIP image captioning to identify synthetic images. To use it, simply upload your image and click 'submit', or click one of the examples to load them." | |
interface = gr.Interface(fn=generate_captions, | |
inputs=gr.inputs.Image(type="pil"), | |
outputs=outputs, | |
examples=examples, | |
title=title, | |
description=description, | |
enable_queue=True) | |
interface.launch() |