File size: 3,430 Bytes
d327984
 
 
 
 
 
763746b
d327984
 
 
763746b
d327984
 
 
763746b
d327984
 
763746b
d327984
 
 
 
 
 
 
 
763746b
d327984
 
4e59cd9
763746b
d327984
 
763746b
d327984
4e59cd9
d327984
 
4e59cd9
 
 
 
 
 
 
 
 
 
d327984
763746b
d327984
 
 
 
 
 
 
 
 
 
 
 
6f35da4
d327984
 
 
 
 
 
 
 
 
 
 
 
6f35da4
d327984
 
 
 
 
 
 
 
 
 
6f35da4
5083802
d327984
 
 
 
 
6f35da4
d327984
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from transformers import BlipProcessor, BlipForConditionalGeneration
from PIL import Image
import gradio as gr
import tempfile
import os
import csv

# Initialize the processor and model
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

def generate_image_caption(image):
    """
    Generate a caption for the given image.

    Args:
        image (PIL.Image): The image object.

    Returns:
        str: The generated caption.
    """
    image = image.convert("RGB")
    inputs = processor(images=image, return_tensors="pt")
    outputs = model.generate(**inputs)
    caption = processor.decode(outputs[0], skip_special_tokens=True)
    return caption

def generate_keywords(caption):
    """
    Generate a list of keywords from the caption, focusing on SEO relevance.

    Args:
        caption (str): The image caption.

    Returns:
        list: A list of 50 single-word keywords.
    """
    words = caption.split()
    # Remove common stopwords and keep unique words for SEO relevance
    stopwords = {"a", "the", "and", "of", "in", "on", "with", "at", "for", "to", "is"}
    keywords = list(set(word.lower() for word in words if word.lower() not in stopwords))
    
    # Ensure the list contains exactly 50 keywords (add repetitions if needed)
    if len(keywords) > 50:
        keywords = keywords[:50]
    elif len(keywords) < 50:
        keywords.extend(keywords[:50-len(keywords)])
    
    return keywords

def process_images(image_files):
    """
    Process uploaded images to generate metadata and create a CSV file.

    Args:
        image_files (list of file-like objects): List of uploaded image files.

    Returns:
        tuple: A list of PIL images, path to the CSV file.
    """
    metadata = []
    temp_dir = tempfile.mkdtemp()

    for image_file in image_files:
        filename = os.path.basename(image_file.name)
        image = Image.open(image_file)
        caption = generate_image_caption(image)
        if caption:
            keywords = generate_keywords(caption)
            title = caption if 70 <= len(caption) <= 100 else caption[:100]
            metadata.append({
                'filename': filename,
                'title': title,
                'keywords': keywords
            })

    # Create CSV file
    csv_file_path = os.path.join(temp_dir, 'images_metadata.csv')
    with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file:
        writer = csv.writer(file)
        writer.writerow(['Filename', 'Title', 'Keywords'])
        for data in metadata:
            filename = data['filename']
            title = data['title']
            keywords = ','.join(data['keywords'])
            writer.writerow([filename, title, keywords])

    return [Image.open(image_file.name) for image_file in image_files], csv_file_path

# Define Gradio interface
iface = gr.Interface(
    fn=process_images,
    inputs=[
        gr.Files(label="Upload Image Files")  # Use gr.Files for multiple file uploads
    ],
    outputs=[
        gr.Gallery(label="Processed Images"),
        gr.File(label="Download Metadata CSV")
    ],
    title="Image Captioning and Metadata Generator",
    description="Upload multiple images to generate captions and metadata. Download the metadata as a CSV file."
)

# Launch the interface
iface.launch(debug=True)