Spaces:
Running
on
Zero
Running
on
Zero
Update Gradio app with multiple files
Browse files- app.py +15 -79
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -49,75 +49,38 @@ optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB",
|
|
| 49 |
|
| 50 |
MAX_SEED = np.iinfo(np.int32).max
|
| 51 |
|
| 52 |
-
def build_light_restoration_prompt(
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Brightness adjustment
|
| 56 |
-
if brightness > 0:
|
| 57 |
-
prompt_parts.append(f"Increase brightness by {brightness}%")
|
| 58 |
-
elif brightness < 0:
|
| 59 |
-
prompt_parts.append(f"Decrease brightness by {abs(brightness)}%")
|
| 60 |
-
|
| 61 |
-
# Contrast adjustment
|
| 62 |
-
if contrast > 0:
|
| 63 |
-
prompt_parts.append(f"Increase contrast by {contrast}%")
|
| 64 |
-
elif contrast < 0:
|
| 65 |
-
prompt_parts.append(f"Decrease contrast by {abs(contrast)}%")
|
| 66 |
-
|
| 67 |
-
# Exposure adjustment
|
| 68 |
-
if exposure > 0:
|
| 69 |
-
prompt_parts.append(f"Increase exposure by {exposure}%")
|
| 70 |
-
elif exposure < 0:
|
| 71 |
-
prompt_parts.append(f"Decrease exposure by {abs(exposure)}%")
|
| 72 |
-
|
| 73 |
-
# Saturation adjustment
|
| 74 |
-
if saturation > 0:
|
| 75 |
-
prompt_parts.append(f"Increase saturation by {saturation}%")
|
| 76 |
-
elif saturation < 0:
|
| 77 |
-
prompt_parts.append(f"Decrease saturation by {abs(saturation)}%")
|
| 78 |
-
|
| 79 |
-
final_prompt = ", ".join(prompt_parts).strip()
|
| 80 |
-
return final_prompt if final_prompt else "Restore image lighting"
|
| 81 |
|
| 82 |
|
| 83 |
@spaces.GPU
|
| 84 |
def infer_light_restoration(
|
| 85 |
image,
|
| 86 |
-
brightness,
|
| 87 |
-
contrast,
|
| 88 |
-
exposure,
|
| 89 |
-
saturation,
|
| 90 |
seed,
|
| 91 |
randomize_seed,
|
| 92 |
true_guidance_scale,
|
| 93 |
num_inference_steps,
|
| 94 |
height,
|
| 95 |
width,
|
| 96 |
-
prev_output = None,
|
| 97 |
progress=gr.Progress(track_tqdm=True)
|
| 98 |
):
|
| 99 |
-
prompt = build_light_restoration_prompt(
|
| 100 |
print(f"Generated Prompt: {prompt}")
|
| 101 |
|
| 102 |
if randomize_seed:
|
| 103 |
seed = random.randint(0, MAX_SEED)
|
| 104 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 105 |
|
| 106 |
-
# Choose input image
|
| 107 |
pil_images = []
|
| 108 |
if image is not None:
|
| 109 |
if isinstance(image, Image.Image):
|
| 110 |
pil_images.append(image.convert("RGB"))
|
| 111 |
elif hasattr(image, "name"):
|
| 112 |
pil_images.append(Image.open(image.name).convert("RGB"))
|
| 113 |
-
elif prev_output:
|
| 114 |
-
pil_images.append(prev_output.convert("RGB"))
|
| 115 |
|
| 116 |
if len(pil_images) == 0:
|
| 117 |
raise gr.Error("Please upload an image first.")
|
| 118 |
-
|
| 119 |
-
if prompt == "Restore image lighting":
|
| 120 |
-
return image, seed, prompt
|
| 121 |
|
| 122 |
result = pipe(
|
| 123 |
image=pil_images,
|
|
@@ -162,8 +125,7 @@ css = '''
|
|
| 162 |
}
|
| 163 |
'''
|
| 164 |
|
| 165 |
-
|
| 166 |
-
return [0, 0, 0, 0]
|
| 167 |
|
| 168 |
def update_dimensions_on_upload(image):
|
| 169 |
if image is None:
|
|
@@ -189,9 +151,9 @@ def update_dimensions_on_upload(image):
|
|
| 189 |
|
| 190 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 191 |
with gr.Column(elem_id="col-container"):
|
| 192 |
-
gr.Markdown("# ✨
|
| 193 |
gr.Markdown("""
|
| 194 |
-
|
| 195 |
Using [dx8152's Light Restoration LoRA](https://huggingface.co/dx8152/Qwen-Image-Edit-2509-Light_restoration)
|
| 196 |
and [Phr00t/Qwen-Image-Edit-Rapid-AIO](https://huggingface.co/Phr00t/Qwen-Image-Edit-Rapid-AIO) for fast inference 💨<br>
|
| 197 |
Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
|
@@ -200,18 +162,9 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 200 |
|
| 201 |
with gr.Row():
|
| 202 |
with gr.Column(scale=1):
|
| 203 |
-
image = gr.Image(label="📸
|
| 204 |
-
prev_output = gr.Image(value=None, visible=False)
|
| 205 |
-
|
| 206 |
-
gr.Markdown("### 🎨 Light Adjustments")
|
| 207 |
-
brightness = gr.Slider(label="☀️ Brightness", minimum=-50, maximum=50, step=5, value=0)
|
| 208 |
-
contrast = gr.Slider(label="🌓 Contrast", minimum=-50, maximum=50, step=5, value=0)
|
| 209 |
-
exposure = gr.Slider(label="💡 Exposure", minimum=-50, maximum=50, step=5, value=0)
|
| 210 |
-
saturation = gr.Slider(label="🎨 Saturation", minimum=-50, maximum=50, step=5, value=0)
|
| 211 |
|
| 212 |
-
|
| 213 |
-
reset_btn = gr.Button("🔄 Reset", size="lg")
|
| 214 |
-
run_btn = gr.Button("✨ Restore", variant="primary", size="lg")
|
| 215 |
|
| 216 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 217 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
|
@@ -222,44 +175,27 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 222 |
width = gr.Slider(label="Width", minimum=256, maximum=2048, step=8, value=1024)
|
| 223 |
|
| 224 |
with gr.Column(scale=1):
|
| 225 |
-
result = gr.Image(label="✨
|
| 226 |
-
prompt_preview = gr.Textbox(label="
|
| 227 |
|
| 228 |
inputs = [
|
| 229 |
-
image,
|
| 230 |
-
seed, randomize_seed, true_guidance_scale, num_inference_steps, height, width
|
| 231 |
]
|
| 232 |
outputs = [result, seed, prompt_preview]
|
| 233 |
|
| 234 |
-
# Reset behavior
|
| 235 |
-
reset_btn.click(
|
| 236 |
-
fn=reset_all,
|
| 237 |
-
inputs=None,
|
| 238 |
-
outputs=[brightness, contrast, exposure, saturation],
|
| 239 |
-
queue=False
|
| 240 |
-
)
|
| 241 |
-
|
| 242 |
# Manual generation
|
| 243 |
run_btn.click(
|
| 244 |
fn=infer_light_restoration,
|
| 245 |
inputs=inputs,
|
| 246 |
outputs=outputs
|
| 247 |
-
)
|
| 248 |
|
| 249 |
-
# Image upload triggers dimension update
|
| 250 |
image.upload(
|
| 251 |
fn=update_dimensions_on_upload,
|
| 252 |
inputs=[image],
|
| 253 |
outputs=[width, height]
|
| 254 |
-
).then(
|
| 255 |
-
fn=reset_all,
|
| 256 |
-
inputs=None,
|
| 257 |
-
outputs=[brightness, contrast, exposure, saturation],
|
| 258 |
-
queue=False
|
| 259 |
)
|
| 260 |
|
| 261 |
-
# Live updates
|
| 262 |
-
for control in [brightness, contrast, exposure, saturation]:
|
| 263 |
-
control.release(fn=infer_light_restoration, inputs=inputs, outputs=outputs)
|
| 264 |
-
|
| 265 |
demo.launch()
|
|
|
|
| 49 |
|
| 50 |
MAX_SEED = np.iinfo(np.int32).max
|
| 51 |
|
| 52 |
+
def build_light_restoration_prompt():
|
| 53 |
+
return "Remove the shadows and use soft lighting to relight the image"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
@spaces.GPU
|
| 57 |
def infer_light_restoration(
|
| 58 |
image,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
seed,
|
| 60 |
randomize_seed,
|
| 61 |
true_guidance_scale,
|
| 62 |
num_inference_steps,
|
| 63 |
height,
|
| 64 |
width,
|
|
|
|
| 65 |
progress=gr.Progress(track_tqdm=True)
|
| 66 |
):
|
| 67 |
+
prompt = build_light_restoration_prompt()
|
| 68 |
print(f"Generated Prompt: {prompt}")
|
| 69 |
|
| 70 |
if randomize_seed:
|
| 71 |
seed = random.randint(0, MAX_SEED)
|
| 72 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 73 |
|
| 74 |
+
# Choose input image
|
| 75 |
pil_images = []
|
| 76 |
if image is not None:
|
| 77 |
if isinstance(image, Image.Image):
|
| 78 |
pil_images.append(image.convert("RGB"))
|
| 79 |
elif hasattr(image, "name"):
|
| 80 |
pil_images.append(Image.open(image.name).convert("RGB"))
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if len(pil_images) == 0:
|
| 83 |
raise gr.Error("Please upload an image first.")
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
result = pipe(
|
| 86 |
image=pil_images,
|
|
|
|
| 125 |
}
|
| 126 |
'''
|
| 127 |
|
| 128 |
+
|
|
|
|
| 129 |
|
| 130 |
def update_dimensions_on_upload(image):
|
| 131 |
if image is None:
|
|
|
|
| 151 |
|
| 152 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 153 |
with gr.Column(elem_id="col-container"):
|
| 154 |
+
gr.Markdown("# ✨ Shadow Removal & Relighting")
|
| 155 |
gr.Markdown("""
|
| 156 |
+
Remove shadows and apply soft lighting to your images<br>
|
| 157 |
Using [dx8152's Light Restoration LoRA](https://huggingface.co/dx8152/Qwen-Image-Edit-2509-Light_restoration)
|
| 158 |
and [Phr00t/Qwen-Image-Edit-Rapid-AIO](https://huggingface.co/Phr00t/Qwen-Image-Edit-Rapid-AIO) for fast inference 💨<br>
|
| 159 |
Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
|
|
|
| 162 |
|
| 163 |
with gr.Row():
|
| 164 |
with gr.Column(scale=1):
|
| 165 |
+
image = gr.Image(label="📸 Input Image", type="pil", height=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
+
run_btn = gr.Button("✨ Remove Shadows & Relight", variant="primary", size="lg")
|
|
|
|
|
|
|
| 168 |
|
| 169 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 170 |
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
|
|
|
| 175 |
width = gr.Slider(label="Width", minimum=256, maximum=2048, step=8, value=1024)
|
| 176 |
|
| 177 |
with gr.Column(scale=1):
|
| 178 |
+
result = gr.Image(label="✨ Output Image", interactive=False, height=500)
|
| 179 |
+
prompt_preview = gr.Textbox(label="Prompt Used", interactive=False, value="Remove the shadows and use soft lighting to relight the image")
|
| 180 |
|
| 181 |
inputs = [
|
| 182 |
+
image,
|
| 183 |
+
seed, randomize_seed, true_guidance_scale, num_inference_steps, height, width
|
| 184 |
]
|
| 185 |
outputs = [result, seed, prompt_preview]
|
| 186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
# Manual generation
|
| 188 |
run_btn.click(
|
| 189 |
fn=infer_light_restoration,
|
| 190 |
inputs=inputs,
|
| 191 |
outputs=outputs
|
| 192 |
+
)
|
| 193 |
|
| 194 |
+
# Image upload triggers dimension update
|
| 195 |
image.upload(
|
| 196 |
fn=update_dimensions_on_upload,
|
| 197 |
inputs=[image],
|
| 198 |
outputs=[width, height]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
)
|
| 200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -7,4 +7,4 @@ dashscope
|
|
| 7 |
kernels
|
| 8 |
torchvision
|
| 9 |
peft
|
| 10 |
-
torchao==0.11.0
|
|
|
|
| 7 |
kernels
|
| 8 |
torchvision
|
| 9 |
peft
|
| 10 |
+
torchao==0.11.0
|