Spaces:
Sleeping
Sleeping
import transformers | |
import torch | |
import gradio as gr | |
import requests | |
from transformers import BlipForConditionalGeneration | |
from transformers import AutoProcessor | |
from transformers.utils import logging | |
from PIL import Image | |
model = BlipForConditionalGeneration.from_pretrained( | |
"Salesforce/blip-image-captioning-base") | |
processor = AutoProcessor.from_pretrained( | |
"Salesforce/blip-image-captioning-base") | |
def process_image(input_type, image_url, image_upload): | |
if input_type == "URL": | |
raw_image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB') | |
else: | |
raw_image = image_upload | |
inputs = processor(raw_image,return_tensors="pt") | |
out = model.generate(**inputs)[0] | |
description = processor.decode(out, skip_special_tokens=True).capitalize() | |
formatted_description = ( | |
f"""<div><h1 style='text-align: center; font-size: 40px; color: orange;'> | |
{description} | |
</h1></div>""" | |
) | |
return formatted_description | |
def display_image_from_url(image_url): | |
if image_url: | |
image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB') | |
return image | |
return None | |
def toggle_inputs(input_type): | |
if input_type == "URL": | |
return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False) | |
else: | |
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True) | |
with gr.Blocks() as demo: | |
gr.Markdown( | |
""" | |
# Image Captioning - test & demo app by Srinivas.V.. | |
Paste either URL of an image or upload the image and submit. | |
""") | |
input_type = gr.Radio(choices=["URL", "Upload"], label="Input Type") | |
image_url = gr.Textbox(label="Image URL", visible=False) | |
url_image = gr.Image(type="pil", label="URL Image", visible=False) | |
image_upload = gr.Image(type="pil", label="Upload Image", visible=False) | |
input_type.change(fn=toggle_inputs, inputs=input_type, outputs=[image_url, url_image, image_upload]) | |
image_url.change(fn=display_image_from_url, inputs=image_url, outputs=url_image) | |
submit_btn = gr.Button("Submit") | |
processed_image = gr.HTML(label="Caption for the Image") | |
submit_btn.click(fn=process_image, inputs=[input_type, image_url, image_upload], outputs=processed_image) | |
demo.launch(debug=True, share=True) |