garychew commited on
Commit
b860d7b
·
verified ·
1 Parent(s): ff9a698

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -1,4 +1,3 @@
1
- import gradio as gr
2
  import requests
3
  from PIL import Image
4
  from transformers import AutoProcessor, BlipForConditionalGeneration
@@ -7,9 +6,19 @@ from transformers import AutoProcessor, BlipForConditionalGeneration
7
  processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
8
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
9
 
 
 
 
 
10
 
11
- def greet(name):
12
- return "Hello " + name + "!!"
 
13
 
14
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
15
- demo.launch()
 
 
 
 
 
 
 
1
  import requests
2
  from PIL import Image
3
  from transformers import AutoProcessor, BlipForConditionalGeneration
 
6
  processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
7
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
8
 
9
+ # Load your image, DON'T FORGET TO WRITE YOUR IMAGE NAME
10
+ img_path = "YOUR IMAGE NAME.jpeg"
11
+ # convert it into an RGB format
12
+ image = Image.open(img_path).convert('RGB')
13
 
14
+ # You do not need a question for image captioning
15
+ text = "the image of"
16
+ inputs = processor(images=image, text=text, return_tensors="pt")
17
 
18
+ # Generate a caption for the image
19
+ outputs = model.generate(**inputs, max_length=50)
20
+
21
+ # Decode the generated tokens to text
22
+ caption = processor.decode(outputs[0], skip_special_tokens=True)
23
+ # Print the caption
24
+ print(caption)