Spaces:
Runtime error
Runtime error
File size: 1,625 Bytes
7b4c32c |
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 |
import gradio as gr
import numpy as np
from PIL import Image
from transformers import AutoProcessor, BlipForConditionalGeneration
import os
# Load the pretrained processor and model
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
def caption_image(input_image: np.ndarray):
# Convert numpy array to PIL Image and convert to RGB
raw_image = Image.fromarray(input_image).convert('RGB')
# Process the image
inputs = processor(raw_image, return_tensors="pt")
# Generate a caption for the image
out = model.generate(**inputs,max_length=50)
# Decode the generated tokens to text
caption = processor.decode(out[0], skip_special_tokens=True)
return caption
# Save the data to the Hugging Face dataset
HF_TOKEN = os.getenv("HF_TOKEN")
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-images-data")
# Define examples
examples = [
["1.jpg"],
["2.jpg"],
["3.jpg"],
["4.jpg"],
]
# Create a Gradio interface
iface = gr.Interface(
fn=caption_image,
inputs=gr.Image(),
outputs=gr.Textbox(label="Generated Caption", lines=2),
title="π Image Caption Generator πΌοΈ ",
description = "Generate stunning captions for your images with our AI-powered model! π\n\nπ«π Note: Please avoid entering any sensitive or personal information, as inputs may be reviewed or used for training purposes.",
allow_flagging="auto",
flagging_callback=hf_writer,
examples=examples,
)
iface.launch()
|