File size: 1,840 Bytes
2693b38
b8436d4
2693b38
b8436d4
 
caab17b
b8436d4
 
caab17b
b8436d4
caab17b
b8436d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c0a3b4
b8436d4
 
 
 
6fc7565
b8436d4
 
 
d5f5ad4
b8436d4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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):
    inputs = processor(images=image, return_tensors="pt").to(device)
    
    generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50)

    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"]]

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="image",
                         outputs="textbox",
                         examples=examples, 
                         title=title,
                         description=description)
interface.launch()