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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -45
app.py CHANGED
@@ -1,53 +1,20 @@
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, 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
- title = "Generator Identification via Image Captioning"
44
- 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."
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()