import os
import gradio as gr
import numpy as np
import spaces
import torch
import random
from PIL import Image
from typing import Iterable
from diffusers import FluxKontextPipeline
from diffusers.utils import load_image
from huggingface_hub import hf_hub_download
from gradio_imageslider import ImageSlider
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# --- Main Model Initialization ---
MAX_SEED = np.iinfo(np.int32).max
pipe = FluxKontextPipeline.from_pretrained("black-forest-labs/FLUX.1-Kontext-dev", torch_dtype=torch.bfloat16).to("cuda")
# --- Load New Adapter ---
pipe.load_lora_weights("prithivMLmods/Kontext-Watermark-Remover", weight_name="Kontext-Watermark-Remover.safetensors", adapter_name="watermark_remover")
@spaces.GPU
def infer(input_image, prompt, seed=42, randomize_seed=False, guidance_scale=2.5, steps=28, progress=gr.Progress(track_tqdm=True)):
"""
Perform image editing, returning a pair for the ImageSlider.
"""
if not input_image:
raise gr.Error("Please upload an image for editing.")
pipe.set_adapters(["watermark_remover"], adapter_weights=[1.0])
if randomize_seed:
seed = random.randint(0, MAX_SEED)
original_image = input_image.copy().convert("RGB")
image = pipe(
image=original_image,
prompt=prompt,
guidance_scale=guidance_scale,
width = original_image.size[0],
height = original_image.size[1],
num_inference_steps=steps,
generator=torch.Generator().manual_seed(seed),
).images[0]
return (original_image, image), seed, gr.Button(visible=True)
css="""
#col-container {
margin: 0 auto;
max-width: 960px;
}
#main-title h1 {font-size: 2.1em !important;}
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown("# **Photo-Mate-i2i: Watermark Remover**", elem_id="main-title")
gr.Markdown("Image manipulation with FLUX.1 Kontext. This demo focuses on watermark removal.")
with gr.Row():
with gr.Column():
input_image = gr.Image(label="Upload Image with Watermark", type="pil", height="300")
with gr.Row():
prompt = gr.Text(
label="Edit Prompt",
show_label=False,
max_lines=1,
placeholder="e.g., 'Remove the watermark'",
container=False,
value="[photo content], remove any watermark text or logos from the image while preserving the background, texture, lighting, and overall realism. Ensure the edited areas blend seamlessly with surrounding details, leaving no visible traces of watermark removal."
)
run_button = gr.Button("Run", variant="primary", scale=0)
with gr.Accordion("Advanced Settings", open=False):
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=MAX_SEED,
step=1,
value=0,
)
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
guidance_scale = gr.Slider(
label="Guidance Scale",
minimum=1,
maximum=10,
step=0.1,
value=2.5,
)
steps = gr.Slider(
label="Steps",
minimum=1,
maximum=30,
value=28,
step=1
)
with gr.Column():
output_slider = ImageSlider(label="Before / After", show_label=False, interactive=False)
reuse_button = gr.Button("Reuse this image", visible=False)
gr.on(
triggers=[run_button.click, prompt.submit],
fn=infer,
inputs=[input_image, prompt, seed, randomize_seed, guidance_scale, steps],
outputs=[output_slider, seed, reuse_button]
)
reuse_button.click(
fn=lambda images: images[1] if isinstance(images, (list, tuple)) and len(images) > 1 else images,
inputs=[output_slider],
outputs=[input_image]
)
demo.launch(mcp_server=True, ssr_mode=False, show_error=True)