Spaces:
Paused
Paused
IronJayx
commited on
Commit
β’
f85986b
1
Parent(s):
9844a5a
base commit
Browse files- .gitignore +2 -0
- README.md +9 -2
- app.py +251 -0
- requirements.txt +4 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
images/
|
2 |
+
.env
|
README.md
CHANGED
@@ -1,8 +1,15 @@
|
|
1 |
---
|
|
|
2 |
title: CreativeUpscaler
|
3 |
emoji: π
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.43.0
|
8 |
app_file: app.py
|
|
|
1 |
---
|
2 |
+
<<<<<<< HEAD
|
3 |
title: CreativeUpscaler
|
4 |
emoji: π
|
5 |
+
colorFrom: yellow
|
6 |
+
colorTo: indigo
|
7 |
+
=======
|
8 |
+
title: Creative Upscaler
|
9 |
+
emoji: π¦
|
10 |
+
colorFrom: pink
|
11 |
+
colorTo: green
|
12 |
+
>>>>>>> master
|
13 |
sdk: gradio
|
14 |
sdk_version: 4.43.0
|
15 |
app_file: app.py
|
app.py
ADDED
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_imageslider import ImageSlider
|
3 |
+
import os
|
4 |
+
from comfydeploy import ComfyDeploy
|
5 |
+
import requests
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
import base64
|
10 |
+
from typing import Optional, Tuple, Union
|
11 |
+
import glob
|
12 |
+
|
13 |
+
load_dotenv()
|
14 |
+
|
15 |
+
# Initialize ComfyDeploy client
|
16 |
+
client: ComfyDeploy = ComfyDeploy(bearer_auth=os.environ.get("COMFY_DEPLOY_API_KEY"))
|
17 |
+
deployment_id: str = os.environ.get("COMFY_DEPLOYMENT_ID")
|
18 |
+
|
19 |
+
# Add these global variables at the top of the file, after imports
|
20 |
+
global_input_image = None
|
21 |
+
global_image_slider = None
|
22 |
+
|
23 |
+
|
24 |
+
def clear_output():
|
25 |
+
return None
|
26 |
+
|
27 |
+
|
28 |
+
def process_image(
|
29 |
+
image: Optional[Union[str, Image.Image]],
|
30 |
+
denoise: float,
|
31 |
+
steps: int,
|
32 |
+
tile_size: int,
|
33 |
+
downscale: float,
|
34 |
+
upscale: float,
|
35 |
+
color_match: float,
|
36 |
+
controlnet_tile_end: float,
|
37 |
+
controlnet_tile_strength: float,
|
38 |
+
) -> Tuple[Optional[Image.Image], Optional[Image.Image]]:
|
39 |
+
# Convert image to base64
|
40 |
+
if image is not None:
|
41 |
+
if isinstance(image, str):
|
42 |
+
with open(image, "rb") as img_file:
|
43 |
+
image_base64: str = base64.b64encode(img_file.read()).decode("utf-8")
|
44 |
+
else:
|
45 |
+
buffered: BytesIO = BytesIO()
|
46 |
+
image.save(buffered, format="PNG")
|
47 |
+
image_base64: str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
48 |
+
else:
|
49 |
+
return None, None
|
50 |
+
|
51 |
+
# Prepare inputs
|
52 |
+
inputs: dict = {
|
53 |
+
"image": f"data:image/png;base64,{image_base64}",
|
54 |
+
"denoise": str(denoise),
|
55 |
+
"steps": str(steps),
|
56 |
+
"tile_size": str(tile_size),
|
57 |
+
"downscale": str(downscale),
|
58 |
+
"upscale": str(upscale),
|
59 |
+
"color_match": str(color_match),
|
60 |
+
"controlnet_tile_end": str(controlnet_tile_end),
|
61 |
+
"controlnet_tile_strength": str(controlnet_tile_strength),
|
62 |
+
}
|
63 |
+
|
64 |
+
# Call ComfyDeploy API
|
65 |
+
try:
|
66 |
+
result = client.run.create(
|
67 |
+
request={"deployment_id": deployment_id, "inputs": inputs}
|
68 |
+
)
|
69 |
+
|
70 |
+
if result and result.object:
|
71 |
+
run_id: str = result.object.run_id
|
72 |
+
# Wait for the result
|
73 |
+
while True:
|
74 |
+
run_result = client.run.get(run_id=run_id)
|
75 |
+
if run_result.object.status == "success":
|
76 |
+
for output in run_result.object.outputs:
|
77 |
+
if output.data and output.data.images:
|
78 |
+
image_url: str = output.data.images[0].url
|
79 |
+
# Download and return both the original and processed images
|
80 |
+
response: requests.Response = requests.get(image_url)
|
81 |
+
processed_image: Image.Image = Image.open(
|
82 |
+
BytesIO(response.content)
|
83 |
+
)
|
84 |
+
return image, processed_image
|
85 |
+
return None, None
|
86 |
+
elif run_result.object.status == "failed":
|
87 |
+
return None, None
|
88 |
+
except Exception as e:
|
89 |
+
print(f"Error: {e}")
|
90 |
+
return None, None
|
91 |
+
|
92 |
+
|
93 |
+
def run(
|
94 |
+
denoise,
|
95 |
+
steps,
|
96 |
+
tile_size,
|
97 |
+
downscale,
|
98 |
+
upscale,
|
99 |
+
color_match,
|
100 |
+
controlnet_tile_end,
|
101 |
+
controlnet_tile_strength,
|
102 |
+
):
|
103 |
+
global global_input_image
|
104 |
+
global global_image_slider
|
105 |
+
|
106 |
+
if not global_input_image:
|
107 |
+
return None
|
108 |
+
|
109 |
+
# Set image_slider to None before processing
|
110 |
+
global_image_slider = None
|
111 |
+
|
112 |
+
# Process the image
|
113 |
+
original, processed = process_image(
|
114 |
+
global_input_image,
|
115 |
+
denoise,
|
116 |
+
steps,
|
117 |
+
tile_size,
|
118 |
+
downscale,
|
119 |
+
upscale,
|
120 |
+
color_match,
|
121 |
+
controlnet_tile_end,
|
122 |
+
controlnet_tile_strength,
|
123 |
+
)
|
124 |
+
|
125 |
+
if original and processed:
|
126 |
+
global_image_slider = [original, processed]
|
127 |
+
|
128 |
+
return global_image_slider
|
129 |
+
|
130 |
+
|
131 |
+
# Function to load preset images
|
132 |
+
def load_preset_images():
|
133 |
+
image_files = glob.glob("images/inputs/*")
|
134 |
+
return [
|
135 |
+
{"name": img, "image": Image.open(img)}
|
136 |
+
for img in image_files
|
137 |
+
if Image.open(img).format.lower()
|
138 |
+
in ["png", "jpg", "jpeg", "gif", "bmp", "webp"]
|
139 |
+
]
|
140 |
+
|
141 |
+
|
142 |
+
def set_input_image(images, evt: gr.SelectData):
|
143 |
+
global global_input_image
|
144 |
+
global_input_image = images[evt.index][0]
|
145 |
+
return global_input_image
|
146 |
+
|
147 |
+
|
148 |
+
# Define Gradio interface
|
149 |
+
with gr.Blocks() as demo:
|
150 |
+
gr.Markdown("# π Creative Image Upscaler")
|
151 |
+
with gr.Row():
|
152 |
+
with gr.Column():
|
153 |
+
input_image = gr.Image(
|
154 |
+
type="pil",
|
155 |
+
label="Input Image",
|
156 |
+
value=lambda: global_input_image,
|
157 |
+
interactive=True,
|
158 |
+
)
|
159 |
+
|
160 |
+
# Add preset images
|
161 |
+
gr.Markdown("### Preset Images")
|
162 |
+
preset_images = load_preset_images()
|
163 |
+
gallery = gr.Gallery(
|
164 |
+
[img["image"] for img in preset_images],
|
165 |
+
label="Preset Images",
|
166 |
+
columns=5,
|
167 |
+
height=130,
|
168 |
+
allow_preview=False,
|
169 |
+
)
|
170 |
+
gallery.select(set_input_image, gallery, input_image)
|
171 |
+
|
172 |
+
with gr.Accordion("Advanced Parameters", open=False):
|
173 |
+
denoise: gr.Slider = gr.Slider(0, 1, value=0.4, label="Denoise")
|
174 |
+
steps: gr.Slider = gr.Slider(1, 40, value=10, step=1, label="Steps")
|
175 |
+
tile_size: gr.Slider = gr.Slider(
|
176 |
+
64, 2048, value=1024, step=8, label="Tile Size"
|
177 |
+
)
|
178 |
+
downscale: gr.Slider = gr.Slider(
|
179 |
+
1, 4, value=1, step=1, label="Downscale"
|
180 |
+
)
|
181 |
+
upscale: gr.Slider = gr.Slider(1, 4, value=4, step=0.1, label="Upscale")
|
182 |
+
color_match: gr.Slider = gr.Slider(0, 1, value=0, label="Color Match")
|
183 |
+
controlnet_tile_end: gr.Slider = gr.Slider(
|
184 |
+
0, 1, value=1, label="ControlNet Tile End"
|
185 |
+
)
|
186 |
+
controlnet_tile_strength: gr.Slider = gr.Slider(
|
187 |
+
0, 1, value=0.7, label="ControlNet Tile Strength"
|
188 |
+
)
|
189 |
+
|
190 |
+
with gr.Column():
|
191 |
+
image_slider = ImageSlider(
|
192 |
+
label="Compare Original and Processed",
|
193 |
+
type="pil",
|
194 |
+
value=lambda: global_image_slider,
|
195 |
+
interactive=True,
|
196 |
+
)
|
197 |
+
|
198 |
+
process_btn: gr.Button = gr.Button("Run")
|
199 |
+
process_btn.click(
|
200 |
+
fn=run,
|
201 |
+
inputs=[
|
202 |
+
denoise,
|
203 |
+
steps,
|
204 |
+
tile_size,
|
205 |
+
downscale,
|
206 |
+
upscale,
|
207 |
+
color_match,
|
208 |
+
controlnet_tile_end,
|
209 |
+
controlnet_tile_strength,
|
210 |
+
],
|
211 |
+
outputs=[image_slider],
|
212 |
+
)
|
213 |
+
|
214 |
+
def build_example(input_image_path):
|
215 |
+
output_image_path = input_image_path.replace("inputs", "outputs")
|
216 |
+
return [
|
217 |
+
input_image_path,
|
218 |
+
0.4, # denoise
|
219 |
+
10, # steps
|
220 |
+
1024, # tile_size
|
221 |
+
1, # downscale
|
222 |
+
4, # upscale
|
223 |
+
0, # color_match
|
224 |
+
1, # controlnet_tile_end
|
225 |
+
0.7, # controlnet_tile_strength
|
226 |
+
(input_image_path, output_image_path),
|
227 |
+
]
|
228 |
+
|
229 |
+
# Build examples
|
230 |
+
input_images = glob.glob("images/inputs/*")
|
231 |
+
examples = [build_example(img) for img in input_images]
|
232 |
+
|
233 |
+
# Update the gr.Examples call
|
234 |
+
gr.Examples(
|
235 |
+
examples=examples,
|
236 |
+
inputs=[
|
237 |
+
input_image,
|
238 |
+
denoise,
|
239 |
+
steps,
|
240 |
+
tile_size,
|
241 |
+
downscale,
|
242 |
+
upscale,
|
243 |
+
color_match,
|
244 |
+
controlnet_tile_end,
|
245 |
+
controlnet_tile_strength,
|
246 |
+
image_slider,
|
247 |
+
],
|
248 |
+
)
|
249 |
+
|
250 |
+
if __name__ == "__main__":
|
251 |
+
demo.launch(debug=True, share=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
comfydeploy
|
3 |
+
python-dotenv
|
4 |
+
gradio_imageslider
|