Spaces:
Running
on
Zero
Running
on
Zero
revert: temp revert for you animals for local debugging
Browse files- README.md +2 -2
- app.py +11 -0
- app_temp.py +395 -0
- optimization_simple.py +70 -0
README.md
CHANGED
|
@@ -5,10 +5,10 @@ colorFrom: indigo
|
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.46.1
|
| 8 |
-
app_file:
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
-
short_description: 4-step Qwen Image Edit 2509 w/ a
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.46.1
|
| 8 |
+
app_file: app_temp.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
+
short_description: 4-step Qwen Image Edit 2509 w/ a local caption model.
|
| 12 |
---
|
| 13 |
|
| 14 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -228,7 +228,18 @@ scheduler_config = {
|
|
| 228 |
|
| 229 |
# Initialize scheduler with Lightning config
|
| 230 |
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
|
|
|
|
|
|
| 232 |
# Load the model pipeline
|
| 233 |
pipe = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509",
|
| 234 |
scheduler=scheduler,
|
|
|
|
| 228 |
|
| 229 |
# Initialize scheduler with Lightning config
|
| 230 |
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
|
| 231 |
+
# Load the model pipeline
|
| 232 |
+
pipe = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509", torch_dtype=dtype).to(device)
|
| 233 |
+
|
| 234 |
+
# Apply the same optimizations from the first version
|
| 235 |
+
pipe.transformer.__class__ = QwenImageTransformer2DModel
|
| 236 |
+
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
| 237 |
+
|
| 238 |
+
# --- Ahead-of-time compilation ---
|
| 239 |
+
optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))], prompt="prompt")
|
| 240 |
|
| 241 |
+
# --- UI Constants and Helpers ---
|
| 242 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 243 |
# Load the model pipeline
|
| 244 |
pipe = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509",
|
| 245 |
scheduler=scheduler,
|
app_temp.py
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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_simple 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 |
+
from huggingface_hub import InferenceClient
|
| 15 |
+
import math
|
| 16 |
+
|
| 17 |
+
import os
|
| 18 |
+
import base64
|
| 19 |
+
import json
|
| 20 |
+
|
| 21 |
+
SYSTEM_PROMPT = '''
|
| 22 |
+
# Edit Instruction Rewriter
|
| 23 |
+
You are a professional edit instruction rewriter. Your task is to generate a precise, concise, and visually achievable professional-level edit instruction based on the user-provided instruction and the image to be edited.
|
| 24 |
+
|
| 25 |
+
Please strictly follow the rewriting rules below:
|
| 26 |
+
|
| 27 |
+
## 1. General Principles
|
| 28 |
+
- Keep the rewritten prompt **concise and comprehensive**. Avoid overly long sentences and unnecessary descriptive language.
|
| 29 |
+
- If the instruction is contradictory, vague, or unachievable, prioritize reasonable inference and correction, and supplement details when necessary.
|
| 30 |
+
- Keep the main part of the original instruction unchanged, only enhancing its clarity, rationality, and visual feasibility.
|
| 31 |
+
- All added objects or modifications must align with the logic and style of the scene in the input images.
|
| 32 |
+
- If multiple sub-images are to be generated, describe the content of each sub-image individually.
|
| 33 |
+
|
| 34 |
+
## 2. Task-Type Handling Rules
|
| 35 |
+
|
| 36 |
+
### 1. Add, Delete, Replace Tasks
|
| 37 |
+
- If the instruction is clear (already includes task type, target entity, position, quantity, attributes), preserve the original intent and only refine the grammar.
|
| 38 |
+
- If the description is vague, supplement with minimal but sufficient details (category, color, size, orientation, position, etc.). For example:
|
| 39 |
+
> Original: "Add an animal"
|
| 40 |
+
> Rewritten: "Add a light-gray cat in the bottom-right corner, sitting and facing the camera"
|
| 41 |
+
- Remove meaningless instructions: e.g., "Add 0 objects" should be ignored or flagged as invalid.
|
| 42 |
+
- For replacement tasks, specify "Replace Y with X" and briefly describe the key visual features of X.
|
| 43 |
+
|
| 44 |
+
### 2. Text Editing Tasks
|
| 45 |
+
- All text content must be enclosed in English double quotes `" "`. Keep the original language of the text, and keep the capitalization.
|
| 46 |
+
- Both adding new text and replacing existing text are text replacement tasks, For example:
|
| 47 |
+
- Replace "xx" to "yy"
|
| 48 |
+
- Replace the mask / bounding box to "yy"
|
| 49 |
+
- Replace the visual object to "yy"
|
| 50 |
+
- Specify text position, color, and layout only if user has required.
|
| 51 |
+
- If font is specified, keep the original language of the font.
|
| 52 |
+
|
| 53 |
+
### 3. Human Editing Tasks
|
| 54 |
+
- Make the smallest changes to the given user's prompt.
|
| 55 |
+
- If changes to background, action, expression, camera shot, or ambient lighting are required, please list each modification individually.
|
| 56 |
+
- **Edits to makeup or facial features / expression must be subtle, not exaggerated, and must preserve the subject’s identity consistency.**
|
| 57 |
+
> Original: "Add eyebrows to the face"
|
| 58 |
+
> Rewritten: "Slightly thicken the person’s eyebrows with little change, look natural."
|
| 59 |
+
|
| 60 |
+
### 4. Style Conversion or Enhancement Tasks
|
| 61 |
+
- If a style is specified, describe it concisely using key visual features. For example:
|
| 62 |
+
> Original: "Disco style"
|
| 63 |
+
> Rewritten: "1970s disco style: flashing lights, disco ball, mirrored walls, vibrant colors"
|
| 64 |
+
- For style reference, analyze the original image and extract key characteristics (color, composition, texture, lighting, artistic style, etc.), integrating them into the instruction.
|
| 65 |
+
- **Colorization tasks (including old photo restoration) must use the fixed template:**
|
| 66 |
+
"Restore and colorize the old photo."
|
| 67 |
+
- Clearly specify the object to be modified. For example:
|
| 68 |
+
> Original: Modify the subject in Picture 1 to match the style of Picture 2.
|
| 69 |
+
> Rewritten: Change the girl in Picture 1 to the ink-wash style of Picture 2 — rendered in black-and-white watercolor with soft color transitions.
|
| 70 |
+
|
| 71 |
+
### 5. Material Replacement
|
| 72 |
+
- Clearly specify the object and the material. For example: "Change the material of the apple to papercut style."
|
| 73 |
+
- For text material replacement, use the fixed template:
|
| 74 |
+
"Change the material of text "xxxx" to laser style"
|
| 75 |
+
|
| 76 |
+
### 6. Logo/Pattern Editing
|
| 77 |
+
- Material replacement should preserve the original shape and structure as much as possible. For example:
|
| 78 |
+
> Original: "Convert to sapphire material"
|
| 79 |
+
> Rewritten: "Convert the main subject in the image to sapphire material, preserving similar shape and structure"
|
| 80 |
+
- When migrating logos/patterns to new scenes, ensure shape and structure consistency. For example:
|
| 81 |
+
> Original: "Migrate the logo in the image to a new scene"
|
| 82 |
+
> Rewritten: "Migrate the logo in the image to a new scene, preserving similar shape and structure"
|
| 83 |
+
|
| 84 |
+
### 7. Multi-Image Tasks
|
| 85 |
+
- Rewritten prompts must clearly point out which image’s element is being modified. For example:
|
| 86 |
+
> Original: "Replace the subject of picture 1 with the subject of picture 2"
|
| 87 |
+
> Rewritten: "Replace the girl of picture 1 with the boy of picture 2, keeping picture 2’s background unchanged"
|
| 88 |
+
- For stylization tasks, describe the reference image’s style in the rewritten prompt, while preserving the visual content of the source image.
|
| 89 |
+
|
| 90 |
+
## 3. Rationale and Logic Check
|
| 91 |
+
- Resolve contradictory instructions: e.g., “Remove all trees but keep all trees” requires logical correction.
|
| 92 |
+
- Supplement missing critical information: e.g., if position is unspecified, choose a reasonable area based on composition (near subject, blank space, center/edge, etc.).
|
| 93 |
+
|
| 94 |
+
# Output Format Example
|
| 95 |
+
```json
|
| 96 |
+
{
|
| 97 |
+
"Rewritten": "..."
|
| 98 |
+
}
|
| 99 |
+
'''
|
| 100 |
+
# --- Prompt Enhancement using Hugging Face InferenceClient ---
|
| 101 |
+
def polish_prompt_hf(prompt, img_list):
|
| 102 |
+
"""
|
| 103 |
+
Rewrites the prompt using a Hugging Face InferenceClient.
|
| 104 |
+
"""
|
| 105 |
+
# Ensure HF_TOKEN is set
|
| 106 |
+
api_key = os.environ.get("HF_TOKEN")
|
| 107 |
+
if not api_key:
|
| 108 |
+
print("Warning: HF_TOKEN not set. Falling back to original prompt.")
|
| 109 |
+
return prompt
|
| 110 |
+
|
| 111 |
+
try:
|
| 112 |
+
# Initialize the client
|
| 113 |
+
prompt = f"{SYSTEM_PROMPT}\n\nUser Input: {prompt}\n\nRewritten Prompt:"
|
| 114 |
+
# Initialize the client
|
| 115 |
+
client = InferenceClient(
|
| 116 |
+
provider="novita",
|
| 117 |
+
api_key=api_key,
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# Format the messages for the chat completions API
|
| 121 |
+
sys_promot = "you are a helpful assistant, you should provide useful answers to users."
|
| 122 |
+
messages = [
|
| 123 |
+
{"role": "system", "content": sys_promot},
|
| 124 |
+
{"role": "user", "content": []}]
|
| 125 |
+
for img in img_list:
|
| 126 |
+
messages[1]["content"].append(
|
| 127 |
+
{"image": f"data:image/png;base64,{encode_image(img)}"})
|
| 128 |
+
messages[1]["content"].append({"text": f"{prompt}"})
|
| 129 |
+
|
| 130 |
+
completion = client.chat.completions.create(
|
| 131 |
+
model="Qwen/Qwen3-Next-80B-A3B-Instruct",
|
| 132 |
+
messages=messages,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Parse the response
|
| 136 |
+
result = completion.choices[0].message.content
|
| 137 |
+
|
| 138 |
+
# Try to extract JSON if present
|
| 139 |
+
if '{"Rewritten"' in result:
|
| 140 |
+
try:
|
| 141 |
+
# Clean up the response
|
| 142 |
+
result = result.replace('```json', '').replace('```', '')
|
| 143 |
+
result_json = json.loads(result)
|
| 144 |
+
polished_prompt = result_json.get('Rewritten', result)
|
| 145 |
+
except:
|
| 146 |
+
polished_prompt = result
|
| 147 |
+
else:
|
| 148 |
+
polished_prompt = result
|
| 149 |
+
|
| 150 |
+
polished_prompt = polished_prompt.strip().replace("\n", " ")
|
| 151 |
+
return polished_prompt
|
| 152 |
+
|
| 153 |
+
except Exception as e:
|
| 154 |
+
print(f"Error during API call to Hugging Face: {e}")
|
| 155 |
+
# Fallback to original prompt if enhancement fails
|
| 156 |
+
return prompt
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def encode_image(pil_image):
|
| 161 |
+
import io
|
| 162 |
+
buffered = io.BytesIO()
|
| 163 |
+
pil_image.save(buffered, format="PNG")
|
| 164 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 165 |
+
|
| 166 |
+
# --- Model Loading ---
|
| 167 |
+
dtype = torch.bfloat16
|
| 168 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 169 |
+
|
| 170 |
+
# Scheduler configuration for Lightning
|
| 171 |
+
scheduler_config = {
|
| 172 |
+
"base_image_seq_len": 256,
|
| 173 |
+
"base_shift": math.log(3),
|
| 174 |
+
"invert_sigmas": False,
|
| 175 |
+
"max_image_seq_len": 8192,
|
| 176 |
+
"max_shift": math.log(3),
|
| 177 |
+
"num_train_timesteps": 1000,
|
| 178 |
+
"shift": 1.0,
|
| 179 |
+
"shift_terminal": None,
|
| 180 |
+
"stochastic_sampling": False,
|
| 181 |
+
"time_shift_type": "exponential",
|
| 182 |
+
"use_beta_sigmas": False,
|
| 183 |
+
"use_dynamic_shifting": True,
|
| 184 |
+
"use_exponential_sigmas": False,
|
| 185 |
+
"use_karras_sigmas": False,
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
# Initialize scheduler with Lightning config
|
| 189 |
+
scheduler = FlowMatchEulerDiscreteScheduler.from_config(scheduler_config)
|
| 190 |
+
|
| 191 |
+
# Load the model pipeline
|
| 192 |
+
pipe = QwenImageEditPlusPipeline.from_pretrained("Qwen/Qwen-Image-Edit-2509",
|
| 193 |
+
scheduler=scheduler,
|
| 194 |
+
torch_dtype=dtype).to(device)
|
| 195 |
+
pipe.load_lora_weights(
|
| 196 |
+
"lightx2v/Qwen-Image-Lightning",
|
| 197 |
+
weight_name="Qwen-Image-Edit-2509/Qwen-Image-Edit-2509-Lightning-4steps-V1.0-fp32.safetensors"
|
| 198 |
+
)
|
| 199 |
+
pipe.fuse_lora()
|
| 200 |
+
|
| 201 |
+
# Apply the same optimizations from the first version
|
| 202 |
+
pipe.transformer.__class__ = QwenImageTransformer2DModel
|
| 203 |
+
pipe.transformer.set_attn_processor(QwenDoubleStreamAttnProcessorFA3())
|
| 204 |
+
|
| 205 |
+
# --- Ahead-of-time compilation ---
|
| 206 |
+
optimize_pipeline_(pipe, image=[Image.new("RGB", (1024, 1024)), Image.new("RGB", (1024, 1024))], prompt="prompt")
|
| 207 |
+
|
| 208 |
+
# --- UI Constants and Helpers ---
|
| 209 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 210 |
+
|
| 211 |
+
# --- Main Inference Function (with hardcoded negative prompt) ---
|
| 212 |
+
@spaces.GPU(duration=40)
|
| 213 |
+
def infer(
|
| 214 |
+
images,
|
| 215 |
+
prompt,
|
| 216 |
+
seed=42,
|
| 217 |
+
randomize_seed=False,
|
| 218 |
+
true_guidance_scale=1.0,
|
| 219 |
+
num_inference_steps=4,
|
| 220 |
+
height=None,
|
| 221 |
+
width=None,
|
| 222 |
+
rewrite_prompt=True,
|
| 223 |
+
num_images_per_prompt=1,
|
| 224 |
+
progress=gr.Progress(track_tqdm=True),
|
| 225 |
+
):
|
| 226 |
+
"""
|
| 227 |
+
Generates an image using the local Qwen-Image diffusers pipeline.
|
| 228 |
+
"""
|
| 229 |
+
# Hardcode the negative prompt as requested
|
| 230 |
+
negative_prompt = " "
|
| 231 |
+
|
| 232 |
+
if randomize_seed:
|
| 233 |
+
seed = random.randint(0, MAX_SEED)
|
| 234 |
+
|
| 235 |
+
# Set up the generator for reproducibility
|
| 236 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 237 |
+
|
| 238 |
+
# Load input images into PIL Images
|
| 239 |
+
pil_images = []
|
| 240 |
+
if images is not None:
|
| 241 |
+
for item in images:
|
| 242 |
+
try:
|
| 243 |
+
if isinstance(item[0], Image.Image):
|
| 244 |
+
pil_images.append(item[0].convert("RGB"))
|
| 245 |
+
elif isinstance(item[0], str):
|
| 246 |
+
pil_images.append(Image.open(item[0]).convert("RGB"))
|
| 247 |
+
elif hasattr(item, "name"):
|
| 248 |
+
pil_images.append(Image.open(item.name).convert("RGB"))
|
| 249 |
+
except Exception:
|
| 250 |
+
continue
|
| 251 |
+
|
| 252 |
+
if height==256 and width==256:
|
| 253 |
+
height, width = None, None
|
| 254 |
+
print(f"Calling pipeline with prompt: '{prompt}'")
|
| 255 |
+
print(f"Negative Prompt: '{negative_prompt}'")
|
| 256 |
+
print(f"Seed: {seed}, Steps: {num_inference_steps}, Guidance: {true_guidance_scale}, Size: {width}x{height}")
|
| 257 |
+
if rewrite_prompt and len(pil_images) > 0:
|
| 258 |
+
prompt = polish_prompt_hf(prompt, pil_images)
|
| 259 |
+
print(f"Rewritten Prompt: {prompt}")
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
# Generate the image
|
| 263 |
+
image = pipe(
|
| 264 |
+
image=pil_images if len(pil_images) > 0 else None,
|
| 265 |
+
prompt=prompt,
|
| 266 |
+
height=height,
|
| 267 |
+
width=width,
|
| 268 |
+
negative_prompt=negative_prompt,
|
| 269 |
+
num_inference_steps=num_inference_steps,
|
| 270 |
+
generator=generator,
|
| 271 |
+
true_cfg_scale=true_guidance_scale,
|
| 272 |
+
num_images_per_prompt=num_images_per_prompt,
|
| 273 |
+
).images
|
| 274 |
+
|
| 275 |
+
return image, seed
|
| 276 |
+
|
| 277 |
+
# --- Examples and UI Layout ---
|
| 278 |
+
examples = []
|
| 279 |
+
|
| 280 |
+
css = """
|
| 281 |
+
#col-container {
|
| 282 |
+
margin: 0 auto;
|
| 283 |
+
max-width: 1024px;
|
| 284 |
+
}
|
| 285 |
+
#logo-title {
|
| 286 |
+
text-align: center;
|
| 287 |
+
}
|
| 288 |
+
#logo-title img {
|
| 289 |
+
width: 400px;
|
| 290 |
+
}
|
| 291 |
+
#edit_text{margin-top: -62px !important}
|
| 292 |
+
"""
|
| 293 |
+
|
| 294 |
+
with gr.Blocks(css=css) as demo:
|
| 295 |
+
with gr.Column(elem_id="col-container"):
|
| 296 |
+
gr.HTML("""
|
| 297 |
+
<div id="logo-title">
|
| 298 |
+
<img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_edit_logo.png" alt="Qwen-Image Edit Logo" width="400" style="display: block; margin: 0 auto;">
|
| 299 |
+
<h2 style="font-style: italic;color: #5b47d1;margin-top: -27px !important;margin-left: 96px">[Plus] Fast, 8-steps with Lightning LoRA</h2>
|
| 300 |
+
</div>
|
| 301 |
+
""")
|
| 302 |
+
gr.Markdown("""
|
| 303 |
+
[Learn more](https://github.com/QwenLM/Qwen-Image) about the Qwen-Image series.
|
| 304 |
+
This demo uses the new [Qwen-Image-Edit-2509](https://huggingface.co/Qwen/Qwen-Image-Edit-2509) with the [Qwen-Image-Lightning v2](https://huggingface.co/lightx2v/Qwen-Image-Lightning) LoRA + [AoT compilation & FA3](https://huggingface.co/blog/zerogpu-aoti) for accelerated inference.
|
| 305 |
+
Try on [Qwen Chat](https://chat.qwen.ai/), or [download model](https://huggingface.co/Qwen/Qwen-Image-Edit-2509) to run locally with ComfyUI or diffusers.
|
| 306 |
+
""")
|
| 307 |
+
with gr.Row():
|
| 308 |
+
with gr.Column():
|
| 309 |
+
input_images = gr.Gallery(label="Input Images",
|
| 310 |
+
show_label=False,
|
| 311 |
+
type="pil",
|
| 312 |
+
interactive=True)
|
| 313 |
+
|
| 314 |
+
# result = gr.Image(label="Result", show_label=False, type="pil")
|
| 315 |
+
result = gr.Gallery(label="Result", show_label=False, type="pil")
|
| 316 |
+
with gr.Row():
|
| 317 |
+
prompt = gr.Text(
|
| 318 |
+
label="Prompt",
|
| 319 |
+
show_label=False,
|
| 320 |
+
placeholder="describe the edit instruction",
|
| 321 |
+
container=False,
|
| 322 |
+
)
|
| 323 |
+
run_button = gr.Button("Edit!", variant="primary")
|
| 324 |
+
|
| 325 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 326 |
+
# Negative prompt UI element is removed here
|
| 327 |
+
|
| 328 |
+
seed = gr.Slider(
|
| 329 |
+
label="Seed",
|
| 330 |
+
minimum=0,
|
| 331 |
+
maximum=MAX_SEED,
|
| 332 |
+
step=1,
|
| 333 |
+
value=0,
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 337 |
+
|
| 338 |
+
with gr.Row():
|
| 339 |
+
|
| 340 |
+
true_guidance_scale = gr.Slider(
|
| 341 |
+
label="True guidance scale",
|
| 342 |
+
minimum=1.0,
|
| 343 |
+
maximum=10.0,
|
| 344 |
+
step=0.1,
|
| 345 |
+
value=1.0
|
| 346 |
+
)
|
| 347 |
+
|
| 348 |
+
num_inference_steps = gr.Slider(
|
| 349 |
+
label="Number of inference steps",
|
| 350 |
+
minimum=1,
|
| 351 |
+
maximum=40,
|
| 352 |
+
step=1,
|
| 353 |
+
value=4,
|
| 354 |
+
)
|
| 355 |
+
|
| 356 |
+
height = gr.Slider(
|
| 357 |
+
label="Height",
|
| 358 |
+
minimum=256,
|
| 359 |
+
maximum=2048,
|
| 360 |
+
step=8,
|
| 361 |
+
value=None,
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
width = gr.Slider(
|
| 365 |
+
label="Width",
|
| 366 |
+
minimum=256,
|
| 367 |
+
maximum=2048,
|
| 368 |
+
step=8,
|
| 369 |
+
value=None,
|
| 370 |
+
)
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
rewrite_prompt = gr.Checkbox(label="Rewrite prompt (being fixed)", value=False)
|
| 374 |
+
|
| 375 |
+
# gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
|
| 376 |
+
|
| 377 |
+
gr.on(
|
| 378 |
+
triggers=[run_button.click, prompt.submit],
|
| 379 |
+
fn=infer,
|
| 380 |
+
inputs=[
|
| 381 |
+
input_images,
|
| 382 |
+
prompt,
|
| 383 |
+
seed,
|
| 384 |
+
randomize_seed,
|
| 385 |
+
true_guidance_scale,
|
| 386 |
+
num_inference_steps,
|
| 387 |
+
height,
|
| 388 |
+
width,
|
| 389 |
+
rewrite_prompt,
|
| 390 |
+
],
|
| 391 |
+
outputs=[result, seed],
|
| 392 |
+
)
|
| 393 |
+
|
| 394 |
+
if __name__ == "__main__":
|
| 395 |
+
demo.launch()
|
optimization_simple.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
"""
|
| 3 |
+
|
| 4 |
+
from typing import Any
|
| 5 |
+
from typing import Callable
|
| 6 |
+
from typing import ParamSpec
|
| 7 |
+
from torchao.quantization import quantize_
|
| 8 |
+
from torchao.quantization import Float8DynamicActivationFloat8WeightConfig
|
| 9 |
+
import spaces
|
| 10 |
+
import torch
|
| 11 |
+
from torch.utils._pytree import tree_map
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
P = ParamSpec('P')
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
TRANSFORMER_IMAGE_SEQ_LENGTH_DIM = torch.export.Dim('image_seq_length')
|
| 18 |
+
TRANSFORMER_TEXT_SEQ_LENGTH_DIM = torch.export.Dim('text_seq_length')
|
| 19 |
+
|
| 20 |
+
TRANSFORMER_DYNAMIC_SHAPES = {
|
| 21 |
+
'hidden_states': {
|
| 22 |
+
1: TRANSFORMER_IMAGE_SEQ_LENGTH_DIM,
|
| 23 |
+
},
|
| 24 |
+
'encoder_hidden_states': {
|
| 25 |
+
1: TRANSFORMER_TEXT_SEQ_LENGTH_DIM,
|
| 26 |
+
},
|
| 27 |
+
'encoder_hidden_states_mask': {
|
| 28 |
+
1: TRANSFORMER_TEXT_SEQ_LENGTH_DIM,
|
| 29 |
+
},
|
| 30 |
+
'image_rotary_emb': ({
|
| 31 |
+
0: TRANSFORMER_IMAGE_SEQ_LENGTH_DIM,
|
| 32 |
+
}, {
|
| 33 |
+
0: TRANSFORMER_TEXT_SEQ_LENGTH_DIM,
|
| 34 |
+
}),
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
INDUCTOR_CONFIGS = {
|
| 39 |
+
'conv_1x1_as_mm': True,
|
| 40 |
+
'epilogue_fusion': False,
|
| 41 |
+
'coordinate_descent_tuning': True,
|
| 42 |
+
'coordinate_descent_check_all_directions': True,
|
| 43 |
+
'max_autotune': True,
|
| 44 |
+
'triton.cudagraphs': True,
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kwargs):
|
| 49 |
+
|
| 50 |
+
@spaces.GPU(duration=1500)
|
| 51 |
+
def compile_transformer():
|
| 52 |
+
|
| 53 |
+
with spaces.aoti_capture(pipeline.transformer) as call:
|
| 54 |
+
pipeline(*args, **kwargs)
|
| 55 |
+
|
| 56 |
+
dynamic_shapes = tree_map(lambda t: None, call.kwargs)
|
| 57 |
+
dynamic_shapes |= TRANSFORMER_DYNAMIC_SHAPES
|
| 58 |
+
|
| 59 |
+
# quantize_(pipeline.transformer, Float8DynamicActivationFloat8WeightConfig())
|
| 60 |
+
|
| 61 |
+
exported = torch.export.export(
|
| 62 |
+
mod=pipeline.transformer,
|
| 63 |
+
args=call.args,
|
| 64 |
+
kwargs=call.kwargs,
|
| 65 |
+
dynamic_shapes=dynamic_shapes,
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
return spaces.aoti_compile(exported, INDUCTOR_CONFIGS)
|
| 69 |
+
|
| 70 |
+
spaces.aoti_apply(compile_transformer(), pipeline.transformer)
|