QES commited on
Commit
b17dd62
Β·
verified Β·
1 Parent(s): ef80cc8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +186 -0
app.py ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ComfyUI Metadata Viewer
3
+ ========================
4
+ Small Gradio app: drag & drop a file produced by ComfyUI (PNG, MP3,
5
+ FLAC, MP4...) and it displays, as readable tables:
6
+ - the model and LoRAs used
7
+ - the main parameters (steps, cfg, sampler, seed, duration...)
8
+ - the final prompt actually used, and the "dynamic prompt" { | }
9
+ template that generated it, when there is one.
10
+ """
11
+
12
+ import html
13
+ import re
14
+ import traceback
15
+
16
+ import gradio as gr
17
+
18
+ from comfyui_meta import analyze, MetadataNotFound, StructuredView
19
+
20
+ BRACE_RE = re.compile(r"(\{[^{}]*\})")
21
+
22
+
23
+ def _highlight_template(text: str) -> str:
24
+ """Escapes HTML and highlights {a | b | c} blocks."""
25
+ escaped = html.escape(text)
26
+ # re-apply the pattern on the escaped text (braces are unaffected
27
+ # by html.escape)
28
+ parts = BRACE_RE.split(escaped)
29
+ out = []
30
+ for part in parts:
31
+ if part.startswith("{") and part.endswith("}"):
32
+ out.append(f'<span class="wildcard">{part}</span>')
33
+ else:
34
+ out.append(part)
35
+ return "".join(out)
36
+
37
+
38
+ def _models_table(view: StructuredView):
39
+ rows = [[m.kind, m.name, m.detail, m.node] for m in view.models]
40
+ if not rows:
41
+ rows = [["β€”", "No model/LoRA detected", "", ""]]
42
+ return rows
43
+
44
+
45
+ def _params_table(view: StructuredView):
46
+ rows = [[p.node, p.param, p.value] for p in view.params]
47
+ if not rows:
48
+ rows = [["β€”", "No parameter detected", ""]]
49
+ return rows
50
+
51
+
52
+ def _prompts_html(view: StructuredView) -> str:
53
+ if not view.prompts:
54
+ return "<p><em>No prompt detected in this file.</em></p>"
55
+
56
+ cards = []
57
+ for p in view.prompts:
58
+ template_block = ""
59
+ if p.template:
60
+ template_block = f"""
61
+ <div class="field-box template-box">
62
+ <div class="label">🎲 Dynamic template (raw)</div>
63
+ <div class="text mono">{_highlight_template(p.template)}</div>
64
+ </div>
65
+ """
66
+ cards.append(f"""
67
+ <div class="prompt-card">
68
+ <div class="card-title">{html.escape(p.node)} β€” {html.escape(p.role)}</div>
69
+ {template_block}
70
+ <div class="field-box resolved-box">
71
+ <div class="label">βœ… Final prompt (resolved)</div>
72
+ <div class="text">{html.escape(p.resolved)}</div>
73
+ </div>
74
+ </div>
75
+ """)
76
+
77
+ style = """
78
+ <style>
79
+ .prompt-card { margin-bottom: 20px; }
80
+ .prompt-card .card-title { font-weight: 600; margin-bottom: 8px; opacity: 0.85; }
81
+ .field-box {
82
+ border: var(--block-border-width, 1px) solid var(--block-border-color, rgba(128,128,128,0.35));
83
+ border-radius: var(--block-radius, 8px);
84
+ background: var(--block-background-fill, transparent);
85
+ padding: 12px 14px;
86
+ margin-bottom: 10px;
87
+ }
88
+ .field-box .label { font-size: 0.85em; text-transform: uppercase;
89
+ letter-spacing: 0.03em; opacity: 0.6; margin-bottom: 6px; }
90
+ .field-box .text { white-space: pre-wrap; line-height: 1.4; }
91
+ .field-box .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
92
+ font-size: 0.92em; }
93
+ .wildcard { background: rgba(63, 92, 168, 0.28); border-radius: 4px;
94
+ padding: 0 2px; font-weight: normal; }
95
+ </style>
96
+ """
97
+ return style + "\n".join(cards)
98
+
99
+
100
+ def _warnings_html(view: StructuredView) -> str:
101
+ if not view.warnings:
102
+ return ""
103
+ items = "".join(f"<li>{html.escape(w)}</li>" for w in view.warnings)
104
+ return f"<div style='opacity:0.75; font-size:0.9em;'>⚠️ <ul>{items}</ul></div>"
105
+
106
+
107
+ def process_file(file_path):
108
+ if not file_path:
109
+ return [], [], "", "", "Drop a file above to get started."
110
+
111
+ try:
112
+ view = analyze(file_path)
113
+ except MetadataNotFound as e:
114
+ return [], [], "", "", f"❌ {e}"
115
+ except Exception:
116
+ return [], [], "", "", (
117
+ "❌ Unexpected error during analysis:\n\n" + traceback.format_exc()
118
+ )
119
+
120
+ status = "βœ… ComfyUI metadata found and analyzed."
121
+ return (
122
+ _models_table(view),
123
+ _params_table(view),
124
+ _prompts_html(view),
125
+ _warnings_html(view),
126
+ status,
127
+ )
128
+
129
+
130
+ with gr.Blocks(title="ComfyUI Metadata Viewer") as demo:
131
+ gr.Markdown(
132
+ "# πŸ” ComfyUI Metadata Viewer\n"
133
+ "Drag & drop a file generated by ComfyUI (PNG, MP3, FLAC, MP4...) "
134
+ "to see the model, LoRAs, parameters and prompt (with its original "
135
+ "dynamic prompt, if there is one)."
136
+ )
137
+
138
+ file_input = gr.File(
139
+ label="ComfyUI file (drag & drop)",
140
+ file_types=[".png", ".webp", ".jpg", ".jpeg",
141
+ ".mp3", ".flac", ".m4a", ".ogg",
142
+ ".mp4", ".mov", ".webm", ".mkv"],
143
+ type="filepath",
144
+ )
145
+ clear_btn = gr.Button("πŸ—‘οΈ Clear", size="sm", variant="secondary")
146
+
147
+ status_box = gr.Markdown("Drop a file above to get started.")
148
+
149
+ with gr.Tab("🧩 Model & LoRAs"):
150
+ models_df = gr.Dataframe(
151
+ headers=["Type", "Name", "Detail", "Node"],
152
+ datatype=["str", "str", "str", "str"],
153
+ row_count=(0, "dynamic"),
154
+ wrap=True,
155
+ )
156
+
157
+ with gr.Tab("βš™οΈ Main Parameters"):
158
+ params_df = gr.Dataframe(
159
+ headers=["Node", "Parameter", "Value"],
160
+ datatype=["str", "str", "str"],
161
+ row_count=(0, "dynamic"),
162
+ wrap=True,
163
+ )
164
+
165
+ with gr.Tab("πŸ“ Prompt & Dynamic Prompt"):
166
+ prompts_html = gr.HTML()
167
+
168
+ warnings_html = gr.HTML()
169
+
170
+ file_input.change(
171
+ fn=process_file,
172
+ inputs=[file_input],
173
+ outputs=[models_df, params_df, prompts_html, warnings_html, status_box],
174
+ )
175
+
176
+ def _clear_all():
177
+ return None, [], [], "", "", "Drop a file above to get started."
178
+
179
+ clear_btn.click(
180
+ fn=_clear_all,
181
+ inputs=[],
182
+ outputs=[file_input, models_df, params_df, prompts_html, warnings_html, status_box],
183
+ )
184
+
185
+ if __name__ == "__main__":
186
+ demo.launch()