Spaces:
Runtime error
Runtime error
import requests | |
from PIL import Image | |
from transformers import BlipProcessor, BlipForConditionalGeneration | |
import gradio as gr | |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base") | |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base") | |
def generate_caption(image, caption_type, text): | |
raw_image = Image.fromarray(image.astype('uint8'), 'RGB') | |
if caption_type == "Conditional": | |
caption = conditional_image_captioning(raw_image, text) | |
else: | |
caption = unconditional_image_captioning(raw_image) | |
return caption | |
def conditional_image_captioning(raw_image, text): | |
inputs = processor(raw_image, text, return_tensors="pt") | |
out = model.generate(**inputs) | |
caption = processor.decode(out[0], skip_special_tokens=True) | |
return caption | |
def unconditional_image_captioning(raw_image): | |
inputs = processor(raw_image, return_tensors="pt") | |
out = model.generate(**inputs) | |
caption = processor.decode(out[0], skip_special_tokens=True) | |
return caption | |
input_image = gr.inputs.Image(label="Upload an Image") | |
input_text = gr.inputs.Textbox(label="Enter Text (for Conditional Captioning)") | |
radio_button = gr.inputs.Radio(choices=["Conditional", "Unconditional"], label="Captioning Type") | |
output_text = gr.outputs.Textbox(label="Caption") | |
#examples = [[f"Image{i}.png" ] + ["Unconditional", ""] for i in range(1, 4)] | |
examples = [["Image1.png","Conditional", "Goal"], | |
["Image2.png","Unconditional", ""], | |
["Image3.png","Conditional", "Watch"]] | |
gr.Interface(fn=generate_caption, inputs=[input_image, radio_button, input_text], outputs=output_text, examples = examples, title="Image Captioning", description = "Model was taken from https://huggingface.co/Salesforce/blip-image-captioning-base.You can provide words as conditions to give a direction to your caption.You can refer to the examples below.").launch() | |