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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import random
|
| 4 |
+
import torch
|
| 5 |
+
import spaces
|
| 6 |
+
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from diffusers import FlowMatchEulerDiscreteScheduler
|
| 9 |
+
from optimization import optimize_pipeline_
|
| 10 |
+
from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
|
| 11 |
+
from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
|
| 12 |
+
from qwenimage.qwen_fa3_processor import QwenDoubleStreamAttnProcessorFA3
|
| 13 |
+
|
| 14 |
+
import math
|
| 15 |
+
from huggingface_hub import hf_hub_download
|
| 16 |
+
from safetensors.torch import load_file
|
| 17 |
+
|
| 18 |
+
from PIL import Image
|
| 19 |
+
import os
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# --- Model Loading ---
|
| 23 |
+
dtype = torch.bfloat16
|
| 24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 25 |
+
|
| 26 |
+
pipe = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509",
|
| 27 |
+
transformer= QwenImageTransformer2DModel.from_pretrained("linoyts/Qwen-Image-Edit-Rapid-AIO",
|
| 28 |
+
subfolder='transformer',
|
| 29 |
+
torch_dtype=dtype,
|
| 30 |
+
device_map='cuda'),torch_dtype=dtype).to(device)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
pipe.load_lora_weights("tlennon-ie/QwenEdit2509-FlatLogColor",
|
| 35 |
+
weight_name="QwenEdit2509-FlatLogColor.safetensors",
|
| 36 |
+
adapter_name="FlatLogColor")
|
| 37 |
+
pipe.set_adapters(["FlatLogColor"], adapter_weights=[1.])
|
| 38 |
+
pipe.fuse_lora(adapter_names=["FlatLogColor"], lora_scale=1.0)
|
| 39 |
+
pipe.unload_lora_weights()
|
| 40 |
+
|
| 41 |
+
pipe.transformer.__class__ = QwenImageTransformer2DModel
|
| 42 |
+
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
| 43 |
+
|
| 44 |
+
optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))], prompt="prompt")
|
| 45 |
+
|
| 46 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 47 |
+
|
| 48 |
+
@spaces.GPU
|
| 49 |
+
def convert_to_anime(
|
| 50 |
+
image,
|
| 51 |
+
seed,
|
| 52 |
+
randomize_seed,
|
| 53 |
+
true_guidance_scale,
|
| 54 |
+
num_inference_steps,
|
| 55 |
+
height,
|
| 56 |
+
width,
|
| 57 |
+
progress=gr.Progress(track_tqdm=True)
|
| 58 |
+
):
|
| 59 |
+
prompt = "flatcolor Desaturate the image and lower the contrast to create a flat, ungraded look similar to a camera's log profile. Preserve details in the highlights and shadows."
|
| 60 |
+
|
| 61 |
+
if randomize_seed:
|
| 62 |
+
seed = random.randint(0, MAX_SEED)
|
| 63 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 64 |
+
|
| 65 |
+
pil_images = []
|
| 66 |
+
if image is not None:
|
| 67 |
+
if isinstance(image, Image.Image):
|
| 68 |
+
pil_images.append(image.convert("RGB"))
|
| 69 |
+
elif hasattr(image, "name"):
|
| 70 |
+
pil_images.append(Image.open(image.name).convert("RGB"))
|
| 71 |
+
|
| 72 |
+
if len(pil_images) == 0:
|
| 73 |
+
raise gr.Error("Please upload an image first.")
|
| 74 |
+
|
| 75 |
+
result = pipe(
|
| 76 |
+
image=pil_images,
|
| 77 |
+
prompt=prompt,
|
| 78 |
+
height=height if height != 0 else None,
|
| 79 |
+
width=width if width != 0 else None,
|
| 80 |
+
num_inference_steps=num_inference_steps,
|
| 81 |
+
generator=generator,
|
| 82 |
+
true_cfg_scale=true_guidance_scale,
|
| 83 |
+
num_images_per_prompt=1,
|
| 84 |
+
).images[0]
|
| 85 |
+
|
| 86 |
+
return result, seed
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
# --- UI ---
|
| 90 |
+
css = '''
|
| 91 |
+
#col-container {
|
| 92 |
+
max-width: 900px;
|
| 93 |
+
margin: 0 auto;
|
| 94 |
+
padding: 2rem;
|
| 95 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
| 96 |
+
}
|
| 97 |
+
.gradio-container.light {
|
| 98 |
+
background: linear-gradient(to bottom, #f5f5f7, #ffffff);
|
| 99 |
+
}
|
| 100 |
+
.gradio-container.dark {
|
| 101 |
+
background: linear-gradient(to bottom, #1a1a1a, #0d0d0d);
|
| 102 |
+
}
|
| 103 |
+
#title {
|
| 104 |
+
text-align: center;
|
| 105 |
+
font-size: 2.5rem;
|
| 106 |
+
font-weight: 600;
|
| 107 |
+
margin-bottom: 0.5rem;
|
| 108 |
+
}
|
| 109 |
+
.light #title {
|
| 110 |
+
color: #1d1d1f;
|
| 111 |
+
}
|
| 112 |
+
.dark #title {
|
| 113 |
+
color: #f5f5f7;
|
| 114 |
+
}
|
| 115 |
+
#description {
|
| 116 |
+
text-align: center;
|
| 117 |
+
font-size: 1.1rem;
|
| 118 |
+
margin-bottom: 2rem;
|
| 119 |
+
}
|
| 120 |
+
.light #description {
|
| 121 |
+
color: #6e6e73;
|
| 122 |
+
}
|
| 123 |
+
.dark #description {
|
| 124 |
+
color: #a1a1a6;
|
| 125 |
+
}
|
| 126 |
+
.light #description a {
|
| 127 |
+
color: #0071e3;
|
| 128 |
+
}
|
| 129 |
+
.dark #description a {
|
| 130 |
+
color: #2997ff;
|
| 131 |
+
}
|
| 132 |
+
.image-container {
|
| 133 |
+
border-radius: 18px;
|
| 134 |
+
overflow: hidden;
|
| 135 |
+
}
|
| 136 |
+
.light .image-container {
|
| 137 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.07);
|
| 138 |
+
}
|
| 139 |
+
.dark .image-container {
|
| 140 |
+
box-shadow: 0 4px 6px rgba(255, 255, 255, 0.1);
|
| 141 |
+
}
|
| 142 |
+
#convert-btn {
|
| 143 |
+
background: linear-gradient(180deg, #0071e3 0%, #0077ed 100%);
|
| 144 |
+
border: none;
|
| 145 |
+
border-radius: 12px;
|
| 146 |
+
color: white;
|
| 147 |
+
font-size: 1.1rem;
|
| 148 |
+
font-weight: 500;
|
| 149 |
+
padding: 0.75rem 2rem;
|
| 150 |
+
transition: all 0.3s ease;
|
| 151 |
+
}
|
| 152 |
+
#convert-btn:hover {
|
| 153 |
+
transform: translateY(-2px);
|
| 154 |
+
box-shadow: 0 8px 16px rgba(0, 113, 227, 0.3);
|
| 155 |
+
}
|
| 156 |
+
'''
|
| 157 |
+
|
| 158 |
+
def update_dimensions_on_upload(image):
|
| 159 |
+
if image is None:
|
| 160 |
+
return 1024, 1024
|
| 161 |
+
|
| 162 |
+
original_width, original_height = image.size
|
| 163 |
+
|
| 164 |
+
if original_width > original_height:
|
| 165 |
+
new_width = 1024
|
| 166 |
+
aspect_ratio = original_height / original_width
|
| 167 |
+
new_height = int(new_width * aspect_ratio)
|
| 168 |
+
else:
|
| 169 |
+
new_height = 1024
|
| 170 |
+
aspect_ratio = original_width / original_height
|
| 171 |
+
new_width = int(new_height * aspect_ratio)
|
| 172 |
+
|
| 173 |
+
# Ensure dimensions are multiples of 8
|
| 174 |
+
new_width = (new_width // 8) * 8
|
| 175 |
+
new_height = (new_height // 8) * 8
|
| 176 |
+
|
| 177 |
+
return new_width, new_height
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 181 |
+
with gr.Column(elem_id="col-container"):
|
| 182 |
+
gr.Markdown("# 🎨 QwenEdit2509-FlatLogColor ", elem_id="title")
|
| 183 |
+
gr.Markdown(
|
| 184 |
+
"""
|
| 185 |
+
Turn images into FLAT/LOG style for Pro Color Grading workflows✨ with the <a href='https://huggingface.co/tlennon-ie/QwenEdit2509-FlatLogColor' target='_blank' style='color: #0071e3; text-decoration: none; font-weight: 500;'>tlennon-ie/QwenEdit2509-FlatLogColor</a> model
|
| 186 |
+
<br>
|
| 187 |
+
<div style='text-align: center; margin-top: 1rem;'>
|
| 188 |
+
<a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank' style='color: #0071e3; text-decoration: none; font-weight: 500;'>Built with anycoder</a>
|
| 189 |
+
</div>
|
| 190 |
+
""",
|
| 191 |
+
elem_id="description"
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
with gr.Column(scale=1):
|
| 196 |
+
image = gr.Image(
|
| 197 |
+
label="Upload Photo",
|
| 198 |
+
type="pil",
|
| 199 |
+
elem_classes="image-container"
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 203 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
|
| 204 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 205 |
+
true_guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=1.0)
|
| 206 |
+
num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=40, step=1, value=4)
|
| 207 |
+
height = gr.Slider(label="Height", minimum=256, maximum=2048, step=8, value=1024, visible=False)
|
| 208 |
+
width = gr.Slider(label="Width", minimum=256, maximum=2048, step=8, value=1024, visible=False)
|
| 209 |
+
|
| 210 |
+
convert_btn = gr.Button("Turn to Flat/LOG Profile", variant="primary", elem_id="convert-btn", size="lg")
|
| 211 |
+
|
| 212 |
+
with gr.Column(scale=1):
|
| 213 |
+
result = gr.Image(
|
| 214 |
+
label="Enhanced Result",
|
| 215 |
+
interactive=False,
|
| 216 |
+
elem_classes="image-container"
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
inputs = [
|
| 220 |
+
image, seed, randomize_seed, true_guidance_scale,
|
| 221 |
+
num_inference_steps, height, width
|
| 222 |
+
]
|
| 223 |
+
outputs = [result, seed]
|
| 224 |
+
|
| 225 |
+
# Convert button click
|
| 226 |
+
convert_btn.click(
|
| 227 |
+
fn=convert_to_anime,
|
| 228 |
+
inputs=inputs,
|
| 229 |
+
outputs=outputs
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
# Image upload triggers dimension update
|
| 233 |
+
image.upload(
|
| 234 |
+
fn=update_dimensions_on_upload,
|
| 235 |
+
inputs=[image],
|
| 236 |
+
outputs=[width, height]
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
demo.launch()
|