Spaces:
Running
on
Zero
Running
on
Zero
Upload 4 files
Browse files- .gitattributes +2 -0
- app (1).py +190 -0
- bear1.jpg +3 -0
- bear2.jpg +3 -0
- requirements.txt +14 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
bear1.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
bear2.jpg filter=lfs diff=lfs merge=lfs -text
|
app (1).py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
import math
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from diffusers import QwenImageEditPlusPipeline, FlowMatchEulerDiscreteScheduler
|
| 7 |
+
|
| 8 |
+
# Load pipeline with optimized scheduler at startup
|
| 9 |
+
scheduler_config = {
|
| 10 |
+
"base_image_seq_len": 256,
|
| 11 |
+
"base_shift": math.log(3),
|
| 12 |
+
"invert_sigmas": False,
|
| 13 |
+
"max_image_seq_len": 8192,
|
| 14 |
+
"max_shift": math.log(3),
|
| 15 |
+
"num_train_timesteps": 1000,
|
| 16 |
+
"shift": 1.0,
|
| 17 |
+
"shift_terminal": None,
|
| 18 |
+
"stochastic_sampling": False,
|
| 19 |
+
"time_shift_type": "exponential",
|
| 20 |
+
"use_beta_sigmas": False,
|
| 21 |
+
"use_dynamic_shifting": True,
|
| 22 |
+
"use_exponential_sigmas": False,
|
| 23 |
+
"use_karras_sigmas": False,
|
| 24 |
+
}
|
| 25 |
+
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
|
| 26 |
+
|
| 27 |
+
pipeline = QwenImageEditPlusPipeline.from_pretrained(
|
| 28 |
+
"Qwen/Qwen-Image-Edit-2509",
|
| 29 |
+
scheduler=scheduler,
|
| 30 |
+
torch_dtype=torch.bfloat16
|
| 31 |
+
)
|
| 32 |
+
pipeline.to('cuda')
|
| 33 |
+
pipeline.set_progress_bar_config(disable=None)
|
| 34 |
+
|
| 35 |
+
# Load LoRA for faster inference
|
| 36 |
+
pipeline.load_lora_weights(
|
| 37 |
+
"lightx2v/Qwen-Image-Lightning",
|
| 38 |
+
weight_name="Qwen-Image-Lightning-8steps-V2.0-bf16.safetensors"
|
| 39 |
+
)
|
| 40 |
+
pipeline.fuse_lora()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@spaces.GPU(duration=60)
|
| 45 |
+
def edit_images(image1, image2, prompt, seed, true_cfg_scale, negative_prompt, num_steps, guidance_scale):
|
| 46 |
+
if image1 is None or image2 is None:
|
| 47 |
+
gr.Warning("Please upload both images")
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
# Convert to PIL if needed
|
| 51 |
+
if not isinstance(image1, Image.Image):
|
| 52 |
+
image1 = Image.fromarray(image1)
|
| 53 |
+
if not isinstance(image2, Image.Image):
|
| 54 |
+
image2 = Image.fromarray(image2)
|
| 55 |
+
|
| 56 |
+
inputs = {
|
| 57 |
+
"image": [image1, image2],
|
| 58 |
+
"prompt": prompt,
|
| 59 |
+
"generator": torch.manual_seed(seed),
|
| 60 |
+
"true_cfg_scale": true_cfg_scale,
|
| 61 |
+
"negative_prompt": negative_prompt,
|
| 62 |
+
"num_inference_steps": num_steps,
|
| 63 |
+
"guidance_scale": guidance_scale,
|
| 64 |
+
"num_images_per_prompt": 1,
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
with torch.inference_mode():
|
| 68 |
+
output = pipeline(**inputs)
|
| 69 |
+
return output.images[0]
|
| 70 |
+
|
| 71 |
+
# Example prompts and images
|
| 72 |
+
example_prompts = [
|
| 73 |
+
"The magician bear is on the left, the alchemist bear is on the right, facing each other in the central park square.",
|
| 74 |
+
"Two characters standing side by side in a beautiful garden with flowers blooming",
|
| 75 |
+
"The hero on the left and the villain on the right, facing off in an epic battle scene",
|
| 76 |
+
"Two friends sitting together on a park bench, enjoying the sunset",
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
# Example image paths
|
| 80 |
+
example_images = [
|
| 81 |
+
["bear1.jpg", "bear2.jpg", "The magician bear is on the left, the alchemist bear is on the right, facing each other in the central park square."],
|
| 82 |
+
]
|
| 83 |
+
|
| 84 |
+
with gr.Blocks(css="footer {visibility: hidden}") as demo:
|
| 85 |
+
gr.Markdown(
|
| 86 |
+
"""
|
| 87 |
+
# Qwen Image Edit Plus (Optimized)
|
| 88 |
+
|
| 89 |
+
Upload two images and describe how you want them combined or edited together.
|
| 90 |
+
|
| 91 |
+
[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 92 |
+
"""
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
with gr.Row():
|
| 96 |
+
with gr.Column():
|
| 97 |
+
image1_input = gr.Image(
|
| 98 |
+
label="First Image",
|
| 99 |
+
type="pil",
|
| 100 |
+
height=300
|
| 101 |
+
)
|
| 102 |
+
image2_input = gr.Image(
|
| 103 |
+
label="Second Image",
|
| 104 |
+
type="pil",
|
| 105 |
+
height=300
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
with gr.Column():
|
| 109 |
+
output_image = gr.Image(
|
| 110 |
+
label="Edited Result",
|
| 111 |
+
type="pil",
|
| 112 |
+
height=620
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with gr.Group():
|
| 116 |
+
prompt_input = gr.Textbox(
|
| 117 |
+
label="Prompt",
|
| 118 |
+
placeholder="Describe how you want the images combined or edited...",
|
| 119 |
+
value=example_prompts[0],
|
| 120 |
+
lines=3
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
gr.Examples(
|
| 124 |
+
examples=example_images,
|
| 125 |
+
inputs=[image1_input, image2_input, prompt_input],
|
| 126 |
+
label="Example Images and Prompts"
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
gr.Examples(
|
| 130 |
+
examples=[[p] for p in example_prompts],
|
| 131 |
+
inputs=[prompt_input],
|
| 132 |
+
label="Example Prompts Only"
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 136 |
+
with gr.Row():
|
| 137 |
+
seed_input = gr.Number(
|
| 138 |
+
label="Seed",
|
| 139 |
+
value=0,
|
| 140 |
+
precision=0
|
| 141 |
+
)
|
| 142 |
+
num_steps = gr.Slider(
|
| 143 |
+
label="Number of Inference Steps",
|
| 144 |
+
minimum=8,
|
| 145 |
+
maximum=30,
|
| 146 |
+
value=8,
|
| 147 |
+
step=1
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
with gr.Row():
|
| 151 |
+
true_cfg_scale = gr.Slider(
|
| 152 |
+
label="True CFG Scale",
|
| 153 |
+
minimum=1.0,
|
| 154 |
+
maximum=10.0,
|
| 155 |
+
value=1.0,
|
| 156 |
+
step=0.5
|
| 157 |
+
)
|
| 158 |
+
guidance_scale = gr.Slider(
|
| 159 |
+
label="Guidance Scale",
|
| 160 |
+
minimum=1.0,
|
| 161 |
+
maximum=5.0,
|
| 162 |
+
value=1.0,
|
| 163 |
+
step=0.1
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
negative_prompt = gr.Textbox(
|
| 167 |
+
label="Negative Prompt",
|
| 168 |
+
value=" ",
|
| 169 |
+
placeholder="What to avoid in the generation..."
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
generate_btn = gr.Button("Generate Edited Image", variant="primary", size="lg")
|
| 173 |
+
|
| 174 |
+
generate_btn.click(
|
| 175 |
+
fn=edit_images,
|
| 176 |
+
inputs=[
|
| 177 |
+
image1_input,
|
| 178 |
+
image2_input,
|
| 179 |
+
prompt_input,
|
| 180 |
+
seed_input,
|
| 181 |
+
true_cfg_scale,
|
| 182 |
+
negative_prompt,
|
| 183 |
+
num_steps,
|
| 184 |
+
guidance_scale
|
| 185 |
+
],
|
| 186 |
+
outputs=output_image,
|
| 187 |
+
show_progress="full"
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
demo.launch()
|
bear1.jpg
ADDED
|
Git LFS Details
|
bear2.jpg
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/diffusers
|
| 2 |
+
git+https://github.com/huggingface/transformers
|
| 3 |
+
sentencepiece
|
| 4 |
+
accelerate
|
| 5 |
+
torch
|
| 6 |
+
torchvision
|
| 7 |
+
torchaudio
|
| 8 |
+
gradio
|
| 9 |
+
spaces
|
| 10 |
+
Pillow
|
| 11 |
+
tokenizers
|
| 12 |
+
numpy
|
| 13 |
+
requests
|
| 14 |
+
peft
|