Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,7 @@
|
|
| 1 |
-
|
| 2 |
-
import numpy as np
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from transformers import AutoProcessor, BlipForConditionalGeneration
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 11 |
-
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
-
#processor = # write your code here
|
| 13 |
-
#model = # write your code here
|
| 14 |
-
|
| 15 |
-
def caption_image(input_image: np.ndarray):
|
| 16 |
-
# Convert numpy array to PIL Image and convert to RGB
|
| 17 |
-
raw_image = Image.fromarray(input_image).convert('RGB')
|
| 18 |
-
|
| 19 |
-
# Process the image
|
| 20 |
-
# You do not need a question for image captioning
|
| 21 |
-
text = "the image of"
|
| 22 |
-
inputs = processor(images=image, text=text, return_tensors="pt")
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Generate a caption for the image
|
| 26 |
-
# Generate a caption for the image
|
| 27 |
-
outputs = model.generate(**inputs, max_length=50)
|
| 28 |
-
|
| 29 |
-
# Decode the generated tokens to text and store it into `caption`
|
| 30 |
-
# Decode the generated tokens to text
|
| 31 |
-
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 32 |
-
# Print the caption
|
| 33 |
-
#print(caption)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return caption
|
| 37 |
-
|
| 38 |
-
iface = gr.Interface(
|
| 39 |
-
fn=caption_image,
|
| 40 |
-
inputs=gr.Image(),
|
| 41 |
-
outputs="text",
|
| 42 |
-
title="Image Captioning",
|
| 43 |
-
description="This is a simple web app for generating captions for images using a trained model."
|
| 44 |
)
|
| 45 |
-
|
|
|
|
| 1 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
#pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 4 |
+
pipe = pipeline(model="Salesforce/blip-image-captioning-base")
|
| 5 |
+
preds = pipe(
|
| 6 |
+
images="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/pipeline-cat-chonk.jpeg"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
)
|
|
|