Spaces:
Sleeping
Sleeping
File size: 2,330 Bytes
112b8aa 2265414 112b8aa |
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 |
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) |