umm-maybe commited on
Commit
b8436d4
1 Parent(s): 43cae52

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -12
app.py CHANGED
@@ -1,20 +1,47 @@
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()
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoProcessor, BlipForConditionalGeneration
3
 
4
+ # from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, Blip2ForConditionalGeneration, VisionEncoderDecoderModel
5
+ import torch
 
6
 
7
+ blip_processor_large = AutoProcessor.from_pretrained("umm-maybe/image-generator-identifier")
8
+ blip_model_large = BlipForConditionalGeneration.from_pretrained("umm-maybe/image-generator-identifier")
9
 
10
+ device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
11
 
12
+ blip_model_large.to(device)
13
+
14
+ def generate_caption(processor, model, image):
15
+ inputs = processor(images=image, return_tensors="pt").to(device)
16
+
17
+ generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=50)
18
+
19
+ generated_caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
20
+
21
+ return generated_caption
22
+
23
+
24
+ def generate_captions(image):
25
+
26
+ caption_blip_large = generate_caption(blip_processor_large, blip_model_large, image)
27
+
28
+ return caption_blip_large
29
+
30
+
31
+
32
+ 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"]]
33
  outputs = [
34
+ gr.outputs.Textbox(label="Caption including detected generator (if applicable)"),
35
  ]
36
 
37
+ title = "Generator Identification via Image Captioning"
38
+ 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."
39
+
40
+ interface = gr.Interface(fn=generate_captions,
41
+ inputs=gr.inputs.Image(type="pil"),
42
+ outputs="textbox",
43
+ examples=examples,
44
+ title=title,
45
+ description=description,
46
+ enable_queue=True)
47
+ interface.launch()