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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import spaces
|
| 4 |
+
import torch
|
| 5 |
+
import random
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
from diffusers import FluxKontextPipeline
|
| 9 |
+
from diffusers.utils import load_image
|
| 10 |
+
from huggingface_hub import hf_hub_download
|
| 11 |
+
from aura_sr import AuraSR
|
| 12 |
+
|
| 13 |
+
# --- Main Model Initialization ---
|
| 14 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 15 |
+
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 16 |
+
|
| 17 |
+
# --- Load All Adapters ---
|
| 18 |
+
pipe.load_lora_weights("prithivMLmods/PhotoCleanser-i2i", weight_name="PhotoCleanser-i2i.safetensors", adapter_name="cleanser")
|
| 19 |
+
pipe.load_lora_weights("prithivMLmods/Photo-Restore-i2i", weight_name="Photo-Restore-i2i.safetensors", adapter_name="restorer")
|
| 20 |
+
pipe.load_lora_weights("prithivMLmods/Polaroid-Warm-i2i", weight_name="Polaroid-Warm-i2i.safetensors", adapter_name="polaroid")
|
| 21 |
+
pipe.load_lora_weights("prithivMLmods/Monochrome-Pencil", weight_name="Monochrome-Pencil-i2i.safetensors", adapter_name="pencil")
|
| 22 |
+
|
| 23 |
+
# --- Upscaler Model Initialization ---
|
| 24 |
+
# FIX: Removed the .to("cuda") call. The library handles device placement automatically.
|
| 25 |
+
aura_sr = AuraSR.from_pretrained("fal/AuraSR-v2")
|
| 26 |
+
|
| 27 |
+
@spaces.GPU
|
| 28 |
+
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)):
|
| 29 |
+
"""
|
| 30 |
+
Perform image editing and optional upscaling.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
input_image (PIL.Image.Image): The input image to be edited.
|
| 34 |
+
prompt (str): Text description of the desired edit.
|
| 35 |
+
lora_adapter (str): The name of the LoRA adapter to use.
|
| 36 |
+
upscale_image (bool): If True, the final image will be upscaled 4x.
|
| 37 |
+
seed (int, optional): Random seed for reproducible generation.
|
| 38 |
+
randomize_seed (bool, optional): If True, generates a random seed.
|
| 39 |
+
guidance_scale (float, optional): Controls adherence to the prompt.
|
| 40 |
+
steps (int, optional): Number of diffusion steps.
|
| 41 |
+
progress (gr.Progress, optional): Gradio progress tracker.
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
tuple: A 3-tuple containing the generated/upscaled image, the seed used,
|
| 45 |
+
and a Gradio update to make the reuse button visible.
|
| 46 |
+
"""
|
| 47 |
+
if lora_adapter == "PhotoCleanser":
|
| 48 |
+
pipe.set_adapters(["cleanser"], adapter_weights=[1.0])
|
| 49 |
+
elif lora_adapter == "PhotoRestorer":
|
| 50 |
+
pipe.set_adapters(["restorer"], adapter_weights=[1.0])
|
| 51 |
+
elif lora_adapter == "PolaroidWarm":
|
| 52 |
+
pipe.set_adapters(["polaroid"], adapter_weights=[1.0])
|
| 53 |
+
elif lora_adapter == "MonochromePencil":
|
| 54 |
+
pipe.set_adapters(["pencil"], adapter_weights=[1.0])
|
| 55 |
+
|
| 56 |
+
if randomize_seed:
|
| 57 |
+
seed = random.randint(0, MAX_SEED)
|
| 58 |
+
|
| 59 |
+
if input_image:
|
| 60 |
+
input_image = input_image.convert("RGB")
|
| 61 |
+
image = pipe(
|
| 62 |
+
image=input_image,
|
| 63 |
+
prompt=prompt,
|
| 64 |
+
guidance_scale=guidance_scale,
|
| 65 |
+
width = input_image.size[0],
|
| 66 |
+
height = input_image.size[1],
|
| 67 |
+
num_inference_steps=steps,
|
| 68 |
+
generator=torch.Generator().manual_seed(seed),
|
| 69 |
+
).images[0]
|
| 70 |
+
else:
|
| 71 |
+
image = pipe(
|
| 72 |
+
prompt=prompt,
|
| 73 |
+
guidance_scale=guidance_scale,
|
| 74 |
+
num_inference_steps=steps,
|
| 75 |
+
generator=torch.Generator().manual_seed(seed),
|
| 76 |
+
).images[0]
|
| 77 |
+
|
| 78 |
+
# Conditionally upscale the generated image
|
| 79 |
+
if upscale_image:
|
| 80 |
+
progress(0.8, desc="Upscaling image...")
|
| 81 |
+
image = aura_sr.upscale_4x(image)
|
| 82 |
+
|
| 83 |
+
return image, seed, gr.Button(visible=True)
|
| 84 |
+
|
| 85 |
+
@spaces.GPU
|
| 86 |
+
def infer_example(input_image, prompt, lora_adapter):
|
| 87 |
+
"""
|
| 88 |
+
Wrapper function for gr.Examples. Upscaling is disabled for examples for speed.
|
| 89 |
+
"""
|
| 90 |
+
image, seed, _ = infer(input_image, prompt, lora_adapter, upscale_image=False)
|
| 91 |
+
return image, seed
|
| 92 |
+
|
| 93 |
+
css="""
|
| 94 |
+
#col-container {
|
| 95 |
+
margin: 0 auto;
|
| 96 |
+
max-width: 960px;
|
| 97 |
+
}
|
| 98 |
+
"""
|
| 99 |
+
|
| 100 |
+
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
| 101 |
+
|
| 102 |
+
with gr.Column(elem_id="col-container"):
|
| 103 |
+
gr.Markdown(f"""# **[Photo-Mate-i2i](https://huggingface.co/collections/prithivMLmods/i2i-kontext-exp-68ce573b5c0623476b636ec7)**
|
| 104 |
+
Image manipulation with Kontext adapters""")
|
| 105 |
+
with gr.Row():
|
| 106 |
+
with gr.Column():
|
| 107 |
+
input_image = gr.Image(label="Upload the image for editing", type="pil", height="300")
|
| 108 |
+
with gr.Row():
|
| 109 |
+
prompt = gr.Text(
|
| 110 |
+
label="Prompt",
|
| 111 |
+
show_label=False,
|
| 112 |
+
max_lines=1,
|
| 113 |
+
placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
|
| 114 |
+
container=False,
|
| 115 |
+
)
|
| 116 |
+
run_button = gr.Button("Run", scale=0)
|
| 117 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 118 |
+
|
| 119 |
+
upscale_checkbox = gr.Checkbox(label="Upscale the final image", value=False)
|
| 120 |
+
|
| 121 |
+
seed = gr.Slider(
|
| 122 |
+
label="Seed",
|
| 123 |
+
minimum=0,
|
| 124 |
+
maximum=MAX_SEED,
|
| 125 |
+
step=1,
|
| 126 |
+
value=0,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 130 |
+
|
| 131 |
+
guidance_scale = gr.Slider(
|
| 132 |
+
label="Guidance Scale",
|
| 133 |
+
minimum=1,
|
| 134 |
+
maximum=10,
|
| 135 |
+
step=0.1,
|
| 136 |
+
value=2.5,
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
steps = gr.Slider(
|
| 140 |
+
label="Steps",
|
| 141 |
+
minimum=1,
|
| 142 |
+
maximum=30,
|
| 143 |
+
value=28,
|
| 144 |
+
step=1
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
with gr.Column():
|
| 148 |
+
result = gr.Image(label="Result", show_label=False, interactive=False, format="png")
|
| 149 |
+
reuse_button = gr.Button("Reuse this image", visible=False)
|
| 150 |
+
with gr.Row():
|
| 151 |
+
lora_adapter = gr.Dropdown(
|
| 152 |
+
label="Choose Adapter",
|
| 153 |
+
choices=["PhotoCleanser", "PhotoRestorer", "PolaroidWarm", "MonochromePencil"],
|
| 154 |
+
value="PhotoCleanser"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
gr.Examples(
|
| 158 |
+
examples=[
|
| 159 |
+
["photocleanser/1.png", "[photo content], remove the embroidered pattern from the image while preserving the background and remaining elements, maintaining realism and original details.", "PhotoCleanser"],
|
| 160 |
+
["photocleanser/2.png", "[photo content], remove the cat from the image while preserving the background and remaining elements, maintaining realism and original details.", "PhotoCleanser"],
|
| 161 |
+
["photorestore/1.png", "[photo content], restore and enhance the image by repairing any damage, scratches, or fading. Colorize the photo naturally while preserving authentic textures and details, maintaining a realistic and historically accurate look.", "PhotoRestorer"],
|
| 162 |
+
["photorestore/2.png", "[photo content], restore and enhance the image by repairing any damage, scratches, or fading. Colorize the photo naturally while preserving authentic textures and details, maintaining a realistic and historically accurate look.", "PhotoRestorer"],
|
| 163 |
+
["polaroid/1.png", "[photo content], apply a warm, vintage Polaroid-style filter, enhancing the image with nostalgic tones, soft focus, and characteristic light leaks for an authentic, retro feel.", "PolaroidWarm"],
|
| 164 |
+
["polaroid/2.png", "[photo content], give the image a classic Polaroid look with warm, saturated colors, gentle fading, and a subtle vignette effect, evoking a sense of timeless memories.", "PolaroidWarm"],
|
| 165 |
+
["pencil/1.png", "[photo content], transform the image into a detailed monochrome pencil sketch, emphasizing sharp lines, textures, and shading for a classic hand-drawn look.", "MonochromePencil"],
|
| 166 |
+
["pencil/2.png", "[photo content], convert the photo into a realistic graphite pencil drawing, capturing the subject's form and depth with varied strokes and contrast.", "MonochromePencil"]
|
| 167 |
+
],
|
| 168 |
+
inputs=[input_image, prompt, lora_adapter],
|
| 169 |
+
outputs=[result, seed],
|
| 170 |
+
fn=infer_example,
|
| 171 |
+
cache_examples=False,
|
| 172 |
+
label="Examples (Image | Prompt | Selected LoRA)"
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
gr.on(
|
| 176 |
+
triggers=[run_button.click, prompt.submit],
|
| 177 |
+
fn=infer,
|
| 178 |
+
inputs=[input_image, prompt, lora_adapter, upscale_checkbox, seed, randomize_seed, guidance_scale, steps],
|
| 179 |
+
outputs=[result, seed, reuse_button]
|
| 180 |
+
)
|
| 181 |
+
reuse_button.click(
|
| 182 |
+
fn=lambda image: image,
|
| 183 |
+
inputs=[result],
|
| 184 |
+
outputs=[input_image]
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
demo.launch(mcp_server=True)
|