QES commited on
Commit
9520761
·
verified ·
1 Parent(s): fd9dc15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -24
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 metadata/parameters from a ComfyUI-generated image.
8
- Returns a formatted string with the extracted info.
 
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
- # Check common ComfyUI metadata keys (adjust based on your images)
 
 
 
17
  if "prompt" in metadata:
18
- prompt = metadata["prompt"]
19
- # If it's a JSON string, try to parse it
20
  try:
21
- prompt_data = json.loads(prompt)
22
- # Format JSON nicely
23
- output = "Extracted Parameters:\n"
24
- for key, value in prompt_data.items():
25
- output += f"{key}: {value}\n"
26
- return output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  except json.JSONDecodeError:
28
- # If not JSON, return raw prompt
29
- return f"Prompt: {prompt}"
30
- elif metadata:
31
- # If no "prompt" key, dump all metadata
32
- output = "Metadata Found:\n"
33
- for key, value in metadata.items():
34
- output += f"{key}: {value}\n"
35
- return output
36
  else:
37
- return "No parameters found in image metadata."
 
 
 
 
 
 
 
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=gr.Image(type="filepath", label="Upload a ComfyUI Image"),
 
 
 
45
  outputs=gr.Textbox(label="Extracted Parameters", lines=10),
46
  title="ComfyUI Image to Parameters",
47
- description="Upload an image generated by ComfyUI to extract its embedded parameters or prompt.",
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