Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,49 +2,79 @@ import gradio as gr
|
|
2 |
from PIL import Image
|
3 |
import json
|
4 |
|
5 |
-
def extract_parameters(image):
|
6 |
"""
|
7 |
-
Extract
|
8 |
-
|
|
|
9 |
"""
|
10 |
try:
|
11 |
# Open the uploaded image
|
12 |
img = Image.open(image)
|
13 |
# Get metadata from the image's info dictionary
|
14 |
metadata = img.info
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
17 |
if "prompt" in metadata:
|
18 |
-
|
19 |
-
# If it's a JSON string, try to parse it
|
20 |
try:
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
except json.JSONDecodeError:
|
28 |
-
# If not JSON,
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
output = "Metadata Found:\n"
|
33 |
-
for key, value in metadata.items():
|
34 |
-
output += f"{key}: {value}\n"
|
35 |
-
return output
|
36 |
else:
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
except Exception as e:
|
39 |
return f"Error processing image: {str(e)}"
|
40 |
|
41 |
# Define the Gradio interface
|
42 |
interface = gr.Interface(
|
43 |
fn=extract_parameters,
|
44 |
-
inputs=
|
|
|
|
|
|
|
45 |
outputs=gr.Textbox(label="Extracted Parameters", lines=10),
|
46 |
title="ComfyUI Image to Parameters",
|
47 |
-
description="Upload
|
48 |
theme="default"
|
49 |
)
|
50 |
|
|
|
2 |
from PIL import Image
|
3 |
import json
|
4 |
|
5 |
+
def extract_parameters(image, show_full_metadata=False):
|
6 |
"""
|
7 |
+
Extract and format parameters from a ComfyUI image.
|
8 |
+
If show_full_metadata is False, shows only the main prompt cleanly.
|
9 |
+
If True, shows the full metadata with better formatting.
|
10 |
"""
|
11 |
try:
|
12 |
# Open the uploaded image
|
13 |
img = Image.open(image)
|
14 |
# Get metadata from the image's info dictionary
|
15 |
metadata = img.info
|
16 |
+
|
17 |
+
if not metadata:
|
18 |
+
return "No parameters found in image metadata."
|
19 |
+
|
20 |
+
# Check if "prompt" key exists (common in ComfyUI)
|
21 |
if "prompt" in metadata:
|
22 |
+
raw_prompt = metadata["prompt"]
|
|
|
23 |
try:
|
24 |
+
# Parse as JSON if possible
|
25 |
+
prompt_data = json.loads(raw_prompt)
|
26 |
+
# Look for the main prompt text (often in a node like '51' from your output)
|
27 |
+
main_prompt = None
|
28 |
+
for node_id, node in prompt_data.items():
|
29 |
+
if "inputs" in node and "text" in node["inputs"]:
|
30 |
+
# Prioritize nodes with meaningful text input
|
31 |
+
text = node["inputs"]["text"]
|
32 |
+
if isinstance(text, str) and text.strip():
|
33 |
+
main_prompt = text
|
34 |
+
break
|
35 |
+
|
36 |
+
if not main_prompt:
|
37 |
+
main_prompt = "No clear prompt found in workflow."
|
38 |
+
|
39 |
+
if show_full_metadata:
|
40 |
+
# Format full metadata with spacing
|
41 |
+
full_output = "Full Workflow Metadata:\n\n"
|
42 |
+
for node_id, node in prompt_data.items():
|
43 |
+
full_output += f"Node {node_id}:\n"
|
44 |
+
for key, value in node.items():
|
45 |
+
full_output += f" {key}: {value}\n"
|
46 |
+
full_output += "\n"
|
47 |
+
return f"Main Prompt:\n{main_prompt}\n\n{full_output}"
|
48 |
+
else:
|
49 |
+
# Just the cleaned-up main prompt
|
50 |
+
return f"Main Prompt:\n{main_prompt}"
|
51 |
except json.JSONDecodeError:
|
52 |
+
# If not JSON, treat as raw text
|
53 |
+
if show_full_metadata:
|
54 |
+
return f"Raw Prompt:\n{raw_prompt}\n\nFull Metadata:\n{metadata}"
|
55 |
+
return f"Main Prompt:\n{raw_prompt}"
|
|
|
|
|
|
|
|
|
56 |
else:
|
57 |
+
# No "prompt" key, show all metadata if toggled
|
58 |
+
if show_full_metadata:
|
59 |
+
output = "Full Metadata:\n\n"
|
60 |
+
for key, value in metadata.items():
|
61 |
+
output += f"{key}:\n {value}\n\n"
|
62 |
+
return output
|
63 |
+
return "No prompt found, toggle 'Show Full Metadata' for details."
|
64 |
+
|
65 |
except Exception as e:
|
66 |
return f"Error processing image: {str(e)}"
|
67 |
|
68 |
# Define the Gradio interface
|
69 |
interface = gr.Interface(
|
70 |
fn=extract_parameters,
|
71 |
+
inputs=[
|
72 |
+
gr.Image(type="filepath", label="Upload a ComfyUI Image"),
|
73 |
+
gr.Checkbox(label="Show Full Metadata", default=False)
|
74 |
+
],
|
75 |
outputs=gr.Textbox(label="Extracted Parameters", lines=10),
|
76 |
title="ComfyUI Image to Parameters",
|
77 |
+
description="Upload a ComfyUI image to extract its prompt. Toggle 'Show Full Metadata' for the complete workflow.",
|
78 |
theme="default"
|
79 |
)
|
80 |
|