Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,9 +10,22 @@ from typing import Iterable
|
|
| 10 |
from diffusers import FluxKontextPipeline
|
| 11 |
from diffusers.utils import load_image
|
| 12 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 13 |
|
| 14 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
from gradio.themes import Soft
|
| 17 |
from gradio.themes.utils import colors, fonts, sizes
|
| 18 |
|
|
@@ -84,9 +97,11 @@ class OrangeRedTheme(Soft):
|
|
| 84 |
|
| 85 |
orange_red_theme = OrangeRedTheme()
|
| 86 |
|
|
|
|
| 87 |
MAX_SEED = np.iinfo(np.int32).max
|
| 88 |
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 89 |
|
|
|
|
| 90 |
pipe.load_lora_weights("prithivMLmods/PhotoCleanser-i2i", weight_name="PhotoCleanser-i2i.safetensors", adapter_name="cleanser")
|
| 91 |
pipe.load_lora_weights("prithivMLmods/Photo-Restore-i2i", weight_name="Photo-Restore-i2i.safetensors", adapter_name="restorer")
|
| 92 |
pipe.load_lora_weights("prithivMLmods/Polaroid-Warm-i2i", weight_name="Polaroid-Warm-i2i.safetensors", adapter_name="polaroid")
|
|
@@ -95,8 +110,14 @@ pipe.load_lora_weights("prithivMLmods/LZO-1-Preview", weight_name="LZO-1-Preview
|
|
| 95 |
pipe.load_lora_weights("prithivMLmods/Kontext-Watermark-Remover", weight_name="Kontext-Watermark-Remover.safetensors", adapter_name="watermark-remover")
|
| 96 |
pipe.load_lora_weights("prithivMLmods/Kontext-Unblur-Upscale", weight_name="Kontext-Image-Upscale.safetensors", adapter_name="unblur-upscale")
|
| 97 |
|
|
|
|
|
|
|
|
|
|
| 98 |
@spaces.GPU
|
| 99 |
-
def infer(input_image, prompt, lora_adapter, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
|
|
|
|
|
|
| 100 |
if not input_image:
|
| 101 |
raise gr.Error("Please upload an image for editing.")
|
| 102 |
|
|
@@ -130,11 +151,18 @@ def infer(input_image, prompt, lora_adapter, seed=42, randomize_seed=False, guid
|
|
| 130 |
generator=torch.Generator().manual_seed(seed),
|
| 131 |
).images[0]
|
| 132 |
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
@spaces.GPU
|
| 136 |
def infer_example(input_image, prompt, lora_adapter):
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
| 138 |
return image, seed
|
| 139 |
|
| 140 |
css="""
|
|
@@ -192,7 +220,8 @@ with gr.Blocks() as demo:
|
|
| 192 |
)
|
| 193 |
|
| 194 |
with gr.Column():
|
| 195 |
-
output_image = gr.Image(label="Output Image", interactive=False, format="png", height=
|
|
|
|
| 196 |
|
| 197 |
with gr.Row():
|
| 198 |
lora_adapter = gr.Dropdown(
|
|
@@ -200,6 +229,9 @@ with gr.Blocks() as demo:
|
|
| 200 |
choices=["PhotoCleanser", "PhotoRestorer", "PolaroidWarm", "MonochromePencil", "LZO-Zoom", "Kontext-Watermark-Remover", "Kontext-Unblur-Upscale"],
|
| 201 |
value="PhotoCleanser"
|
| 202 |
)
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
gr.Examples(
|
| 205 |
examples=[
|
|
@@ -224,8 +256,14 @@ with gr.Blocks() as demo:
|
|
| 224 |
gr.on(
|
| 225 |
triggers=[run_button.click, prompt.submit],
|
| 226 |
fn=infer,
|
| 227 |
-
inputs=[input_image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
|
| 228 |
-
outputs=[output_image, seed]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
)
|
| 230 |
|
| 231 |
demo.launch(css=css, theme=orange_red_theme, mcp_server=True, ssr_mode=False, show_error=True)
|
|
|
|
| 10 |
from diffusers import FluxKontextPipeline
|
| 11 |
from diffusers.utils import load_image
|
| 12 |
from huggingface_hub import hf_hub_download
|
| 13 |
+
from aura_sr import AuraSR
|
| 14 |
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
|
| 17 |
+
# --- # Device and CUDA Setup Check ---
|
| 18 |
+
print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
|
| 19 |
+
print("torch.__version__ =", torch.__version__)
|
| 20 |
+
print("torch.version.cuda =", torch.version.cuda)
|
| 21 |
+
print("cuda available:", torch.cuda.is_available())
|
| 22 |
+
print("cuda device count:", torch.cuda.device_count())
|
| 23 |
+
if torch.cuda.is_available():
|
| 24 |
+
print("current device:", torch.cuda.current_device())
|
| 25 |
+
print("device name:", torch.cuda.get_device_name(torch.cuda.current_device()))
|
| 26 |
+
|
| 27 |
+
print("Using device:", device)
|
| 28 |
+
|
| 29 |
from gradio.themes import Soft
|
| 30 |
from gradio.themes.utils import colors, fonts, sizes
|
| 31 |
|
|
|
|
| 97 |
|
| 98 |
orange_red_theme = OrangeRedTheme()
|
| 99 |
|
| 100 |
+
# --- Main Model Initialization ---
|
| 101 |
MAX_SEED = np.iinfo(np.int32).max
|
| 102 |
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 103 |
|
| 104 |
+
# --- Load All Adapters ---
|
| 105 |
pipe.load_lora_weights("prithivMLmods/PhotoCleanser-i2i", weight_name="PhotoCleanser-i2i.safetensors", adapter_name="cleanser")
|
| 106 |
pipe.load_lora_weights("prithivMLmods/Photo-Restore-i2i", weight_name="Photo-Restore-i2i.safetensors", adapter_name="restorer")
|
| 107 |
pipe.load_lora_weights("prithivMLmods/Polaroid-Warm-i2i", weight_name="Polaroid-Warm-i2i.safetensors", adapter_name="polaroid")
|
|
|
|
| 110 |
pipe.load_lora_weights("prithivMLmods/Kontext-Watermark-Remover", weight_name="Kontext-Watermark-Remover.safetensors", adapter_name="watermark-remover")
|
| 111 |
pipe.load_lora_weights("prithivMLmods/Kontext-Unblur-Upscale", weight_name="Kontext-Image-Upscale.safetensors", adapter_name="unblur-upscale")
|
| 112 |
|
| 113 |
+
# --- Upscaler Model Initialization ---
|
| 114 |
+
aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
|
| 115 |
+
|
| 116 |
@spaces.GPU
|
| 117 |
+
def infer(input_image, prompt, lora_adapter, upscale_image, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
|
| 118 |
+
"""
|
| 119 |
+
Perform image editing and optional upscaling, returning the final image.
|
| 120 |
+
"""
|
| 121 |
if not input_image:
|
| 122 |
raise gr.Error("Please upload an image for editing.")
|
| 123 |
|
|
|
|
| 151 |
generator=torch.Generator().manual_seed(seed),
|
| 152 |
).images[0]
|
| 153 |
|
| 154 |
+
if upscale_image:
|
| 155 |
+
progress(0.8, desc="Upscaling image...")
|
| 156 |
+
image = aura_sr.upscale_4x(image)
|
| 157 |
+
|
| 158 |
+
return image, seed, gr.Button(visible=True)
|
| 159 |
|
| 160 |
@spaces.GPU
|
| 161 |
def infer_example(input_image, prompt, lora_adapter):
|
| 162 |
+
"""
|
| 163 |
+
Wrapper function for gr.Examples.
|
| 164 |
+
"""
|
| 165 |
+
image, seed, _ = infer(input_image, prompt, lora_adapter, upscale_image=False)
|
| 166 |
return image, seed
|
| 167 |
|
| 168 |
css="""
|
|
|
|
| 220 |
)
|
| 221 |
|
| 222 |
with gr.Column():
|
| 223 |
+
output_image = gr.Image(label="Output Image", interactive=False, format="png", height=355)
|
| 224 |
+
reuse_button = gr.Button("Reuse this image", visible=False)
|
| 225 |
|
| 226 |
with gr.Row():
|
| 227 |
lora_adapter = gr.Dropdown(
|
|
|
|
| 229 |
choices=["PhotoCleanser", "PhotoRestorer", "PolaroidWarm", "MonochromePencil", "LZO-Zoom", "Kontext-Watermark-Remover", "Kontext-Unblur-Upscale"],
|
| 230 |
value="PhotoCleanser"
|
| 231 |
)
|
| 232 |
+
|
| 233 |
+
with gr.Row():
|
| 234 |
+
upscale_checkbox = gr.Checkbox(label="Upscale the final image", value=False)
|
| 235 |
|
| 236 |
gr.Examples(
|
| 237 |
examples=[
|
|
|
|
| 256 |
gr.on(
|
| 257 |
triggers=[run_button.click, prompt.submit],
|
| 258 |
fn=infer,
|
| 259 |
+
inputs=[input_image, prompt, lora_adapter, upscale_checkbox, seed, randomize_seed, guidance_scale, steps],
|
| 260 |
+
outputs=[output_image, seed, reuse_button]
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
reuse_button.click(
|
| 264 |
+
fn=lambda x: x,
|
| 265 |
+
inputs=[output_image],
|
| 266 |
+
outputs=[input_image]
|
| 267 |
)
|
| 268 |
|
| 269 |
demo.launch(css=css, theme=orange_red_theme, mcp_server=True, ssr_mode=False, show_error=True)
|