Update app.py
Browse files
app.py
CHANGED
|
@@ -1,246 +1,358 @@
|
|
| 1 |
-
import os
|
| 2 |
-
# Nouveau nom d'env torch (l'ancien est déprécié)
|
| 3 |
-
os.environ.setdefault("PYTORCH_ALLOC_CONF", "expandable_segments:True")
|
| 4 |
-
# Évite plusieurs workers concurrent → moins de VRAM surprises
|
| 5 |
-
os.environ.setdefault("GRADIO_NUM_WORKERS", "1")
|
| 6 |
-
|
| 7 |
-
import sys
|
| 8 |
sys.path.append('../')
|
| 9 |
|
| 10 |
import spaces
|
|
|
|
| 11 |
import torch
|
| 12 |
import random
|
| 13 |
import numpy as np
|
| 14 |
from PIL import Image
|
| 15 |
-
import gradio as gr
|
| 16 |
|
|
|
|
| 17 |
from huggingface_hub import hf_hub_download
|
| 18 |
from transformers import AutoModelForImageSegmentation
|
| 19 |
from torchvision import transforms
|
|
|
|
| 20 |
from pipeline import InstantCharacterFluxPipeline
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
#
|
| 24 |
-
#
|
| 25 |
MAX_SEED = np.iinfo(np.int32).max
|
| 26 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 27 |
-
dtype = torch.float16
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# IP-Adapter (≈5.6 Go)
|
| 52 |
-
ip_adapter_path = _dl("tencent/InstantCharacter", "instantcharacter_ip-adapter.bin", HF_TOKEN)
|
| 53 |
|
| 54 |
-
# >>>
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
#
|
| 60 |
-
#
|
| 61 |
pipe = InstantCharacterFluxPipeline.from_pretrained(
|
| 62 |
-
base_model,
|
| 63 |
-
torch_dtype=dtype,
|
| 64 |
-
token=HF_TOKEN,
|
| 65 |
-
low_cpu_mem_usage=True, # réduit le pic RAM au chargement
|
| 66 |
)
|
| 67 |
-
# Tout sur GPU (L40S a de la marge)
|
| 68 |
pipe.to(device)
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
try:
|
| 72 |
-
pipe.enable_xformers_memory_efficient_attention()
|
| 73 |
-
except Exception:
|
| 74 |
-
pass
|
| 75 |
-
|
| 76 |
-
pipe.set_progress_bar_config(disable=True)
|
| 77 |
-
if hasattr(pipe, "vae"):
|
| 78 |
-
if hasattr(pipe.vae, "enable_slicing"):
|
| 79 |
-
pipe.vae.enable_slicing()
|
| 80 |
-
if hasattr(pipe.vae, "enable_tiling"):
|
| 81 |
-
pipe.vae.enable_tiling()
|
| 82 |
-
|
| 83 |
-
# Adapter avec 2 encodeurs (le 2e est 'dinov2-base', léger)
|
| 84 |
pipe.init_adapter(
|
| 85 |
image_encoder_path=image_encoder_path,
|
| 86 |
image_encoder_2_path=image_encoder_2_path,
|
| 87 |
-
subject_ipadapter_cfg=dict(
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
#
|
| 92 |
-
#
|
| 93 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 94 |
-
birefnet_path, trust_remote_code=True
|
| 95 |
)
|
| 96 |
-
birefnet.to(
|
| 97 |
-
|
| 98 |
birefnet_transform_image = transforms.Compose([
|
| 99 |
transforms.Resize((1024, 1024)),
|
| 100 |
transforms.ToTensor(),
|
| 101 |
-
transforms.Normalize(
|
|
|
|
|
|
|
|
|
|
| 102 |
])
|
| 103 |
|
| 104 |
def remove_bkg(subject_image):
|
| 105 |
def infer_matting(img_pil):
|
| 106 |
-
|
|
|
|
|
|
|
| 107 |
with torch.no_grad():
|
| 108 |
-
preds = birefnet(
|
| 109 |
pred = preds[0].squeeze()
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
def get_bbox_from_mask(mask, th=128):
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
if H == W:
|
| 124 |
return image
|
| 125 |
-
|
| 126 |
-
|
|
|
|
| 127 |
if H > W:
|
| 128 |
-
pad_param = ((0, 0), (
|
| 129 |
else:
|
| 130 |
-
pad_param = ((
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
# =====================================================
|
| 147 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
-
# =====================================================
|
| 151 |
-
# GÉNÉRATION D'IMAGE
|
| 152 |
-
# =====================================================
|
| 153 |
@spaces.GPU
|
| 154 |
def create_image(
|
| 155 |
input_image,
|
| 156 |
prompt,
|
| 157 |
-
scale,
|
| 158 |
guidance_scale,
|
| 159 |
num_inference_steps,
|
| 160 |
seed,
|
| 161 |
-
|
| 162 |
-
width=896,
|
| 163 |
-
height=896,
|
| 164 |
):
|
| 165 |
-
|
| 166 |
-
raise gr.Error("Merci d'uploader une image de visage.")
|
| 167 |
-
|
| 168 |
-
if not os.path.exists(onepiece_flux_lora_path):
|
| 169 |
-
raise gr.Error(f"Fichier LoRA introuvable : {onepiece_flux_lora_path}. "
|
| 170 |
-
f"Place-le à la racine du Space.")
|
| 171 |
-
|
| 172 |
-
# Détourage/crop du sujet (CPU)
|
| 173 |
input_image = remove_bkg(input_image)
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
return images
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
#
|
| 196 |
-
#
|
| 197 |
-
title = "
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
with block:
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
| 209 |
with gr.Row():
|
| 210 |
with gr.Column():
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
prompt = gr.Textbox(
|
| 213 |
label="Prompt",
|
| 214 |
-
value="
|
| 215 |
)
|
| 216 |
-
scale = gr.Slider(0.0, 1.5, 1.0, 0.01, label="Scale (Face/ID strength)")
|
| 217 |
-
lora_strength = gr.Slider(0.0, 1.5, 0.85, 0.05, label="LoRA strength (One Piece)")
|
| 218 |
|
| 219 |
-
|
| 220 |
-
guidance_scale = gr.Slider(1.0, 7.0, 3.5, 0.1, label="Guidance scale")
|
| 221 |
-
num_inference_steps = gr.Slider(5, 50, 28, 1, label="Inference steps")
|
| 222 |
-
seed = gr.Slider(-MAX_SEED, MAX_SEED, 123456, 1, label="Seed")
|
| 223 |
-
randomize_seed = gr.Checkbox(value=True, label="Randomize seed")
|
| 224 |
-
width = gr.Slider(704, 1152, 896, 32, label="Width")
|
| 225 |
-
height = gr.Slider(704, 1152, 896, 32, label="Height")
|
| 226 |
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
with gr.Column():
|
| 230 |
-
|
| 231 |
|
| 232 |
generate_button.click(
|
| 233 |
fn=randomize_seed_fn,
|
| 234 |
inputs=[seed, randomize_seed],
|
| 235 |
outputs=seed,
|
| 236 |
queue=False,
|
|
|
|
| 237 |
).then(
|
| 238 |
fn=create_image,
|
| 239 |
inputs=[
|
| 240 |
-
image_pil,
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
],
|
| 243 |
-
outputs=
|
| 244 |
)
|
| 245 |
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys, os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
sys.path.append('../')
|
| 3 |
|
| 4 |
import spaces
|
| 5 |
+
|
| 6 |
import torch
|
| 7 |
import random
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
|
|
|
| 10 |
|
| 11 |
+
import gradio as gr
|
| 12 |
from huggingface_hub import hf_hub_download
|
| 13 |
from transformers import AutoModelForImageSegmentation
|
| 14 |
from torchvision import transforms
|
| 15 |
+
|
| 16 |
from pipeline import InstantCharacterFluxPipeline
|
| 17 |
|
| 18 |
+
# ----------------------------
|
| 19 |
+
# Global
|
| 20 |
+
# ----------------------------
|
| 21 |
MAX_SEED = np.iinfo(np.int32).max
|
| 22 |
+
device = "cuda" if torch.cuda.is_available() else torch.device("cpu")
|
| 23 |
+
dtype = torch.float16 if "cuda" in str(device) else torch.float32
|
| 24 |
+
|
| 25 |
+
# ----------------------------
|
| 26 |
+
# Pre-trained / assets paths
|
| 27 |
+
# ----------------------------
|
| 28 |
+
ip_adapter_path = hf_hub_download(
|
| 29 |
+
repo_id="tencent/InstantCharacter",
|
| 30 |
+
filename="instantcharacter_ip-adapter.bin"
|
| 31 |
+
)
|
| 32 |
+
base_model = 'black-forest-labs/FLUX.1-dev'
|
| 33 |
+
image_encoder_path = 'google/siglip-so400m-patch14-384'
|
| 34 |
+
image_encoder_2_path = 'facebook/dinov2-giant'
|
| 35 |
+
birefnet_path = 'ZhengPeng7/BiRefNet'
|
| 36 |
+
|
| 37 |
+
# Styles LoRA (existants)
|
| 38 |
+
makoto_style_lora_path = hf_hub_download(
|
| 39 |
+
repo_id="InstantX/FLUX.1-dev-LoRA-Makoto-Shinkai",
|
| 40 |
+
filename="Makoto_Shinkai_style.safetensors"
|
| 41 |
+
)
|
| 42 |
+
ghibli_style_lora_path = hf_hub_download(
|
| 43 |
+
repo_id="InstantX/FLUX.1-dev-LoRA-Ghibli",
|
| 44 |
+
filename="ghibli_style.safetensors"
|
| 45 |
+
)
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# >>> NEW: One Piece LoRA (fichier local dans ton Space)
|
| 48 |
+
# Place le fichier à la racine (comme sur ta capture).
|
| 49 |
+
onepiece_style_lora_path = os.path.join(
|
| 50 |
+
os.path.dirname(__file__),
|
| 51 |
+
"onepiece_flux_v2.safetensors"
|
| 52 |
+
)
|
| 53 |
+
ONEPIECE_TRIGGER = "onepiece style"
|
| 54 |
|
| 55 |
+
# ----------------------------
|
| 56 |
+
# Init pipeline
|
| 57 |
+
# ----------------------------
|
| 58 |
pipe = InstantCharacterFluxPipeline.from_pretrained(
|
| 59 |
+
base_model, torch_dtype=torch.bfloat16
|
|
|
|
|
|
|
|
|
|
| 60 |
)
|
|
|
|
| 61 |
pipe.to(device)
|
| 62 |
|
| 63 |
+
# InstantCharacter adapters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
pipe.init_adapter(
|
| 65 |
image_encoder_path=image_encoder_path,
|
| 66 |
image_encoder_2_path=image_encoder_2_path,
|
| 67 |
+
subject_ipadapter_cfg=dict(
|
| 68 |
+
subject_ip_adapter_path=ip_adapter_path,
|
| 69 |
+
nb_token=1024
|
| 70 |
+
),
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# ----------------------------
|
| 74 |
+
# Matting model (background removal)
|
| 75 |
+
# ----------------------------
|
| 76 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 77 |
+
birefnet_path, trust_remote_code=True
|
| 78 |
)
|
| 79 |
+
birefnet.to('cuda' if torch.cuda.is_available() else device)
|
| 80 |
+
birefnet.eval()
|
| 81 |
birefnet_transform_image = transforms.Compose([
|
| 82 |
transforms.Resize((1024, 1024)),
|
| 83 |
transforms.ToTensor(),
|
| 84 |
+
transforms.Normalize(
|
| 85 |
+
[0.485, 0.456, 0.406],
|
| 86 |
+
[0.229, 0.224, 0.225]
|
| 87 |
+
)
|
| 88 |
])
|
| 89 |
|
| 90 |
def remove_bkg(subject_image):
|
| 91 |
def infer_matting(img_pil):
|
| 92 |
+
input_images = birefnet_transform_image(img_pil).unsqueeze(0).to(
|
| 93 |
+
'cuda' if torch.cuda.is_available() else device
|
| 94 |
+
)
|
| 95 |
with torch.no_grad():
|
| 96 |
+
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
| 97 |
pred = preds[0].squeeze()
|
| 98 |
+
pred_pil = transforms.ToPILImage()(pred)
|
| 99 |
+
mask = pred_pil.resize(img_pil.size)
|
| 100 |
+
mask = np.array(mask)
|
| 101 |
+
mask = mask[..., None]
|
| 102 |
+
return mask
|
| 103 |
|
| 104 |
def get_bbox_from_mask(mask, th=128):
|
| 105 |
+
height, width = mask.shape[:2]
|
| 106 |
+
x1, y1, x2, y2 = 0, 0, width - 1, height - 1
|
| 107 |
+
|
| 108 |
+
sample = np.max(mask, axis=0)
|
| 109 |
+
for idx in range(width):
|
| 110 |
+
if sample[idx] >= th:
|
| 111 |
+
x1 = idx
|
| 112 |
+
break
|
| 113 |
+
|
| 114 |
+
sample = np.max(mask[:, ::-1], axis=0)
|
| 115 |
+
for idx in range(width):
|
| 116 |
+
if sample[idx] >= th:
|
| 117 |
+
x2 = width - 1 - idx
|
| 118 |
+
break
|
| 119 |
+
|
| 120 |
+
sample = np.max(mask, axis=1)
|
| 121 |
+
for idx in range(height):
|
| 122 |
+
if sample[idx] >= th:
|
| 123 |
+
y1 = idx
|
| 124 |
+
break
|
| 125 |
+
|
| 126 |
+
sample = np.max(mask[::-1], axis=1)
|
| 127 |
+
for idx in range(height):
|
| 128 |
+
if sample[idx] >= th:
|
| 129 |
+
y2 = height - 1 - idx
|
| 130 |
+
break
|
| 131 |
+
|
| 132 |
+
x1 = np.clip(x1, 0, width-1).round().astype(np.int32)
|
| 133 |
+
y1 = np.clip(y1, 0, height-1).round().astype(np.int32)
|
| 134 |
+
x2 = np.clip(x2, 0, width-1).round().astype(np.int32)
|
| 135 |
+
y2 = np.clip(y2, 0, height-1).round().astype(np.int32)
|
| 136 |
+
return [x1, y1, x2, y2]
|
| 137 |
+
|
| 138 |
+
def pad_to_square(image, pad_value=255, random=False):
|
| 139 |
+
H, W = image.shape[0], image.shape[1]
|
| 140 |
if H == W:
|
| 141 |
return image
|
| 142 |
+
padd = abs(H - W)
|
| 143 |
+
padd_1 = int(np.random.randint(0, padd)) if random else int(padd / 2)
|
| 144 |
+
padd_2 = padd - padd_1
|
| 145 |
if H > W:
|
| 146 |
+
pad_param = ((0, 0), (padd_1, padd_2), (0, 0))
|
| 147 |
else:
|
| 148 |
+
pad_param = ((padd_1, padd_2), (0, 0), (0, 0))
|
| 149 |
+
image = np.pad(image, pad_param, 'constant', constant_values=pad_value)
|
| 150 |
+
return image
|
| 151 |
+
|
| 152 |
+
salient_object_mask = infer_matting(subject_image)[..., 0]
|
| 153 |
+
x1, y1, x2, y2 = get_bbox_from_mask(salient_object_mask)
|
| 154 |
+
subject_image_np = np.array(subject_image)
|
| 155 |
+
salient_object_mask[salient_object_mask > 128] = 255
|
| 156 |
+
salient_object_mask[salient_object_mask < 128] = 0
|
| 157 |
+
sample_mask = np.concatenate([salient_object_mask[..., None]]*3, axis=2)
|
| 158 |
+
obj_image = sample_mask / 255 * subject_image_np + (1 - sample_mask / 255) * 255
|
| 159 |
+
crop_obj_image = obj_image[y1:y2, x1:x2]
|
| 160 |
+
crop_pad_obj_image = pad_to_square(crop_obj_image, 255)
|
| 161 |
+
subject_image = Image.fromarray(crop_pad_obj_image.astype(np.uint8))
|
| 162 |
+
return subject_image
|
| 163 |
+
|
|
|
|
| 164 |
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
| 165 |
+
if randomize_seed:
|
| 166 |
+
seed = random.randint(0, MAX_SEED)
|
| 167 |
+
return seed
|
| 168 |
+
|
| 169 |
+
def get_example():
|
| 170 |
+
case = [
|
| 171 |
+
[
|
| 172 |
+
"./assets/girl.jpg",
|
| 173 |
+
"A girl is playing a guitar in street, " + ONEPIECE_TRIGGER,
|
| 174 |
+
0.9,
|
| 175 |
+
'One Piece style',
|
| 176 |
+
],
|
| 177 |
+
[
|
| 178 |
+
"./assets/boy.jpg",
|
| 179 |
+
"A boy is riding a bike in snow, " + ONEPIECE_TRIGGER,
|
| 180 |
+
0.9,
|
| 181 |
+
'One Piece style',
|
| 182 |
+
],
|
| 183 |
+
]
|
| 184 |
+
return case
|
| 185 |
+
|
| 186 |
+
def run_for_examples(source_image, prompt, scale, style_mode):
|
| 187 |
+
return create_image(
|
| 188 |
+
input_image=source_image,
|
| 189 |
+
prompt=prompt,
|
| 190 |
+
scale=scale,
|
| 191 |
+
guidance_scale=3.5,
|
| 192 |
+
num_inference_steps=28,
|
| 193 |
+
seed=123456,
|
| 194 |
+
style_mode=style_mode,
|
| 195 |
+
)
|
| 196 |
|
|
|
|
|
|
|
|
|
|
| 197 |
@spaces.GPU
|
| 198 |
def create_image(
|
| 199 |
input_image,
|
| 200 |
prompt,
|
| 201 |
+
scale,
|
| 202 |
guidance_scale,
|
| 203 |
num_inference_steps,
|
| 204 |
seed,
|
| 205 |
+
style_mode=None
|
|
|
|
|
|
|
| 206 |
):
|
| 207 |
+
# retire le fond automatiquement
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
input_image = remove_bkg(input_image)
|
| 209 |
|
| 210 |
+
if style_mode is None:
|
| 211 |
+
images = pipe(
|
| 212 |
+
prompt=prompt,
|
| 213 |
+
num_inference_steps=num_inference_steps,
|
| 214 |
+
guidance_scale=guidance_scale,
|
| 215 |
+
width=1024,
|
| 216 |
+
height=1024,
|
| 217 |
+
subject_image=input_image,
|
| 218 |
+
subject_scale=scale,
|
| 219 |
+
generator=torch.manual_seed(seed),
|
| 220 |
+
).images
|
| 221 |
+
else:
|
| 222 |
+
# mapping des styles
|
| 223 |
+
if style_mode == 'Makoto Shinkai style':
|
| 224 |
+
lora_file_path = makoto_style_lora_path
|
| 225 |
+
trigger = 'Makoto Shinkai style'
|
| 226 |
+
elif style_mode == 'Ghibli style':
|
| 227 |
+
lora_file_path = ghibli_style_lora_path
|
| 228 |
+
trigger = 'ghibli style'
|
| 229 |
+
elif style_mode == 'One Piece style':
|
| 230 |
+
lora_file_path = onepiece_style_lora_path
|
| 231 |
+
trigger = ONEPIECE_TRIGGER
|
| 232 |
+
else:
|
| 233 |
+
# fallback: pas de LoRA
|
| 234 |
+
lora_file_path = None
|
| 235 |
+
trigger = ""
|
| 236 |
+
|
| 237 |
+
if lora_file_path is None:
|
| 238 |
+
images = pipe(
|
| 239 |
+
prompt=prompt,
|
| 240 |
+
num_inference_steps=num_inference_steps,
|
| 241 |
+
guidance_scale=guidance_scale,
|
| 242 |
+
width=1024,
|
| 243 |
+
height=1024,
|
| 244 |
+
subject_image=input_image,
|
| 245 |
+
subject_scale=scale,
|
| 246 |
+
generator=torch.manual_seed(seed),
|
| 247 |
+
).images
|
| 248 |
+
else:
|
| 249 |
+
images = pipe.with_style_lora(
|
| 250 |
+
lora_file_path=lora_file_path,
|
| 251 |
+
trigger=trigger,
|
| 252 |
+
prompt=prompt,
|
| 253 |
+
num_inference_steps=num_inference_steps,
|
| 254 |
+
guidance_scale=guidance_scale,
|
| 255 |
+
width=1024,
|
| 256 |
+
height=1024,
|
| 257 |
+
subject_image=input_image,
|
| 258 |
+
subject_scale=scale,
|
| 259 |
+
generator=torch.manual_seed(seed),
|
| 260 |
+
).images
|
| 261 |
|
| 262 |
return images
|
| 263 |
|
| 264 |
+
# ----------------------------
|
| 265 |
+
# UI
|
| 266 |
+
# ----------------------------
|
| 267 |
+
title = r"""
|
| 268 |
+
<h1 align="center">InstantCharacter : Personalize Any Characters with a Scalable Diffusion Transformer Framework</h1>
|
| 269 |
+
"""
|
| 270 |
+
|
| 271 |
+
description = r"""
|
| 272 |
+
<b>Official 🤗 Gradio demo</b> for <a href='https://instantcharacter.github.io/' target='_blank'><b>InstantCharacter : Personalize Any Characters with a Scalable Diffusion Transformer Framework</b></a>.<br>
|
| 273 |
+
How to use:<br>
|
| 274 |
+
1. Upload a character image, removing background would be preferred.
|
| 275 |
+
2. Enter a text prompt to describe what you hope the character does.
|
| 276 |
+
3. Choose a style (e.g., <code>One Piece style</code>).
|
| 277 |
+
4. Click <b>Generate Image</b>.
|
| 278 |
+
"""
|
| 279 |
+
|
| 280 |
+
article = r"""
|
| 281 |
+
---
|
| 282 |
+
📝 **Citation**
|
| 283 |
+
<br>
|
| 284 |
+
If our work is helpful for your research or applications, please cite us via:
|
| 285 |
+
```bibtex
|
| 286 |
+
@article{tao2025instantcharacter,
|
| 287 |
+
title={InstantCharacter: Personalize Any Characters with a Scalable Diffusion Transformer Framework},
|
| 288 |
+
author={Tao, Jiale and Zhang, Yanbing and Wang, Qixun and Cheng, Yiji and Wang, Haofan and Bai, Xu and Zhou, Zhengguang and Li, Ruihuang and Wang, Linqing and Wang, Chunyu and others},
|
| 289 |
+
journal={arXiv preprint arXiv:2504.12395},
|
| 290 |
+
year={2025}
|
| 291 |
+
}
|
| 292 |
+
block = gr.Blocks(css="footer {visibility: hidden}").queue(max_size=10, api_open=False)
|
| 293 |
with block:
|
| 294 |
+
gr.Markdown(title)
|
| 295 |
+
gr.Markdown(description)
|
| 296 |
+
|
| 297 |
+
with gr.Tabs():
|
| 298 |
with gr.Row():
|
| 299 |
with gr.Column():
|
| 300 |
+
with gr.Row():
|
| 301 |
+
with gr.Column():
|
| 302 |
+
image_pil = gr.Image(label="Source Image", type='pil')
|
| 303 |
+
|
| 304 |
+
# Astuce : pense à inclure le trigger dans le prompt si besoin
|
| 305 |
prompt = gr.Textbox(
|
| 306 |
label="Prompt",
|
| 307 |
+
value=f"a character is riding a bike in snow, {ONEPIECE_TRIGGER}"
|
| 308 |
)
|
|
|
|
|
|
|
| 309 |
|
| 310 |
+
scale = gr.Slider(minimum=0, maximum=1.5, step=0.01, value=1.0, label="Scale")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
+
style_mode = gr.Dropdown(
|
| 313 |
+
label='Style',
|
| 314 |
+
choices=[None, 'Makoto Shinkai style', 'Ghibli style', 'One Piece style'],
|
| 315 |
+
value='One Piece style'
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
with gr.Accordion(open=False, label="Advanced Options"):
|
| 319 |
+
guidance_scale = gr.Slider(minimum=1, maximum=7.0, step=0.01, value=3.5, label="guidance scale")
|
| 320 |
+
num_inference_steps = gr.Slider(minimum=5, maximum=50.0, step=1.0, value=28, label="num inference steps")
|
| 321 |
+
seed = gr.Slider(minimum=-1000000, maximum=1000000, value=123456, step=1, label="Seed Value")
|
| 322 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
| 323 |
+
|
| 324 |
+
generate_button = gr.Button("Generate Image")
|
| 325 |
|
| 326 |
with gr.Column():
|
| 327 |
+
generated_image = gr.Gallery(label="Generated Image")
|
| 328 |
|
| 329 |
generate_button.click(
|
| 330 |
fn=randomize_seed_fn,
|
| 331 |
inputs=[seed, randomize_seed],
|
| 332 |
outputs=seed,
|
| 333 |
queue=False,
|
| 334 |
+
api_name=False,
|
| 335 |
).then(
|
| 336 |
fn=create_image,
|
| 337 |
inputs=[
|
| 338 |
+
image_pil,
|
| 339 |
+
prompt,
|
| 340 |
+
scale,
|
| 341 |
+
guidance_scale,
|
| 342 |
+
num_inference_steps,
|
| 343 |
+
seed,
|
| 344 |
+
style_mode,
|
| 345 |
],
|
| 346 |
+
outputs=[generated_image]
|
| 347 |
)
|
| 348 |
|
| 349 |
+
gr.Examples(
|
| 350 |
+
examples=get_example(),
|
| 351 |
+
inputs=[image_pil, prompt, scale, style_mode],
|
| 352 |
+
fn=run_for_examples,
|
| 353 |
+
outputs=[generated_image],
|
| 354 |
+
cache_examples=True,
|
| 355 |
+
)
|
| 356 |
+
|
| 357 |
+
gr.Markdown(article)
|
| 358 |
+
block.launch()
|