tlam
commited on
Commit
·
564378d
0
Parent(s):
button
Browse files- .DS_Store +0 -0
- .gitignore +1 -0
- README.md +13 -0
- app.py +109 -0
- gitattributes +35 -0
- requirements.txt +2 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
/venv
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Metadata
|
| 3 |
+
emoji: 🐨
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from PIL.ExifTags import TAGS
|
| 4 |
+
import numpy as np
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
def extract_metadata(image_file):
|
| 8 |
+
img = Image.open(image_file)
|
| 9 |
+
metadata = {}
|
| 10 |
+
|
| 11 |
+
# Extract metadata
|
| 12 |
+
if img.format == 'PNG':
|
| 13 |
+
metadata = img.info
|
| 14 |
+
elif img.format in ['JPEG', 'TIFF']:
|
| 15 |
+
exif_data = img._getexif()
|
| 16 |
+
if exif_data:
|
| 17 |
+
for tag, value in exif_data.items():
|
| 18 |
+
tag_name = TAGS.get(tag, tag)
|
| 19 |
+
metadata[tag_name] = value
|
| 20 |
+
|
| 21 |
+
# Convert image to NumPy array for display
|
| 22 |
+
img_display = np.array(img)
|
| 23 |
+
|
| 24 |
+
# Initialize outputs
|
| 25 |
+
prompt = 'N/A'
|
| 26 |
+
negative_prompt = 'N/A'
|
| 27 |
+
adetailer_prompt_1 = 'N/A'
|
| 28 |
+
adetailer_prompt_2 = 'N/A'
|
| 29 |
+
|
| 30 |
+
# Convert the entire metadata dictionary to a readable string
|
| 31 |
+
metadata_str = "\n".join([f"{key}: {value}" for key, value in metadata.items()])
|
| 32 |
+
|
| 33 |
+
# Parse metadata to extract prompts
|
| 34 |
+
if 'parameters' in metadata:
|
| 35 |
+
parameters = metadata['parameters']
|
| 36 |
+
|
| 37 |
+
# Use regular expressions to extract prompts
|
| 38 |
+
# Extract the prompt (before 'Negative prompt:')
|
| 39 |
+
prompt_match = re.search(r'(.*?)Negative prompt:', parameters, re.DOTALL)
|
| 40 |
+
if prompt_match:
|
| 41 |
+
prompt = prompt_match.group(1).strip()
|
| 42 |
+
else:
|
| 43 |
+
# If 'Negative prompt:' not found, try to extract prompt up to 'Steps:' or end of string
|
| 44 |
+
prompt_match = re.search(r'(.*?)(Steps:|$)', parameters, re.DOTALL)
|
| 45 |
+
if prompt_match:
|
| 46 |
+
prompt = prompt_match.group(1).strip()
|
| 47 |
+
|
| 48 |
+
# Extract the negative prompt
|
| 49 |
+
negative_prompt_match = re.search(r'Negative prompt:(.*?)(Steps:|$)', parameters, re.DOTALL)
|
| 50 |
+
if negative_prompt_match:
|
| 51 |
+
negative_prompt = negative_prompt_match.group(1).strip()
|
| 52 |
+
|
| 53 |
+
# Extract ADetailer prompt 1
|
| 54 |
+
adetailer_prompt_1_match = re.search(r'ADetailer prompt:\s*"(.*?)"', parameters)
|
| 55 |
+
if adetailer_prompt_1_match:
|
| 56 |
+
adetailer_prompt_1 = adetailer_prompt_1_match.group(1).strip()
|
| 57 |
+
|
| 58 |
+
# Extract ADetailer prompt 2
|
| 59 |
+
adetailer_prompt_2_match = re.search(r'ADetailer negative prompt 2nd:\s*"(.*?)"', parameters)
|
| 60 |
+
if adetailer_prompt_2_match:
|
| 61 |
+
adetailer_prompt_2 = adetailer_prompt_2_match.group(1).strip()
|
| 62 |
+
|
| 63 |
+
seed_match = re.search(r'Seed:\s*(\d+)', parameters)
|
| 64 |
+
if seed_match:
|
| 65 |
+
seed_number = seed_match.group(1).strip()
|
| 66 |
+
|
| 67 |
+
else:
|
| 68 |
+
# No 'parameters' field in metadata
|
| 69 |
+
prompt = 'N/A'
|
| 70 |
+
|
| 71 |
+
# Return the outputs, replacing 'original_metadata_output' with 'metadata_str'
|
| 72 |
+
return img_display, prompt, negative_prompt, seed_number, adetailer_prompt_1, adetailer_prompt_2, metadata_str
|
| 73 |
+
|
| 74 |
+
# Create the Gradio interface
|
| 75 |
+
with gr.Blocks() as iface:
|
| 76 |
+
gr.Markdown("# Image Metadata Extractor")
|
| 77 |
+
gr.Markdown("Upload an image (PNG, JPEG, TIFF) to extract its metadata and parse it for prompts.")
|
| 78 |
+
|
| 79 |
+
with gr.Row():
|
| 80 |
+
with gr.Column(): # First column for image upload and preview
|
| 81 |
+
image_input = gr.File(label="Upload Image", type="filepath")
|
| 82 |
+
image_output = gr.Image(label="Image Preview")
|
| 83 |
+
|
| 84 |
+
with gr.Column(): # Second column for metadata outputs
|
| 85 |
+
prompt_output = gr.Textbox(label="Prompt", lines=4, show_copy_button=True)
|
| 86 |
+
negative_prompt_output = gr.Textbox(label="Negative Prompt", lines=4, show_copy_button=True)
|
| 87 |
+
seed_output = gr.Textbox(label="Seed Number", lines=1, show_copy_button=True)
|
| 88 |
+
adetailer_prompt_1_output = gr.Textbox(label="ADetailer Prompt 1", lines=3, show_copy_button=True)
|
| 89 |
+
adetailer_prompt_2_output = gr.Textbox(label="ADetailer Prompt 2", lines=3, show_copy_button=True)
|
| 90 |
+
with gr.Row(): # Second column for metadata outputs
|
| 91 |
+
original_metadata_output = gr.Textbox(label="Original Metadata", lines=10)
|
| 92 |
+
|
| 93 |
+
# Set up the input-output relationships
|
| 94 |
+
image_input.change(
|
| 95 |
+
fn=extract_metadata,
|
| 96 |
+
inputs=image_input,
|
| 97 |
+
outputs=[
|
| 98 |
+
image_output,
|
| 99 |
+
prompt_output,
|
| 100 |
+
negative_prompt_output,
|
| 101 |
+
seed_output,
|
| 102 |
+
adetailer_prompt_1_output,
|
| 103 |
+
adetailer_prompt_2_output,
|
| 104 |
+
original_metadata_output,
|
| 105 |
+
]
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Launch the app
|
| 109 |
+
iface.launch()
|
gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
Pillow
|