Spaces:
Runtime error
Runtime error
Tevfik istanbullu commited on
Update Image Caption Generator.py
Browse files- Image Caption Generator.py +56 -56
Image Caption Generator.py
CHANGED
|
@@ -1,56 +1,56 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import numpy as np
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from transformers import AutoProcessor, BlipForConditionalGeneration
|
| 5 |
-
import os
|
| 6 |
-
# Load the pretrained processor and model
|
| 7 |
-
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 8 |
-
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 9 |
-
|
| 10 |
-
def caption_image(input_image: np.ndarray):
|
| 11 |
-
# Convert numpy array to PIL Image and convert to RGB
|
| 12 |
-
raw_image = Image.fromarray(input_image).convert('RGB')
|
| 13 |
-
|
| 14 |
-
# Process the image
|
| 15 |
-
inputs = processor(raw_image, return_tensors="pt")
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Generate a caption for the image
|
| 19 |
-
out = model.generate(**inputs,max_length=50)
|
| 20 |
-
|
| 21 |
-
# Decode the generated tokens to text
|
| 22 |
-
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 23 |
-
|
| 24 |
-
return caption
|
| 25 |
-
|
| 26 |
-
# Save the data to the Hugging Face dataset
|
| 27 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 28 |
-
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-images-data")
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# Define examples
|
| 32 |
-
examples = [
|
| 33 |
-
["1.jpg"],
|
| 34 |
-
["2.jpg"],
|
| 35 |
-
["3.jpg"],
|
| 36 |
-
["4.jpg"],
|
| 37 |
-
]
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Create a Gradio interface
|
| 42 |
-
|
| 43 |
-
iface = gr.Interface(
|
| 44 |
-
fn=caption_image,
|
| 45 |
-
inputs=gr.Image(),
|
| 46 |
-
outputs=gr.Textbox(label="Generated Caption", lines=2),
|
| 47 |
-
title="π Image Caption Generator πΌοΈ",
|
| 48 |
-
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.",
|
| 49 |
-
allow_flagging="auto",
|
| 50 |
-
flagging_callback=hf_writer,
|
| 51 |
-
examples=examples,
|
| 52 |
-
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
iface.launch()
|
| 56 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from transformers import AutoProcessor, BlipForConditionalGeneration
|
| 5 |
+
import os
|
| 6 |
+
# Load the pretrained processor and model
|
| 7 |
+
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 8 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 9 |
+
|
| 10 |
+
def caption_image(input_image: np.ndarray):
|
| 11 |
+
# Convert numpy array to PIL Image and convert to RGB
|
| 12 |
+
raw_image = Image.fromarray(input_image).convert('RGB')
|
| 13 |
+
|
| 14 |
+
# Process the image
|
| 15 |
+
inputs = processor(raw_image, return_tensors="pt")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Generate a caption for the image
|
| 19 |
+
out = model.generate(**inputs,max_length=50)
|
| 20 |
+
|
| 21 |
+
# Decode the generated tokens to text
|
| 22 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 23 |
+
|
| 24 |
+
return caption
|
| 25 |
+
|
| 26 |
+
# Save the data to the Hugging Face dataset
|
| 27 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 28 |
+
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-images-data")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
# Define examples
|
| 32 |
+
examples = [
|
| 33 |
+
["1.jpg"],
|
| 34 |
+
["2.jpg"],
|
| 35 |
+
["3.jpg"],
|
| 36 |
+
["4.jpg"],
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Create a Gradio interface
|
| 42 |
+
|
| 43 |
+
iface = gr.Interface(
|
| 44 |
+
fn=caption_image,
|
| 45 |
+
inputs=gr.Image(),
|
| 46 |
+
outputs=gr.Textbox(label="Generated Caption", lines=2),
|
| 47 |
+
title="π Image Caption Generator πΌοΈ ",
|
| 48 |
+
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.",
|
| 49 |
+
allow_flagging="auto",
|
| 50 |
+
flagging_callback=hf_writer,
|
| 51 |
+
examples=examples,
|
| 52 |
+
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
iface.launch()
|
| 56 |
+
|