File size: 6,547 Bytes
b17dd62
 
 
a665d4d
 
b17dd62
 
 
 
 
 
 
 
 
 
 
a665d4d
b17dd62
 
 
 
 
 
a665d4d
 
 
 
 
 
 
 
 
 
b17dd62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a665d4d
 
b17dd62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""
ComfyUI Metadata Viewer
========================
Drag & drop a file produced by ComfyUI (PNG, MP3, FLAC, MP4...) and it
displays, as readable tables:
  - the model and LoRAs used
  - the main parameters (steps, cfg, sampler, seed, duration...)
  - the final prompt actually used, and the "dynamic prompt" { | }
    template that generated it, when there is one.
"""

import html
import re
import traceback

import gradio as gr
import spaces

from comfyui_meta import analyze, MetadataNotFound, StructuredView

BRACE_RE = re.compile(r"(\{[^{}]*\})")


@spaces.GPU(duration=1)
def _zerogpu_startup_probe():
    """This app never needs a GPU β€” it only reads metadata out of a
    file. But Spaces running on the free ZeroGPU tier require at least
    one @spaces.GPU-decorated function to exist, or the Space refuses
    to start. This function exists only to satisfy that check; it is
    never called."""
    return None


def _highlight_template(text: str) -> str:
    """Escapes HTML and highlights {a | b | c} blocks."""
    escaped = html.escape(text)
    # re-apply the pattern on the escaped text (braces are unaffected
    # by html.escape)
    parts = BRACE_RE.split(escaped)
    out = []
    for part in parts:
        if part.startswith("{") and part.endswith("}"):
            out.append(f'<span class="wildcard">{part}</span>')
        else:
            out.append(part)
    return "".join(out)


def _models_table(view: StructuredView):
    rows = [[m.kind, m.name, m.detail, m.node] for m in view.models]
    if not rows:
        rows = [["β€”", "No model/LoRA detected", "", ""]]
    return rows


def _params_table(view: StructuredView):
    rows = [[p.node, p.param, p.value] for p in view.params]
    if not rows:
        rows = [["β€”", "No parameter detected", ""]]
    return rows


def _prompts_html(view: StructuredView) -> str:
    if not view.prompts:
        return "<p><em>No prompt detected in this file.</em></p>"

    cards = []
    for p in view.prompts:
        template_block = ""
        if p.template:
            template_block = f"""
            <div class="field-box template-box">
                <div class="label">🎲 Dynamic template (raw)</div>
                <div class="text mono">{_highlight_template(p.template)}</div>
            </div>
            """
        cards.append(f"""
        <div class="prompt-card">
            <div class="card-title">{html.escape(p.node)} β€” {html.escape(p.role)}</div>
            {template_block}
            <div class="field-box resolved-box">
                <div class="label">βœ… Final prompt (resolved)</div>
                <div class="text">{html.escape(p.resolved)}</div>
            </div>
        </div>
        """)

    style = """
    <style>
      .prompt-card { margin-bottom: 20px; }
      .prompt-card .card-title { font-weight: 600; margin-bottom: 8px; opacity: 0.85; }
      /* Same "gray frame" look as the Gradio file drop zone, reusing the
         theme's own tokens so it matches light/dark mode automatically. */
      .field-box {
          border: var(--block-border-width, 1px) solid var(--block-border-color, rgba(128,128,128,0.35));
          border-radius: var(--block-radius, 8px);
          background: var(--block-background-fill, transparent);
          padding: 12px 14px;
          margin-bottom: 10px;
      }
      .field-box .label { font-size: 0.85em; text-transform: uppercase;
                           letter-spacing: 0.03em; opacity: 0.6; margin-bottom: 6px; }
      .field-box .text { white-space: pre-wrap; line-height: 1.4; }
      .field-box .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
                          font-size: 0.92em; }
      .wildcard { background: rgba(63, 92, 168, 0.28); border-radius: 4px;
                  padding: 0 2px; font-weight: normal; }
    </style>
    """
    return style + "\n".join(cards)


def _warnings_html(view: StructuredView) -> str:
    if not view.warnings:
        return ""
    items = "".join(f"<li>{html.escape(w)}</li>" for w in view.warnings)
    return f"<div style='opacity:0.75; font-size:0.9em;'>⚠️ <ul>{items}</ul></div>"


def process_file(file_path):
    if not file_path:
        return [], [], "", "", "Drop a file above to get started."

    try:
        view = analyze(file_path)
    except MetadataNotFound as e:
        return [], [], "", "", f"❌ {e}"
    except Exception:
        return [], [], "", "", (
            "❌ Unexpected error during analysis:\n\n" + traceback.format_exc()
        )

    status = "βœ… ComfyUI metadata found and analyzed."
    return (
        _models_table(view),
        _params_table(view),
        _prompts_html(view),
        _warnings_html(view),
        status,
    )


with gr.Blocks(title="ComfyUI Metadata Viewer") as demo:
    gr.Markdown(
        "# πŸ” ComfyUI Metadata Viewer\n"
        "Drag & drop a file generated by ComfyUI (PNG, MP3, FLAC, MP4...) "
        "to see the model, LoRAs, parameters and prompt (with its original "
        "dynamic prompt, if there is one)."
    )

    file_input = gr.File(
        label="ComfyUI file (drag & drop)",
        file_types=[".png", ".webp", ".jpg", ".jpeg",
                    ".mp3", ".flac", ".m4a", ".ogg",
                    ".mp4", ".mov", ".webm", ".mkv"],
        type="filepath",
    )
    clear_btn = gr.Button("πŸ—‘οΈ Clear", size="sm", variant="secondary")

    status_box = gr.Markdown("Drop a file above to get started.")

    with gr.Tab("🧩 Model & LoRAs"):
        models_df = gr.Dataframe(
            headers=["Type", "Name", "Detail", "Node"],
            datatype=["str", "str", "str", "str"],
            row_count=(0, "dynamic"),
            wrap=True,
        )

    with gr.Tab("βš™οΈ Main Parameters"):
        params_df = gr.Dataframe(
            headers=["Node", "Parameter", "Value"],
            datatype=["str", "str", "str"],
            row_count=(0, "dynamic"),
            wrap=True,
        )

    with gr.Tab("πŸ“ Prompt & Dynamic Prompt"):
        prompts_html = gr.HTML()

    warnings_html = gr.HTML()

    file_input.change(
        fn=process_file,
        inputs=[file_input],
        outputs=[models_df, params_df, prompts_html, warnings_html, status_box],
    )

    def _clear_all():
        return None, [], [], "", "", "Drop a file above to get started."

    clear_btn.click(
        fn=_clear_all,
        inputs=[],
        outputs=[file_input, models_df, params_df, prompts_html, warnings_html, status_box],
    )

if __name__ == "__main__":
    demo.launch()