Spaces:
Running on Zero
Running on Zero
qiaochanghao commited on
Commit ·
484e146
1
Parent(s): 3c8d247
update to firered1.1
Browse files- app.py +258 -122
- examples/makeup1.png +3 -0
- examples/makeup2.png +3 -0
- examples/master1.png +3 -0
- examples/master2.png +3 -0
- examples/master3_1.png +3 -0
- examples/master3_2.png +3 -0
- examples/master4_1.png +3 -0
- examples/master4_2.png +3 -0
- examples/text1_1.png +3 -0
- examples/text1_2.png +3 -0
- examples/text2_1.png +3 -0
- examples/text2_2.png +3 -0
- prompt_augment.py +1 -1
- requirements.txt +6 -1
app.py
CHANGED
|
@@ -3,86 +3,188 @@ import numpy as np
|
|
| 3 |
import random
|
| 4 |
import torch
|
| 5 |
import spaces
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
from PIL import Image
|
| 8 |
from diffusers import QwenImageEditPlusPipeline
|
| 9 |
-
|
| 10 |
-
import os
|
| 11 |
-
import base64
|
| 12 |
-
import json
|
| 13 |
|
| 14 |
from huggingface_hub import login
|
| 15 |
from prompt_augment import PromptAugment
|
| 16 |
login(token=os.environ.get('hf'))
|
| 17 |
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
# --- Model Loading ---
|
| 22 |
dtype = torch.bfloat16
|
| 23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
prompt_handler = PromptAugment()
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
# --- Main Inference Function (with hardcoded negative prompt) ---
|
| 33 |
@spaces.GPU(duration=180)
|
| 34 |
def infer(
|
| 35 |
-
|
| 36 |
prompt,
|
|
|
|
| 37 |
seed=42,
|
| 38 |
randomize_seed=False,
|
| 39 |
-
true_guidance_scale=
|
| 40 |
-
num_inference_steps=
|
| 41 |
height=None,
|
| 42 |
width=None,
|
| 43 |
-
rewrite_prompt=
|
| 44 |
num_images_per_prompt=1,
|
| 45 |
progress=gr.Progress(track_tqdm=True),
|
| 46 |
):
|
| 47 |
-
"""
|
| 48 |
-
Generates an image using the local Qwen-Image diffusers pipeline.
|
| 49 |
-
"""
|
| 50 |
-
# Hardcode the negative prompt as requested
|
| 51 |
negative_prompt = " "
|
| 52 |
|
| 53 |
if randomize_seed:
|
| 54 |
seed = random.randint(0, MAX_SEED)
|
| 55 |
|
| 56 |
-
# Set up the generator for reproducibility
|
| 57 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 58 |
|
| 59 |
-
|
| 60 |
pil_images = []
|
| 61 |
-
if
|
| 62 |
-
for item in
|
| 63 |
try:
|
| 64 |
-
if isinstance(item
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
continue
|
| 72 |
-
|
| 73 |
-
if height
|
| 74 |
-
height
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
if rewrite_prompt and len(pil_images) > 0:
|
| 79 |
-
# prompt = polish_prompt(prompt, pil_images[0])
|
| 80 |
prompt = prompt_handler.predict(prompt, [pil_images[0]])
|
| 81 |
print(f"Rewritten Prompt: {prompt}")
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
| 86 |
image=pil_images if len(pil_images) > 0 else None,
|
| 87 |
prompt=prompt,
|
| 88 |
height=height,
|
|
@@ -90,116 +192,150 @@ def infer(
|
|
| 90 |
negative_prompt=negative_prompt,
|
| 91 |
num_inference_steps=num_inference_steps,
|
| 92 |
generator=generator,
|
|
|
|
| 93 |
true_cfg_scale=true_guidance_scale,
|
| 94 |
num_images_per_prompt=num_images_per_prompt,
|
| 95 |
).images
|
| 96 |
|
| 97 |
-
return
|
| 98 |
-
|
| 99 |
-
# --- Examples and UI Layout ---
|
| 100 |
-
examples = []
|
| 101 |
|
| 102 |
css = """
|
| 103 |
-
#col-container {
|
| 104 |
-
|
| 105 |
-
max-width: 1024px;
|
| 106 |
-
}
|
| 107 |
-
#edit_text{margin-top: -62px !important}
|
| 108 |
"""
|
| 109 |
|
|
|
|
|
|
|
| 110 |
def get_image_base64(image_path):
|
| 111 |
with open(image_path, "rb") as img_file:
|
| 112 |
return base64.b64encode(img_file.read()).decode('utf-8')
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
| 115 |
with gr.Blocks(css=css) as demo:
|
| 116 |
with gr.Column(elem_id="col-container"):
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
| 119 |
with gr.Row():
|
| 120 |
-
with gr.Column():
|
| 121 |
-
input_images = gr.Gallery(
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
with gr.Accordion("Advanced Settings", open=False):
|
| 135 |
-
# Negative prompt UI element is removed here
|
| 136 |
-
|
| 137 |
-
seed = gr.Slider(
|
| 138 |
-
label="Seed",
|
| 139 |
-
minimum=0,
|
| 140 |
-
maximum=MAX_SEED,
|
| 141 |
-
step=1,
|
| 142 |
-
value=0,
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 146 |
-
|
| 147 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
)
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
gr.on(
|
| 187 |
triggers=[run_button.click, prompt.submit],
|
| 188 |
fn=infer,
|
| 189 |
inputs=[
|
| 190 |
input_images,
|
| 191 |
-
prompt,
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
true_guidance_scale,
|
| 195 |
-
num_inference_steps,
|
| 196 |
-
height,
|
| 197 |
-
width,
|
| 198 |
-
rewrite_prompt,
|
| 199 |
],
|
| 200 |
outputs=[result, seed],
|
| 201 |
)
|
| 202 |
|
| 203 |
if __name__ == "__main__":
|
| 204 |
-
|
| 205 |
-
demo.launch(allowed_paths=["./"])
|
|
|
|
| 3 |
import random
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
+
import os
|
| 7 |
+
import base64
|
| 8 |
+
import math
|
| 9 |
|
| 10 |
from PIL import Image
|
| 11 |
from diffusers import QwenImageEditPlusPipeline
|
| 12 |
+
from pillow_heif import register_heif_opener
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
from huggingface_hub import login
|
| 15 |
from prompt_augment import PromptAugment
|
| 16 |
login(token=os.environ.get('hf'))
|
| 17 |
|
| 18 |
+
register_heif_opener()
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
dtype = torch.bfloat16
|
| 21 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 22 |
|
| 23 |
+
pipe = QwenImageEditPlusPipeline.from_pretrained(
|
| 24 |
+
"FireRedTeam/FireRed-Image-Edit-1.1",
|
| 25 |
+
torch_dtype=dtype
|
| 26 |
+
).to(device)
|
| 27 |
+
|
| 28 |
prompt_handler = PromptAugment()
|
| 29 |
|
| 30 |
+
ADAPTER_SPECS = {
|
| 31 |
+
"Covercraft": {
|
| 32 |
+
"repo": "FireRedTeam/FireRed-Image-Edit-LoRA-Zoo",
|
| 33 |
+
"weights": "FireRed-Image-Edit-Covercraft.safetensors",
|
| 34 |
+
"adapter_name": "covercraft",
|
| 35 |
+
},
|
| 36 |
+
"Lightning": {
|
| 37 |
+
"repo": "FireRedTeam/FireRed-Image-Edit-LoRA-Zoo",
|
| 38 |
+
"weights": "FireRed-Image-Edit-Lightning-8steps-v1.0.safetensors",
|
| 39 |
+
"adapter_name": "lightning",
|
| 40 |
+
},
|
| 41 |
+
"Makeup": {
|
| 42 |
+
"repo": "FireRedTeam/FireRed-Image-Edit-LoRA-Zoo",
|
| 43 |
+
"weights": "FireRed-Image-Edit-Makeup.safetensors",
|
| 44 |
+
"adapter_name": "makeup",
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
LOADED_ADAPTERS = set()
|
| 49 |
+
LORA_OPTIONS = ["None"] + list(ADAPTER_SPECS.keys())
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def load_lora(lora_name):
|
| 53 |
+
"""加载并激活指定的 LoRA"""
|
| 54 |
+
if lora_name == "None" or not lora_name:
|
| 55 |
+
if LOADED_ADAPTERS:
|
| 56 |
+
pipe.set_adapters([], adapter_weights=[])
|
| 57 |
+
return
|
| 58 |
+
|
| 59 |
+
spec = ADAPTER_SPECS.get(lora_name)
|
| 60 |
+
if not spec:
|
| 61 |
+
raise gr.Error(f"LoRA 配置未找到: {lora_name}")
|
| 62 |
+
|
| 63 |
+
adapter_name = spec["adapter_name"]
|
| 64 |
+
|
| 65 |
+
if adapter_name not in LOADED_ADAPTERS:
|
| 66 |
+
print(f"--- Downloading and Loading Adapter: {lora_name} ---")
|
| 67 |
+
try:
|
| 68 |
+
pipe.load_lora_weights(
|
| 69 |
+
spec["repo"],
|
| 70 |
+
weight_name=spec["weights"],
|
| 71 |
+
adapter_name=adapter_name
|
| 72 |
+
)
|
| 73 |
+
LOADED_ADAPTERS.add(adapter_name)
|
| 74 |
+
except Exception as e:
|
| 75 |
+
raise gr.Error(f"Failed to load adapter {lora_name}: {e}")
|
| 76 |
+
else:
|
| 77 |
+
print(f"--- Adapter {lora_name} is already loaded ---")
|
| 78 |
+
|
| 79 |
+
pipe.set_adapters([adapter_name], adapter_weights=[1.0])
|
| 80 |
+
|
| 81 |
+
|
| 82 |
MAX_SEED = np.iinfo(np.int32).max
|
| 83 |
+
MAX_INPUT_IMAGES = 3
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def limit_images(images):
|
| 87 |
+
if images is None:
|
| 88 |
+
return None
|
| 89 |
+
if len(images) > MAX_INPUT_IMAGES:
|
| 90 |
+
gr.Info(f"最多支持 {MAX_INPUT_IMAGES} 张图片,已自动移除多余图片")
|
| 91 |
+
return images[:MAX_INPUT_IMAGES]
|
| 92 |
+
return images
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def calculate_dimensions(target_area, ratio):
|
| 96 |
+
width = math.sqrt(target_area * ratio)
|
| 97 |
+
height = width / ratio
|
| 98 |
+
width = round(width / 32) * 32
|
| 99 |
+
height = round(height / 32) * 32
|
| 100 |
+
return int(width), int(height)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def update_dimensions_on_upload(images, max_area=1024*1024):
|
| 104 |
+
if images is None or len(images) == 0:
|
| 105 |
+
return 0, 0
|
| 106 |
+
|
| 107 |
+
try:
|
| 108 |
+
first_item = images[0]
|
| 109 |
+
if isinstance(first_item, tuple):
|
| 110 |
+
img = first_item[0]
|
| 111 |
+
else:
|
| 112 |
+
img = first_item
|
| 113 |
+
|
| 114 |
+
if isinstance(img, Image.Image):
|
| 115 |
+
pil_img = img
|
| 116 |
+
elif isinstance(img, str):
|
| 117 |
+
pil_img = Image.open(img)
|
| 118 |
+
else:
|
| 119 |
+
return 0, 0
|
| 120 |
+
|
| 121 |
+
h, w = pil_img.height, pil_img.width
|
| 122 |
+
is_multi_image = len(images) > 1
|
| 123 |
+
|
| 124 |
+
if not is_multi_image:
|
| 125 |
+
return 0, 0
|
| 126 |
+
|
| 127 |
+
ratio = w / h
|
| 128 |
+
new_w, new_h = calculate_dimensions(max_area, ratio)
|
| 129 |
+
return new_h, new_w
|
| 130 |
+
except Exception as e:
|
| 131 |
+
print(f"获取图片尺寸失败: {e}")
|
| 132 |
+
return 0, 0
|
| 133 |
+
|
| 134 |
|
|
|
|
| 135 |
@spaces.GPU(duration=180)
|
| 136 |
def infer(
|
| 137 |
+
input_images,
|
| 138 |
prompt,
|
| 139 |
+
lora_choice,
|
| 140 |
seed=42,
|
| 141 |
randomize_seed=False,
|
| 142 |
+
true_guidance_scale=4.0,
|
| 143 |
+
num_inference_steps=40,
|
| 144 |
height=None,
|
| 145 |
width=None,
|
| 146 |
+
rewrite_prompt=False,
|
| 147 |
num_images_per_prompt=1,
|
| 148 |
progress=gr.Progress(track_tqdm=True),
|
| 149 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
negative_prompt = " "
|
| 151 |
|
| 152 |
if randomize_seed:
|
| 153 |
seed = random.randint(0, MAX_SEED)
|
| 154 |
|
|
|
|
| 155 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 156 |
|
| 157 |
+
load_lora(lora_choice)
|
| 158 |
pil_images = []
|
| 159 |
+
if input_images is not None:
|
| 160 |
+
for item in input_images[:MAX_INPUT_IMAGES]:
|
| 161 |
try:
|
| 162 |
+
if isinstance(item, tuple):
|
| 163 |
+
img = item[0]
|
| 164 |
+
else:
|
| 165 |
+
img = item
|
| 166 |
+
|
| 167 |
+
if isinstance(img, Image.Image):
|
| 168 |
+
pil_images.append(img.convert("RGB"))
|
| 169 |
+
elif isinstance(img, str):
|
| 170 |
+
pil_images.append(Image.open(img).convert("RGB"))
|
| 171 |
+
except Exception as e:
|
| 172 |
+
print(f"处理图片出错: {e}")
|
| 173 |
continue
|
| 174 |
+
|
| 175 |
+
if height == 0:
|
| 176 |
+
height = None
|
| 177 |
+
if width == 0:
|
| 178 |
+
width = None
|
| 179 |
+
|
| 180 |
if rewrite_prompt and len(pil_images) > 0:
|
|
|
|
| 181 |
prompt = prompt_handler.predict(prompt, [pil_images[0]])
|
| 182 |
print(f"Rewritten Prompt: {prompt}")
|
| 183 |
|
| 184 |
+
if pil_images:
|
| 185 |
+
for i, img in enumerate(pil_images):
|
| 186 |
+
print(f" [{i}] size: {img.width}x{img.height}")
|
| 187 |
+
images = pipe(
|
| 188 |
image=pil_images if len(pil_images) > 0 else None,
|
| 189 |
prompt=prompt,
|
| 190 |
height=height,
|
|
|
|
| 192 |
negative_prompt=negative_prompt,
|
| 193 |
num_inference_steps=num_inference_steps,
|
| 194 |
generator=generator,
|
| 195 |
+
guidance_scale=1.0,
|
| 196 |
true_cfg_scale=true_guidance_scale,
|
| 197 |
num_images_per_prompt=num_images_per_prompt,
|
| 198 |
).images
|
| 199 |
|
| 200 |
+
return images, seed
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
css = """
|
| 203 |
+
#col-container { margin: 0 auto; max-width: 1200px; }
|
| 204 |
+
#edit-btn { height: 100% !important; min-height: 42px; }
|
|
|
|
|
|
|
|
|
|
| 205 |
"""
|
| 206 |
|
| 207 |
+
|
| 208 |
+
|
| 209 |
def get_image_base64(image_path):
|
| 210 |
with open(image_path, "rb") as img_file:
|
| 211 |
return base64.b64encode(img_file.read()).decode('utf-8')
|
| 212 |
+
|
| 213 |
+
|
| 214 |
+
logo_base64 = get_image_base64("logo.png") if os.path.exists("logo.png") else None
|
| 215 |
|
| 216 |
with gr.Blocks(css=css) as demo:
|
| 217 |
with gr.Column(elem_id="col-container"):
|
| 218 |
+
if logo_base64:
|
| 219 |
+
gr.HTML(f'<img src="data:image/png;base64,{logo_base64}" alt="FireRed Logo" width="400" style="display: block; margin: 0 auto;">')
|
| 220 |
+
else:
|
| 221 |
+
gr.Markdown("# FireRed Image Edit")
|
| 222 |
+
gr.Markdown(f"[Learn more](https://github.com/FireRedTeam/FireRed-Image-Edit) about the FireRed-Image-Edit series. Supports multi-image input (up to {MAX_INPUT_IMAGES} images.)")
|
| 223 |
with gr.Row():
|
| 224 |
+
with gr.Column(scale=1):
|
| 225 |
+
input_images = gr.Gallery(
|
| 226 |
+
label="Upload Images",
|
| 227 |
+
type="pil",
|
| 228 |
+
interactive=True,
|
| 229 |
+
height=300,
|
| 230 |
+
columns=3,
|
| 231 |
+
object_fit="contain",
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
with gr.Column(scale=1):
|
| 235 |
+
result = gr.Gallery(
|
| 236 |
+
label="Output Images",
|
| 237 |
+
type="pil",
|
| 238 |
+
height=300,
|
| 239 |
+
columns=2,
|
| 240 |
+
object_fit="contain",
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
prompt = gr.Textbox(
|
| 244 |
+
label="Edit Prompt",
|
| 245 |
+
placeholder="e.g., transform into anime..",
|
| 246 |
+
)
|
| 247 |
+
|
| 248 |
+
with gr.Row(equal_height=True):
|
| 249 |
+
with gr.Column(scale=5):
|
| 250 |
+
lora_choice = gr.Dropdown(
|
| 251 |
+
label="Choose Lora",
|
| 252 |
+
choices=LORA_OPTIONS,
|
| 253 |
+
value=LORA_OPTIONS[0] if LORA_OPTIONS else "None",
|
| 254 |
+
)
|
| 255 |
+
with gr.Column(scale=4):
|
| 256 |
+
run_button = gr.Button("Edit Image", variant="primary", elem_id="edit-btn")
|
| 257 |
|
| 258 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
with gr.Row():
|
| 260 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42)
|
| 261 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 262 |
+
|
| 263 |
+
with gr.Row():
|
| 264 |
+
true_guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=4.0)
|
| 265 |
+
num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=40)
|
| 266 |
+
|
| 267 |
+
with gr.Row():
|
| 268 |
+
height = gr.Slider(label="Height (0=auto)", minimum=0, maximum=2048, step=8, value=0)
|
| 269 |
+
width = gr.Slider(label="Width (0=auto)", minimum=0, maximum=2048, step=8, value=0)
|
| 270 |
+
|
| 271 |
+
with gr.Row():
|
| 272 |
+
rewrite_prompt = gr.Checkbox(label="Rewrite Prompt", value=False)
|
| 273 |
+
num_images_per_prompt = gr.Slider(label="Num Images", minimum=1, maximum=4, step=1, value=1)
|
| 274 |
|
| 275 |
+
# Examples
|
| 276 |
+
gr.Examples(
|
| 277 |
+
examples=[
|
| 278 |
+
[["examples/master1.png"], "将背景换为带自然光效的浅蓝色,身穿浅米色蕾丝领上衣,将发型改为右侧佩戴精致珍珠发夹,同时单手向前抬起握着一把宝剑,另一只手自然摆放。面部微笑。", "None"],
|
| 279 |
+
[["examples/master2.png"], "替换背景为盛开的樱花树场景;更换衣服为黑色西装,为人物添加单肩蓝色书包,单手抓住包带。头发变为高马尾。色调明亮。蹲下。", "None"],
|
| 280 |
+
[["examples/master3_1.png", "examples/master3_2.png"], "把图1中的模特换成图2里的长裙和高帮帆布鞋,保持原有姿态和配饰,整体风格统一。", "None"],
|
| 281 |
+
[["examples/master4_1.png", "examples/master4_2.png"], "把图1中的白色衬衫和棕色半裙,换成图2里的灰褐色连帽卫衣、黑色侧边条纹裤、卡其色工装靴和同色云朵包,保持模特姿态和背景不变。", "None"],
|
| 282 |
+
[["examples/makeup1.png"], "为人物添加纯欲厌世妆:使用冷白皮哑光粉底均匀肤色,描绘细挑的灰黑色野生眉,眼部晕染浅灰调眼影并加深眼尾,画出上扬的黑色眼线,粘贴浓密卷翘的假睫毛,在眼头和卧蚕处提亮,涂抹深紫调哑光口红并勾勒唇形,在颧骨处扫上浅粉腮红,鼻梁和眉骨处打高光,下颌线处轻扫阴影。", "Makeup"],
|
| 283 |
+
[["examples/makeup2.png"], "为人物添加妆容:使用象牙白哑光粉底均匀肤色,描绘细长柳叶眉并填充浅棕色,眼部晕染浅棕色眼影并加深眼尾,画出自然黑色眼线,粘贴浓密假睫毛,用浅棕色眼影提亮卧蚕;涂抹豆沙色哑光口红并勾勒唇形,在两颊扫上浅粉色腮红,在鼻梁和颧骨处轻扫高光,在面部轮廓处轻扫阴影。", "Makeup"],
|
| 284 |
+
[["examples/text1_1.png", "examples/text1_2.png"], "请在图1添加主标题文本 “谁说我们丑了”,字体样式参考图2中主标题《人!给我开个罐罐》;主标题整体采用横向排版多行错落(非严格对齐),置于图片左下角;在狗狗右下方、贴近前爪附近添加一个手绘“爱心”涂鸦贴纸;增加鱼眼镜头效果", "Covercraft"],
|
| 285 |
+
[["examples/text2_1.png", "examples/text2_2.png"], "请在图1添加主标题文本 “崽子第一次玩冰”,副标题“坐标:东南休闲公园”,主标题和副标题的字体样式参考图2中主标题“无露营不冬天”,主标题整体采用横向排版多行,主标题添加在画面左侧上方;副标题添加在画面左侧下方,字的层级更小,避免修改和遮挡图1主体关键信息(人物/核心景物)和画面中心。", "Covercraft"],
|
| 286 |
+
],
|
| 287 |
+
inputs=[input_images, prompt, lora_choice],
|
| 288 |
+
outputs=[result, seed],
|
| 289 |
+
fn=infer,
|
| 290 |
+
cache_examples=False,
|
| 291 |
+
label="Examples"
|
| 292 |
+
)
|
| 293 |
|
| 294 |
+
# 监听 LoRA 选择变化:Lightning 时锁定参数
|
| 295 |
+
def on_lora_change(lora_name):
|
| 296 |
+
if lora_name == "Lightning":
|
| 297 |
+
return (
|
| 298 |
+
gr.update(value=8, interactive=False), # num_inference_steps
|
| 299 |
+
gr.update(value=1.0, interactive=False), # true_guidance_scale
|
| 300 |
+
gr.update(value=0, interactive=True), # seed
|
| 301 |
+
gr.update(value=False, interactive=False), # randomize_seed
|
| 302 |
+
)
|
| 303 |
+
else:
|
| 304 |
+
return (
|
| 305 |
+
gr.update(value=40, interactive=True), # num_inference_steps
|
| 306 |
+
gr.update(value=4.0, interactive=True), # true_guidance_scale
|
| 307 |
+
gr.update(value=42, interactive=True), # seed
|
| 308 |
+
gr.update(value=True, interactive=True), # randomize_seed
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
lora_choice.change(
|
| 312 |
+
fn=on_lora_change,
|
| 313 |
+
inputs=[lora_choice],
|
| 314 |
+
outputs=[num_inference_steps, true_guidance_scale, seed, randomize_seed],
|
| 315 |
+
)
|
| 316 |
+
def on_image_upload(images):
|
| 317 |
+
limited = limit_images(images)
|
| 318 |
+
h, w = update_dimensions_on_upload(limited)
|
| 319 |
+
return limited, h, w
|
| 320 |
+
|
| 321 |
+
input_images.upload(
|
| 322 |
+
fn=on_image_upload,
|
| 323 |
+
inputs=[input_images],
|
| 324 |
+
outputs=[input_images, height, width],
|
| 325 |
+
)
|
| 326 |
|
| 327 |
gr.on(
|
| 328 |
triggers=[run_button.click, prompt.submit],
|
| 329 |
fn=infer,
|
| 330 |
inputs=[
|
| 331 |
input_images,
|
| 332 |
+
prompt, lora_choice, seed, randomize_seed,
|
| 333 |
+
true_guidance_scale, num_inference_steps,
|
| 334 |
+
height, width, rewrite_prompt, num_images_per_prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
],
|
| 336 |
outputs=[result, seed],
|
| 337 |
)
|
| 338 |
|
| 339 |
if __name__ == "__main__":
|
| 340 |
+
demo.queue()
|
| 341 |
+
demo.launch(allowed_paths=["./"])
|
examples/makeup1.png
ADDED
|
Git LFS Details
|
examples/makeup2.png
ADDED
|
Git LFS Details
|
examples/master1.png
ADDED
|
Git LFS Details
|
examples/master2.png
ADDED
|
Git LFS Details
|
examples/master3_1.png
ADDED
|
Git LFS Details
|
examples/master3_2.png
ADDED
|
Git LFS Details
|
examples/master4_1.png
ADDED
|
Git LFS Details
|
examples/master4_2.png
ADDED
|
Git LFS Details
|
examples/text1_1.png
ADDED
|
Git LFS Details
|
examples/text1_2.png
ADDED
|
Git LFS Details
|
examples/text2_1.png
ADDED
|
Git LFS Details
|
examples/text2_2.png
ADDED
|
Git LFS Details
|
prompt_augment.py
CHANGED
|
@@ -180,7 +180,7 @@ Please strictly follow the rewriting rules below:
|
|
| 180 |
|
| 181 |
def predict(self, original_prompt, img_list=[]):
|
| 182 |
api_key = os.environ.get('DASH_API_KEY')
|
| 183 |
-
model="qwen3-vl-235b-a22b-
|
| 184 |
language = contains_chinese(original_prompt)
|
| 185 |
original_prompt = original_prompt.strip()
|
| 186 |
if language == 'zh':
|
|
|
|
| 180 |
|
| 181 |
def predict(self, original_prompt, img_list=[]):
|
| 182 |
api_key = os.environ.get('DASH_API_KEY')
|
| 183 |
+
model="qwen3-vl-235b-a22b-instruct"
|
| 184 |
language = contains_chinese(original_prompt)
|
| 185 |
original_prompt = original_prompt.strip()
|
| 186 |
if language == 'zh':
|
requirements.txt
CHANGED
|
@@ -5,4 +5,9 @@ safetensors
|
|
| 5 |
sentencepiece
|
| 6 |
dashscope
|
| 7 |
kernels
|
| 8 |
-
torchvision
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
sentencepiece
|
| 6 |
dashscope
|
| 7 |
kernels
|
| 8 |
+
torchvision
|
| 9 |
+
invisible_watermark
|
| 10 |
+
torch
|
| 11 |
+
xformers
|
| 12 |
+
pillow_heif
|
| 13 |
+
peft
|