Spaces:
Running
on
Zero
Running
on
Zero
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,213 +0,0 @@
|
|
| 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 |
-
|
| 12 |
-
MAX_SEED = np.iinfo(np.int32).max
|
| 13 |
-
|
| 14 |
-
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
|
| 15 |
-
|
| 16 |
-
# Add all four adapters to the app with unique names
|
| 17 |
-
pipe.load_lora_weights("prithivMLmods/PhotoCleanser-i2i", weight_name="PhotoCleanser-i2i.safetensors", adapter_name="cleanser")
|
| 18 |
-
pipe.load_lora_weights("prithivMLmods/Photo-Restore-i2i", weight_name="Photo-Restore-i2i.safetensors", adapter_name="restorer")
|
| 19 |
-
pipe.load_lora_weights("prithivMLmods/Polaroid-Warm-i2i", weight_name="Polaroid-Warm-i2i.safetensors", adapter_name="polaroid")
|
| 20 |
-
pipe.load_lora_weights("prithivMLmods/Monochrome-Pencil", weight_name="Monochrome-Pencil-i2i.safetensors", adapter_name="pencil")
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
@spaces.GPU
|
| 24 |
-
def infer(input_image, prompt, lora_adapter, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
|
| 25 |
-
"""
|
| 26 |
-
Perform image editing using the FLUX.1 Kontext pipeline with a selected adapter.
|
| 27 |
-
|
| 28 |
-
This function takes an input image, a text prompt, and a LoRA adapter choice to
|
| 29 |
-
generate a modified version of the image.
|
| 30 |
-
|
| 31 |
-
Args:
|
| 32 |
-
input_image (PIL.Image.Image): The input image to be edited.
|
| 33 |
-
prompt (str): Text description of the desired edit.
|
| 34 |
-
lora_adapter (str): The name of the LoRA adapter to use.
|
| 35 |
-
seed (int, optional): Random seed for reproducible generation. Defaults to 42.
|
| 36 |
-
randomize_seed (bool, optional): If True, generates a random seed. Defaults to False.
|
| 37 |
-
guidance_scale (float, optional): Controls adherence to the prompt. Defaults to 2.5.
|
| 38 |
-
steps (int, optional): Number of diffusion steps. Defaults to 28.
|
| 39 |
-
progress (gr.Progress, optional): Gradio progress tracker.
|
| 40 |
-
|
| 41 |
-
Returns:
|
| 42 |
-
tuple: A 3-tuple containing the generated image, the seed used, and a Gradio
|
| 43 |
-
update to make the reuse button visible.
|
| 44 |
-
"""
|
| 45 |
-
if lora_adapter == "PhotoCleanser":
|
| 46 |
-
pipe.set_adapters(["cleanser"], adapter_weights=[1.0])
|
| 47 |
-
elif lora_adapter == "PhotoRestorer":
|
| 48 |
-
pipe.set_adapters(["restorer"], adapter_weights=[1.0])
|
| 49 |
-
elif lora_adapter == "PolaroidWarm":
|
| 50 |
-
pipe.set_adapters(["polaroid"], adapter_weights=[1.0])
|
| 51 |
-
elif lora_adapter == "MonochromePencil":
|
| 52 |
-
pipe.set_adapters(["pencil"], adapter_weights=[1.0])
|
| 53 |
-
|
| 54 |
-
if randomize_seed:
|
| 55 |
-
seed = random.randint(0, MAX_SEED)
|
| 56 |
-
|
| 57 |
-
if input_image:
|
| 58 |
-
input_image = input_image.convert("RGB")
|
| 59 |
-
image = pipe(
|
| 60 |
-
image=input_image,
|
| 61 |
-
prompt=prompt,
|
| 62 |
-
guidance_scale=guidance_scale,
|
| 63 |
-
width = input_image.size[0],
|
| 64 |
-
height = input_image.size[1],
|
| 65 |
-
num_inference_steps=steps,
|
| 66 |
-
generator=torch.Generator().manual_seed(seed),
|
| 67 |
-
).images[0]
|
| 68 |
-
else:
|
| 69 |
-
image = pipe(
|
| 70 |
-
prompt=prompt,
|
| 71 |
-
guidance_scale=guidance_scale,
|
| 72 |
-
num_inference_steps=steps,
|
| 73 |
-
generator=torch.Generator().manual_seed(seed),
|
| 74 |
-
).images[0]
|
| 75 |
-
return image, seed, gr.Button(visible=True)
|
| 76 |
-
|
| 77 |
-
@spaces.GPU
|
| 78 |
-
def infer_example(input_image, prompt, lora_adapter):
|
| 79 |
-
"""
|
| 80 |
-
Wrapper function for gr.Examples to call the main infer logic.
|
| 81 |
-
It unpacks the results to match the expected outputs of the Examples component.
|
| 82 |
-
"""
|
| 83 |
-
image, seed, _ = infer(input_image, prompt, lora_adapter)
|
| 84 |
-
return image, seed
|
| 85 |
-
|
| 86 |
-
css="""
|
| 87 |
-
#col-container {
|
| 88 |
-
margin: 0 auto;
|
| 89 |
-
max-width: 960px;
|
| 90 |
-
}
|
| 91 |
-
"""
|
| 92 |
-
|
| 93 |
-
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
| 94 |
-
|
| 95 |
-
with gr.Column(elem_id="col-container"):
|
| 96 |
-
gr.Markdown(f"""# **[Photo-Mate-i2i](https://huggingface.co/collections/prithivMLmods/i2i-kontext-exp-68ce573b5c0623476b636ec7)**
|
| 97 |
-
Image manipulation with Kontext adapters""")
|
| 98 |
-
with gr.Row():
|
| 99 |
-
with gr.Column():
|
| 100 |
-
input_image = gr.Image(label="Upload the image for editing", type="pil", height="300")
|
| 101 |
-
with gr.Row():
|
| 102 |
-
prompt = gr.Text(
|
| 103 |
-
label="Prompt",
|
| 104 |
-
show_label=False,
|
| 105 |
-
max_lines=1,
|
| 106 |
-
placeholder="Enter your prompt for editing (e.g., 'Remove glasses', 'Add a hat')",
|
| 107 |
-
container=False,
|
| 108 |
-
)
|
| 109 |
-
run_button = gr.Button("Run", scale=0)
|
| 110 |
-
with gr.Accordion("Advanced Settings", open=False):
|
| 111 |
-
|
| 112 |
-
seed = gr.Slider(
|
| 113 |
-
label="Seed",
|
| 114 |
-
minimum=0,
|
| 115 |
-
maximum=MAX_SEED,
|
| 116 |
-
step=1,
|
| 117 |
-
value=0,
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 121 |
-
|
| 122 |
-
guidance_scale = gr.Slider(
|
| 123 |
-
label="Guidance Scale",
|
| 124 |
-
minimum=1,
|
| 125 |
-
maximum=10,
|
| 126 |
-
step=0.1,
|
| 127 |
-
value=2.5,
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
steps = gr.Slider(
|
| 131 |
-
label="Steps",
|
| 132 |
-
minimum=1,
|
| 133 |
-
maximum=30,
|
| 134 |
-
value=28,
|
| 135 |
-
step=1
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
with gr.Column():
|
| 139 |
-
result = gr.Image(label="Result", show_label=False, interactive=False, format="png")
|
| 140 |
-
reuse_button = gr.Button("Reuse this image", visible=False)
|
| 141 |
-
with gr.Row():
|
| 142 |
-
lora_adapter = gr.Dropdown(
|
| 143 |
-
label="Choose Adapter",
|
| 144 |
-
choices=["PhotoCleanser", "PhotoRestorer", "PolaroidWarm", "MonochromePencil"],
|
| 145 |
-
value="PhotoCleanser"
|
| 146 |
-
)
|
| 147 |
-
|
| 148 |
-
# Consolidate all examples into a single component with a new "Selected LoRA" column
|
| 149 |
-
gr.Examples(
|
| 150 |
-
examples=[
|
| 151 |
-
[
|
| 152 |
-
"photocleanser/1.png",
|
| 153 |
-
"[photo content], remove the embroidered pattern from the image while preserving the background and remaining elements, maintaining realism and original details.",
|
| 154 |
-
"PhotoCleanser"
|
| 155 |
-
],
|
| 156 |
-
[
|
| 157 |
-
"photocleanser/2.png",
|
| 158 |
-
"[photo content], remove the cat from the image while preserving the background and remaining elements, maintaining realism and original details.",
|
| 159 |
-
"PhotoCleanser"
|
| 160 |
-
],
|
| 161 |
-
[
|
| 162 |
-
"photorestore/1.png",
|
| 163 |
-
"[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.",
|
| 164 |
-
"PhotoRestorer"
|
| 165 |
-
],
|
| 166 |
-
[
|
| 167 |
-
"photorestore/2.png",
|
| 168 |
-
"[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.",
|
| 169 |
-
"PhotoRestorer"
|
| 170 |
-
],
|
| 171 |
-
[
|
| 172 |
-
"polaroid/1.png",
|
| 173 |
-
"[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.",
|
| 174 |
-
"PolaroidWarm"
|
| 175 |
-
],
|
| 176 |
-
[
|
| 177 |
-
"polaroid/2.png",
|
| 178 |
-
"[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.",
|
| 179 |
-
"PolaroidWarm"
|
| 180 |
-
],
|
| 181 |
-
[
|
| 182 |
-
"pencil/1.png",
|
| 183 |
-
"[photo content], transform the image into a detailed monochrome pencil sketch, emphasizing sharp lines, textures, and shading for a classic hand-drawn look.",
|
| 184 |
-
"MonochromePencil"
|
| 185 |
-
],
|
| 186 |
-
[
|
| 187 |
-
"pencil/2.png",
|
| 188 |
-
"[photo content], convert the photo into a realistic graphite pencil drawing, capturing the subject's form and depth with varied strokes and contrast.",
|
| 189 |
-
"MonochromePencil"
|
| 190 |
-
]
|
| 191 |
-
],
|
| 192 |
-
# The inputs now include the lora_adapter dropdown
|
| 193 |
-
inputs=[input_image, prompt, lora_adapter],
|
| 194 |
-
outputs=[result, seed],
|
| 195 |
-
fn=infer_example,
|
| 196 |
-
cache_examples=False,
|
| 197 |
-
# Provide headers for clarity
|
| 198 |
-
label="Examples (Image | Prompt | Selected LoRA)"
|
| 199 |
-
)
|
| 200 |
-
|
| 201 |
-
gr.on(
|
| 202 |
-
triggers=[run_button.click, prompt.submit],
|
| 203 |
-
fn = infer,
|
| 204 |
-
inputs = [input_image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
|
| 205 |
-
outputs = [result, se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