Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,14 +19,10 @@ def generate_image_caption(image):
|
|
| 19 |
Returns:
|
| 20 |
str: The generated caption.
|
| 21 |
"""
|
| 22 |
-
# Convert image to RGB format
|
| 23 |
image = image.convert("RGB")
|
| 24 |
-
|
| 25 |
-
# Preprocess the image and generate a caption
|
| 26 |
inputs = processor(images=image, return_tensors="pt")
|
| 27 |
outputs = model.generate(**inputs)
|
| 28 |
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 29 |
-
|
| 30 |
return caption
|
| 31 |
|
| 32 |
def generate_keywords(caption):
|
|
@@ -40,7 +36,7 @@ def generate_keywords(caption):
|
|
| 40 |
list: A list of single-word keywords.
|
| 41 |
"""
|
| 42 |
words = caption.split()
|
| 43 |
-
keywords = list(set(words))[:50]
|
| 44 |
return keywords
|
| 45 |
|
| 46 |
def process_images(image_files):
|
|
@@ -55,7 +51,7 @@ def process_images(image_files):
|
|
| 55 |
"""
|
| 56 |
metadata = []
|
| 57 |
temp_dir = tempfile.mkdtemp()
|
| 58 |
-
|
| 59 |
for image_file in image_files:
|
| 60 |
filename = os.path.basename(image_file.name)
|
| 61 |
image = Image.open(image_file)
|
|
@@ -68,7 +64,7 @@ def process_images(image_files):
|
|
| 68 |
'title': title,
|
| 69 |
'keywords': keywords
|
| 70 |
})
|
| 71 |
-
|
| 72 |
# Create CSV file
|
| 73 |
csv_file_path = os.path.join(temp_dir, 'images_metadata.csv')
|
| 74 |
with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file:
|
|
@@ -79,14 +75,14 @@ def process_images(image_files):
|
|
| 79 |
title = data['title']
|
| 80 |
keywords = ','.join(data['keywords'])
|
| 81 |
writer.writerow([filename, title, keywords])
|
| 82 |
-
|
| 83 |
return [Image.open(image_file.name) for image_file in image_files], csv_file_path
|
| 84 |
|
| 85 |
# Define Gradio interface
|
| 86 |
iface = gr.Interface(
|
| 87 |
fn=process_images,
|
| 88 |
inputs=[
|
| 89 |
-
gr.
|
| 90 |
],
|
| 91 |
outputs=[
|
| 92 |
gr.Gallery(label="Processed Images"),
|
|
|
|
| 19 |
Returns:
|
| 20 |
str: The generated caption.
|
| 21 |
"""
|
|
|
|
| 22 |
image = image.convert("RGB")
|
|
|
|
|
|
|
| 23 |
inputs = processor(images=image, return_tensors="pt")
|
| 24 |
outputs = model.generate(**inputs)
|
| 25 |
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 26 |
return caption
|
| 27 |
|
| 28 |
def generate_keywords(caption):
|
|
|
|
| 36 |
list: A list of single-word keywords.
|
| 37 |
"""
|
| 38 |
words = caption.split()
|
| 39 |
+
keywords = list(set(words))[:50]
|
| 40 |
return keywords
|
| 41 |
|
| 42 |
def process_images(image_files):
|
|
|
|
| 51 |
"""
|
| 52 |
metadata = []
|
| 53 |
temp_dir = tempfile.mkdtemp()
|
| 54 |
+
|
| 55 |
for image_file in image_files:
|
| 56 |
filename = os.path.basename(image_file.name)
|
| 57 |
image = Image.open(image_file)
|
|
|
|
| 64 |
'title': title,
|
| 65 |
'keywords': keywords
|
| 66 |
})
|
| 67 |
+
|
| 68 |
# Create CSV file
|
| 69 |
csv_file_path = os.path.join(temp_dir, 'images_metadata.csv')
|
| 70 |
with open(csv_file_path, mode='w', newline='', encoding='utf-8') as file:
|
|
|
|
| 75 |
title = data['title']
|
| 76 |
keywords = ','.join(data['keywords'])
|
| 77 |
writer.writerow([filename, title, keywords])
|
| 78 |
+
|
| 79 |
return [Image.open(image_file.name) for image_file in image_files], csv_file_path
|
| 80 |
|
| 81 |
# Define Gradio interface
|
| 82 |
iface = gr.Interface(
|
| 83 |
fn=process_images,
|
| 84 |
inputs=[
|
| 85 |
+
gr.Files(label="Upload Image Files") # Use gr.Files for multiple file uploads
|
| 86 |
],
|
| 87 |
outputs=[
|
| 88 |
gr.Gallery(label="Processed Images"),
|