Nepjune commited on
Commit
b56d4a5
·
verified ·
1 Parent(s): a9e8d0a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -26
app.py CHANGED
@@ -1,29 +1,19 @@
1
- from transformers import pipeline
2
- image_captioner = pipeline("image-to-text",model="Salesforce/blip-image-captioning-large")
3
- #Set up Prerequisites for Image Captioning App User Interface
4
- import os
5
- import io
6
- import IPython.display
7
  from PIL import Image
8
- import base64
9
-
10
  import gradio as gr
11
 
12
- def image_to_base64_str(pil_image):
13
- byte_arr = io.BytesIO()
14
- pil_image.save(byte_arr, format='PNG')
15
- byte_arr = byte_arr.getvalue()
16
- return str(base64.b64encode(byte_arr).decode('utf-8'))
17
- def captioner(image):
18
- base64_image = image_to_base64_str(image)
19
- result = image_captioner(base64_image)
20
- return result[0]['generated_text']
21
- gr.close_all()
22
- ImageCaptionApp = gr.Interface(fn=captioner,
23
- inputs=[gr.Image(label="Upload image", type="pil")],
24
- outputs=[gr.Textbox(label="Caption")],
25
- title="Image Captioning with BLIP",
26
- description="Caption any image using the BLIP model",
27
- allow_flagging="never")
28
-
29
- ImageCaptionApp.launch()
 
 
 
 
 
 
 
1
  from PIL import Image
2
+ import requests
 
3
  import gradio as gr
4
 
5
+ from transformers import BlipProcessor, BlipForConditionalGeneration
6
+
7
+ model_id = "Salesforce/blip-image-captioning-base"
8
+
9
+ model = BlipForConditionalGeneration.from_pretrained(model_id)
10
+ processor = BlipProcessor.from_pretrained(model_id)
11
+
12
+ def launch(input):
13
+ image = Image.open(requests.get(input, stream=True).raw).convert('RGB')
14
+ inputs = processor(image, return_tensors="pt")
15
+ out = model.generate(**inputs)
16
+ return processor.decode(out[0], skip_special_tokens=True)
17
+
18
+ iface = gr.Interface(launch, inputs="text", outputs="text")
19
+ iface.launch()