Spaces:
Sleeping
Sleeping
File size: 1,841 Bytes
ae789f7 6d66457 ae789f7 cef44ac ae789f7 cef44ac ae789f7 6d66457 ae789f7 cef44ac 6d66457 cef44ac ae789f7 6d66457 cef44ac 6d66457 ae789f7 ade1511 6d66457 |
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import base64
import gradio as gr
import io
from PIL import Image
import spaces
from transformers import pipeline
def image_to_base64_str(pil_image: Image.Image) -> str:
"""
Converts a PIL image to a base64 encoded string.
Args:
pil_image (Image.Image): The PIL image to be converted.
Returns:
str: The base64 encoded string representation of the image.
"""
byte_arr = io.BytesIO()
pil_image.save(byte_arr, format='PNG')
byte_arr = byte_arr.getvalue()
return str(base64.b64encode(byte_arr).decode('utf-8'))
# Initialize Model
get_completion = pipeline("image-to-text",model="Salesforce/blip-image-captioning-base", device=0)
@spaces.GPU(duration=120)
def captioner(input: Image.Image) -> str:
"""
Generate a caption for the given image using the BLIP-IMAGE-CAPTIONING-BASE model.
Args:
input (Image.Image): The input image for which to generate a caption.
Returns:
str: The generated caption text.
"""
base64_image = image_to_base64_str(image)
output = get_completion(base64_image)
return output[0]['generated_text']
####### GRADIO APP #######
title = """<h1 id="title"> Image Captioning with BLIP model </h1>"""
description = """
- The model used for Generating Captions [BLIP-IMAGE-CAPTIONING-BASE](https://huggingface.co./Salesforce/blip-image-captioning-base).
"""
css = '''
h1#title {
text-align: center;
}
'''
theme = gr.themes.Soft()
demo = gr.Blocks(css=css, theme=theme)
with demo:
gr.Markdown(title)
gr.Markdown(description)
interface = gr.Interface(fn=captioner,
inputs=[gr.Image(label="Upload image", type="pil")],
outputs=[gr.Textbox(label="Caption")],
allow_flagging="never",
examples= "example_images")
demo.launch() |