Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,104 @@
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
return result[0]['generated_text']
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return ",".join(list(words)[:50])
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 2 |
+
from PIL import Image
|
| 3 |
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import tempfile
|
| 7 |
+
import os
|
| 8 |
+
import csv
|
| 9 |
|
| 10 |
+
# Initialize the processor and model
|
| 11 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 12 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
|
|
|
| 13 |
|
| 14 |
+
def generate_image_caption(image):
|
| 15 |
+
"""
|
| 16 |
+
Generate a caption for the given image.
|
| 17 |
|
| 18 |
+
Args:
|
| 19 |
+
image (PIL.Image): The image object.
|
| 20 |
|
| 21 |
+
Returns:
|
| 22 |
+
str: The generated caption.
|
| 23 |
+
"""
|
| 24 |
+
# Convert image to RGB format
|
| 25 |
+
image = image.convert("RGB")
|
| 26 |
+
|
| 27 |
+
# Preprocess the image and generate a caption
|
| 28 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 29 |
+
outputs = model.generate(**inputs)
|
| 30 |
+
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 31 |
+
|
| 32 |
+
return caption
|
| 33 |
|
| 34 |
+
def generate_keywords(caption):
|
| 35 |
+
"""
|
| 36 |
+
Generate a list of keywords from the caption.
|
|
|
|
| 37 |
|
| 38 |
+
Args:
|
| 39 |
+
caption (str): The image caption.
|
| 40 |
|
| 41 |
+
Returns:
|
| 42 |
+
list: A list of single-word keywords.
|
| 43 |
+
"""
|
| 44 |
+
# Example simple keyword extraction (use a more sophisticated method if needed)
|
| 45 |
+
words = caption.split()
|
| 46 |
+
keywords = list(set(words))[:50] # Take unique words and limit to 50
|
| 47 |
+
return keywords
|
| 48 |
|
| 49 |
+
def process_images(image_files):
|
| 50 |
+
"""
|
| 51 |
+
Process uploaded images to generate metadata and create a CSV file.
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
image_files (list of file-like objects): List of uploaded image files.
|
| 55 |
+
|
| 56 |
+
Returns:
|
| 57 |
+
tuple: A list of PIL images, path to the CSV file.
|
| 58 |
+
"""
|
| 59 |
+
metadata = []
|
| 60 |
+
temp_dir = tempfile.mkdtemp()
|
| 61 |
+
|
| 62 |
+
for image_file in image_files:
|
| 63 |
+
filename = os.path.basename(image_file.name)
|
| 64 |
+
image = Image.open(image_file)
|
| 65 |
+
caption = generate_image_caption(image)
|
| 66 |
+
if caption:
|
| 67 |
+
keywords = generate_keywords(caption)
|
| 68 |
+
# Ensure the title is within the 70 to 100 characters range
|
| 69 |
+
title = caption if 70 <= len(caption) <= 100 else caption[:100]
|
| 70 |
+
metadata.append({
|
| 71 |
+
'filename': filename,
|
| 72 |
+
'title': title,
|
| 73 |
+
'keywords': keywords
|
| 74 |
+
})
|
| 75 |
+
|
| 76 |
+
# Create CSV file
|
| 77 |
+
csv_file_path = os.path.join(temp_dir, 'images_metadata.csv')
|
| 78 |
+
with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file:
|
| 79 |
+
writer = csv.writer(file)
|
| 80 |
+
writer.writerow(['Filename', 'Title', 'Keywords'])
|
| 81 |
+
for data in metadata:
|
| 82 |
+
filename = data['filename']
|
| 83 |
+
title = data['title']
|
| 84 |
+
keywords = ','.join(data['keywords'])
|
| 85 |
+
writer.writerow([filename, title, keywords])
|
| 86 |
+
|
| 87 |
+
return [Image.open(img_file.name) for img_file in image_files], csv_file_path
|
| 88 |
+
|
| 89 |
+
# Define Gradio interface
|
| 90 |
+
iface = gr.Interface(
|
| 91 |
+
fn=process_images,
|
| 92 |
+
inputs=[
|
| 93 |
+
gr.Files(label="Upload Image Files", type="file", multiple=True)
|
| 94 |
+
],
|
| 95 |
+
outputs=[
|
| 96 |
+
gr.Gallery(label="Processed Images"),
|
| 97 |
+
gr.File(label="Download Metadata CSV")
|
| 98 |
+
],
|
| 99 |
+
title="Image Captioning and Metadata Generator",
|
| 100 |
+
description="Upload multiple images to generate captions and metadata. Download the metadata as a CSV file."
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Launch the interface
|
| 104 |
+
iface.launch(debug=True)
|