File size: 3,394 Bytes
fd9dc15
 
 
 
9520761
fd9dc15
9520761
 
 
fd9dc15
 
 
 
 
 
9520761
 
 
 
 
fd9dc15
9520761
fd9dc15
9520761
 
65ca49f
9520761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd9dc15
9520761
 
 
fd9dc15
9520761
 
 
 
 
 
 
fd9dc15
 
 
65ca49f
fd9dc15
 
9520761
 
65ca49f
9520761
fd9dc15
 
65ca49f
 
 
 
 
 
 
 
fd9dc15
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import gradio as gr
from PIL import Image
import json

def extract_parameters(image, show_full_metadata=False):
    """
    Extract and format parameters from a ComfyUI image.
    If show_full_metadata is False, shows only the main prompt cleanly.
    If True, shows the full metadata with better formatting.
    """
    try:
        # Open the uploaded image
        img = Image.open(image)
        # Get metadata from the image's info dictionary
        metadata = img.info

        if not metadata:
            return "No parameters found in image metadata."

        # Check if "prompt" key exists (common in ComfyUI)
        if "prompt" in metadata:
            raw_prompt = metadata["prompt"]
            try:
                # Parse as JSON if possible
                prompt_data = json.loads(raw_prompt)
                # Look for the main prompt text
                main_prompt = None
                for node_id, node in prompt_data.items():
                    if "inputs" in node and "text" in node["inputs"]:
                        text = node["inputs"]["text"]
                        if isinstance(text, str) and text.strip():
                            main_prompt = text
                            break
                
                if not main_prompt:
                    main_prompt = "No clear prompt found in workflow."

                if show_full_metadata:
                    full_output = "Full Workflow Metadata:\n\n"
                    for node_id, node in prompt_data.items():
                        full_output += f"Node {node_id}:\n"
                        for key, value in node.items():
                            full_output += f"  {key}: {value}\n"
                        full_output += "\n"
                    return f"Main Prompt:\n{main_prompt}\n\n{full_output}"
                else:
                    return f"Main Prompt:\n{main_prompt}"
            except json.JSONDecodeError:
                if show_full_metadata:
                    return f"Raw Prompt:\n{raw_prompt}\n\nFull Metadata:\n{metadata}"
                return f"Main Prompt:\n{raw_prompt}"
        else:
            if show_full_metadata:
                output = "Full Metadata:\n\n"
                for key, value in metadata.items():
                    output += f"{key}:\n  {value}\n\n"
                return output
            return "No prompt found, toggle 'Show Full Metadata' for details."

    except Exception as e:
        return f"Error processing image: {str(e)}"

# Define the Gradio interface with attribution
interface = gr.Interface(
    fn=extract_parameters,
    inputs=[
        gr.Image(type="filepath", label="Upload a ComfyUI Image"),
        gr.Checkbox(label="Show Full Metadata", value=False)
    ],
    outputs=gr.Textbox(label="Extracted Parameters", lines=10),
    title="ComfyUI Image to Parameters",
    description=(
        "Upload a ComfyUI image to extract its prompt. Toggle 'Show Full Metadata' for the complete workflow.\n\n"
        "This app is inspired by and adapted from ImagePromptViewer by LordKa-Berlin. "
        "Original creator: LordKa-Berlin. "
        "Licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). "
        "Source: https://github.com/LordKa-Berlin/ImagePromptViewer. "
        "Modified by QES for Gradio integration."
    ),
    theme="default"
)

# Launch the app
interface.launch()