Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,38 +9,36 @@ def extract_metadata(image):
|
|
9 |
|
10 |
try:
|
11 |
metadata = {}
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
metadata = json.loads(image.info['Comment'])
|
18 |
-
metadata['model'] = 'NovelAI'
|
19 |
-
elif 'parameters' in image.info:
|
20 |
-
if image.info['parameters'].startswith('{'):
|
21 |
-
parameters_data = json.loads(image.info['parameters'])
|
22 |
-
if 'sui_image_params' in parameters_data:
|
23 |
-
sui_image_params = parameters_data['sui_image_params']
|
24 |
-
metadata.update(sui_image_params)
|
25 |
-
else:
|
26 |
-
metadata = parameters_data
|
27 |
-
else:
|
28 |
-
lines = image.info['parameters'].split('\n')
|
29 |
-
prompt = lines[0].strip()
|
30 |
-
negative_prompt = lines[1].strip().replace('Negative prompt:', '').strip()
|
31 |
-
metadata['prompt'] = prompt
|
32 |
-
metadata['negative_prompt'] = negative_prompt
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
metadata[key.strip()] = value.strip()
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
return "No supported metadata found in the image.", {}
|
45 |
|
46 |
return "Metadata extracted successfully.", metadata
|
@@ -68,6 +66,7 @@ with gr.Blocks() as demo:
|
|
68 |
status_output = gr.Textbox(label="Status")
|
69 |
output_metadata = gr.JSON(label="Metadata")
|
70 |
|
|
|
71 |
input_image.change(
|
72 |
fn=process_image,
|
73 |
inputs=input_image,
|
@@ -75,6 +74,4 @@ with gr.Blocks() as demo:
|
|
75 |
api_name="interrogate"
|
76 |
)
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
demo.launch()
|
|
|
9 |
|
10 |
try:
|
11 |
metadata = {}
|
12 |
+
# Handling multiple possible metadata keys
|
13 |
+
potential_keys = ['metadata', 'prompt', 'Comment', 'parameters', 'exif']
|
14 |
+
for key in potential_keys:
|
15 |
+
if key in image.info:
|
16 |
+
raw_data = image.info[key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
# If raw data starts with '{', assume JSON format
|
19 |
+
if raw_data.startswith('{'):
|
20 |
+
metadata = json.loads(raw_data)
|
21 |
+
else:
|
22 |
+
if key == 'parameters':
|
23 |
+
# Attempt to process Stable Diffusion or NovelAI style data
|
24 |
+
lines = raw_data.split('\n')
|
25 |
+
prompt = lines[0].strip()
|
26 |
+
negative_prompt = lines[1].strip().replace('Negative prompt:', '').strip()
|
27 |
+
metadata['prompt'] = prompt
|
28 |
+
metadata['negative_prompt'] = negative_prompt
|
29 |
+
|
30 |
+
for line in lines[2:]:
|
31 |
+
line = line.strip()
|
32 |
+
if ':' in line:
|
33 |
+
key, value = line.split(':', 1)
|
34 |
metadata[key.strip()] = value.strip()
|
35 |
+
elif key == 'Comment':
|
36 |
+
# Specific handling for NovelAI
|
37 |
+
metadata = json.loads(raw_data)
|
38 |
+
metadata['model'] = 'NovelAI'
|
39 |
+
break # Exit loop once a supported key is found
|
40 |
+
|
41 |
+
if not metadata:
|
42 |
return "No supported metadata found in the image.", {}
|
43 |
|
44 |
return "Metadata extracted successfully.", metadata
|
|
|
66 |
status_output = gr.Textbox(label="Status")
|
67 |
output_metadata = gr.JSON(label="Metadata")
|
68 |
|
69 |
+
# Event listener for when the image is changed
|
70 |
input_image.change(
|
71 |
fn=process_image,
|
72 |
inputs=input_image,
|
|
|
74 |
api_name="interrogate"
|
75 |
)
|
76 |
|
77 |
+
demo.launch()
|
|
|
|