Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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 |
+
|
51 |
+
# Launch the app
|
52 |
+
interface.launch()
|