Spaces:
Paused
Paused
IronJayx
commited on
Commit
·
7d125a7
1
Parent(s):
9e17d8f
fixed inputs
Browse files- app.py +115 -77
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from gradio_imageslider import ImageSlider
|
| 4 |
from comfydeploy import ComfyDeploy
|
|
@@ -7,7 +8,6 @@ import requests
|
|
| 7 |
from io import BytesIO
|
| 8 |
import base64
|
| 9 |
import glob
|
| 10 |
-
import asyncio
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
|
| 13 |
load_dotenv()
|
|
@@ -29,9 +29,31 @@ def get_base64_from_image(image: Image.Image) -> str:
|
|
| 29 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 30 |
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
progress(0, desc="Preparing inputs...")
|
| 36 |
image_base64 = get_base64_from_image(image)
|
| 37 |
|
|
@@ -40,45 +62,46 @@ async def process_image(
|
|
| 40 |
**{k: str(v) for k, v in params.items()},
|
| 41 |
}
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
result = client.run.create(
|
| 45 |
request={"deployment_id": DEPLOYMENT_ID, "inputs": inputs}
|
| 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 |
except Exception as e:
|
| 72 |
print(f"Error: {e}")
|
| 73 |
-
return None
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
async def run_async(image: Image.Image, params: dict, progress: gr.Progress):
|
| 77 |
-
return await process_image(image, params, progress)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
def run(image: Image.Image, params: dict):
|
| 81 |
-
return asyncio.run(run_async(image, params, gr.Progress()))
|
| 82 |
|
| 83 |
|
| 84 |
def load_preset_images():
|
|
@@ -107,55 +130,70 @@ def build_example(input_image_path):
|
|
| 107 |
]
|
| 108 |
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
with gr.Blocks() as demo:
|
| 111 |
-
gr.
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
with gr.Column():
|
|
|
|
| 114 |
input_image = gr.Image(type="pil", label="Input Image", interactive=True)
|
| 115 |
-
params = {
|
| 116 |
-
"denoise": gr.Slider(0, 1, value=0.4, label="Denoise"),
|
| 117 |
-
"steps": gr.Slider(1, 25, value=10, label="Steps"),
|
| 118 |
-
"tile_size": gr.Slider(256, 2048, value=1024, label="Tile Size"),
|
| 119 |
-
"downscale": gr.Slider(1, 4, value=1, label="Downscale"),
|
| 120 |
-
"upscale": gr.Slider(1, 4, value=4, label="Upscale"),
|
| 121 |
-
"color_match": gr.Slider(0, 1, value=0, label="Color Match"),
|
| 122 |
-
"controlnet_tile_end": gr.Slider(
|
| 123 |
-
0, 1, value=1, label="ControlNet Tile End"
|
| 124 |
-
),
|
| 125 |
-
"controlnet_tile_strength": gr.Slider(
|
| 126 |
-
0, 1, value=0.7, label="ControlNet Tile Strength"
|
| 127 |
-
),
|
| 128 |
-
}
|
| 129 |
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
image_slider = ImageSlider(
|
| 132 |
-
label="Compare Original and Processed",
|
| 133 |
)
|
| 134 |
|
|
|
|
|
|
|
|
|
|
| 135 |
process_btn.click(
|
| 136 |
-
fn=lambda
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
img,
|
| 146 |
-
{
|
| 147 |
-
"denoise": denoise,
|
| 148 |
-
"steps": steps,
|
| 149 |
-
"tile_size": tile_size,
|
| 150 |
-
"downscale": downscale,
|
| 151 |
-
"upscale": upscale,
|
| 152 |
-
"color_match": color_match,
|
| 153 |
-
"controlnet_tile_end": controlnet_tile_end,
|
| 154 |
-
"controlnet_tile_strength": controlnet_tile_strength,
|
| 155 |
-
},
|
| 156 |
-
),
|
| 157 |
-
inputs=[input_image, *params.values()],
|
| 158 |
outputs=[image_slider],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
)
|
| 160 |
|
| 161 |
examples = [build_example(img) for img in glob.glob("images/inputs/*")]
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import gradio as gr
|
| 4 |
from gradio_imageslider import ImageSlider
|
| 5 |
from comfydeploy import ComfyDeploy
|
|
|
|
| 8 |
from io import BytesIO
|
| 9 |
import base64
|
| 10 |
import glob
|
|
|
|
| 11 |
from dotenv import load_dotenv
|
| 12 |
|
| 13 |
load_dotenv()
|
|
|
|
| 29 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 30 |
|
| 31 |
|
| 32 |
+
def get_profile(profile) -> dict:
|
| 33 |
+
return {
|
| 34 |
+
"username": profile.username,
|
| 35 |
+
"profile": profile.profile,
|
| 36 |
+
"name": profile.name,
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
async def process(
|
| 41 |
+
image: Image.Image | None = None,
|
| 42 |
+
profile: gr.OAuthProfile | None = None,
|
| 43 |
+
progress: gr.Progress = gr.Progress(),
|
| 44 |
+
) -> tuple[Image.Image, Image.Image] | None:
|
| 45 |
+
if not image:
|
| 46 |
+
gr.Info("Please upload an image ")
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
if profile is None:
|
| 50 |
+
gr.Info("Please log in to process the image.")
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
+
user_data = get_profile(profile)
|
| 54 |
+
print("--------- RUN ----------")
|
| 55 |
+
print(user_data)
|
| 56 |
+
|
| 57 |
progress(0, desc="Preparing inputs...")
|
| 58 |
image_base64 = get_base64_from_image(image)
|
| 59 |
|
|
|
|
| 62 |
**{k: str(v) for k, v in params.items()},
|
| 63 |
}
|
| 64 |
|
| 65 |
+
output = await process_image(inputs, progress)
|
| 66 |
+
|
| 67 |
+
progress(100, desc="Processing completed")
|
| 68 |
+
|
| 69 |
+
return image, output
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
async def process_image(inputs: dict, progress: gr.Progress) -> Image.Image | None:
|
| 73 |
try:
|
| 74 |
result = client.run.create(
|
| 75 |
request={"deployment_id": DEPLOYMENT_ID, "inputs": inputs}
|
| 76 |
)
|
| 77 |
+
|
| 78 |
+
if result and result.object:
|
| 79 |
+
run_id: str = result.object.run_id
|
| 80 |
+
progress(0, desc="Starting processing...")
|
| 81 |
+
while True:
|
| 82 |
+
run_result = client.run.get(run_id=run_id)
|
| 83 |
+
if not run_result.object:
|
| 84 |
+
continue
|
| 85 |
+
|
| 86 |
+
progress_value = run_result.object.progress or 0
|
| 87 |
+
status = run_result.object.live_status or "Cold starting..."
|
| 88 |
+
progress(progress_value, desc=f"Status: {status}")
|
| 89 |
+
|
| 90 |
+
if run_result.object.status == "success":
|
| 91 |
+
for output in run_result.object.outputs or []:
|
| 92 |
+
if output.data and output.data.images:
|
| 93 |
+
image_url: str = output.data.images[0].url
|
| 94 |
+
response = requests.get(image_url)
|
| 95 |
+
processed_image = Image.open(BytesIO(response.content))
|
| 96 |
+
return processed_image
|
| 97 |
+
elif run_result.object.status == "failed":
|
| 98 |
+
print("Processing failed")
|
| 99 |
+
return None
|
| 100 |
+
|
| 101 |
+
time.sleep(1) # Wait for 1 second before checking the status again
|
| 102 |
except Exception as e:
|
| 103 |
print(f"Error: {e}")
|
| 104 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
def load_preset_images():
|
|
|
|
| 130 |
]
|
| 131 |
|
| 132 |
|
| 133 |
+
def serialize_params(params: dict) -> dict:
|
| 134 |
+
return {
|
| 135 |
+
key: {"value": param.value, "label": param.label}
|
| 136 |
+
for key, param in params.items()
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
|
| 140 |
with gr.Blocks() as demo:
|
| 141 |
+
gr.HTML("""
|
| 142 |
+
<div style="display: flex; justify-content: center; text-align:center; flex-direction: column;">
|
| 143 |
+
<h1 style="color: #333;">🌊 Creative Image Upscaler</h1>
|
| 144 |
+
<div style="max-width: 800px; margin: 0 auto;">
|
| 145 |
+
<p style="font-size: 16px;">Upload an image and adjust the parameters to enhance your image.</p>
|
| 146 |
+
<p style="font-size: 16px;">Click on the <b>"Run"</b> button to process the image and compare the original and processed images using the slider.</p>
|
| 147 |
+
<p style="font-size: 16px;">⚠️ Note that the images are compressed to reduce the workloads of the demo.</p>
|
| 148 |
+
</div>
|
| 149 |
+
</div>
|
| 150 |
+
""")
|
| 151 |
+
with gr.Row(equal_height=False):
|
| 152 |
with gr.Column():
|
| 153 |
+
# The image overflow, fix
|
| 154 |
input_image = gr.Image(type="pil", label="Input Image", interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
+
with gr.Accordion("Avanced parameters", open=False):
|
| 157 |
+
params = {
|
| 158 |
+
"denoise": gr.Slider(0, 1, value=0.4, label="Denoise"),
|
| 159 |
+
"steps": gr.Slider(1, 25, value=10, label="Steps"),
|
| 160 |
+
"tile_size": gr.Slider(256, 2048, value=1024, label="Tile Size"),
|
| 161 |
+
"downscale": gr.Slider(1, 4, value=1, label="Downscale"),
|
| 162 |
+
"upscale": gr.Slider(1, 4, value=4, label="Upscale"),
|
| 163 |
+
"color_match": gr.Slider(0, 1, value=0, label="Color Match"),
|
| 164 |
+
"controlnet_tile_end": gr.Slider(
|
| 165 |
+
0, 1, value=1, label="ControlNet Tile End"
|
| 166 |
+
),
|
| 167 |
+
"controlnet_tile_strength": gr.Slider(
|
| 168 |
+
0, 1, value=0.7, label="ControlNet Tile Strength"
|
| 169 |
+
),
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
with gr.Column():
|
| 173 |
image_slider = ImageSlider(
|
| 174 |
+
label="Compare Original and Processed", interactive=False
|
| 175 |
)
|
| 176 |
|
| 177 |
+
login_button = gr.LoginButton(scale=8)
|
| 178 |
+
process_btn = gr.Button("Run", variant="primary", size="lg")
|
| 179 |
+
|
| 180 |
process_btn.click(
|
| 181 |
+
fn=lambda _: gr.update(interactive=False, value="Processing..."),
|
| 182 |
+
inputs=[],
|
| 183 |
+
outputs=[process_btn],
|
| 184 |
+
api_name=False,
|
| 185 |
+
).then(
|
| 186 |
+
fn=process,
|
| 187 |
+
inputs=[
|
| 188 |
+
input_image,
|
| 189 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
outputs=[image_slider],
|
| 191 |
+
api_name=False,
|
| 192 |
+
).then(
|
| 193 |
+
fn=lambda _: gr.update(interactive=True, value="Run"),
|
| 194 |
+
inputs=[],
|
| 195 |
+
outputs=[process_btn],
|
| 196 |
+
api_name=False,
|
| 197 |
)
|
| 198 |
|
| 199 |
examples = [build_example(img) for img in glob.glob("images/inputs/*")]
|
requirements.txt
CHANGED
|
@@ -2,3 +2,4 @@ gradio
|
|
| 2 |
comfydeploy
|
| 3 |
python-dotenv
|
| 4 |
gradio_imageslider
|
|
|
|
|
|
| 2 |
comfydeploy
|
| 3 |
python-dotenv
|
| 4 |
gradio_imageslider
|
| 5 |
+
gradio[oauth]
|