Update app.py
Browse files
app.py
CHANGED
|
@@ -175,29 +175,46 @@ def contains_korean(text):
|
|
| 175 |
return any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text)
|
| 176 |
|
| 177 |
|
| 178 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
@spaces.GPU()
|
| 180 |
def generate_fashion(prompt, mode, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
|
| 181 |
try:
|
| 182 |
# ํ๊ธ ์ฒ๋ฆฌ
|
| 183 |
if contains_korean(prompt):
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
| 187 |
else:
|
| 188 |
actual_prompt = prompt
|
| 189 |
|
| 190 |
-
# ํ์ดํ๋ผ์ธ
|
| 191 |
pipe = initialize_fashion_pipe()
|
| 192 |
|
| 193 |
# LoRA ์ค์
|
| 194 |
if mode == "Generate Model":
|
| 195 |
-
pipe
|
| 196 |
trigger_word = "fashion photography, professional model"
|
| 197 |
else:
|
| 198 |
-
pipe
|
| 199 |
trigger_word = "upper clothing, fashion item"
|
| 200 |
-
|
| 201 |
# ํ๋ผ๋ฏธํฐ ์ ํ
|
| 202 |
width = min(width, 768)
|
| 203 |
height = min(height, 768)
|
|
@@ -206,27 +223,32 @@ def generate_fashion(prompt, mode, cfg_scale, steps, randomize_seed, seed, width
|
|
| 206 |
# ์๋ ์ค์
|
| 207 |
if randomize_seed:
|
| 208 |
seed = random.randint(0, MAX_SEED)
|
| 209 |
-
generator = torch.Generator(
|
| 210 |
-
|
| 211 |
-
# ์งํ๋ฅ ํ์
|
| 212 |
-
progress(0, "Starting fashion generation...")
|
| 213 |
|
| 214 |
# ์ด๋ฏธ์ง ์์ฑ
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
return image, seed
|
| 226 |
-
|
| 227 |
except Exception as e:
|
| 228 |
print(f"Error in generate_fashion: {str(e)}")
|
| 229 |
-
raise
|
| 230 |
|
| 231 |
@safe_model_call
|
| 232 |
def leffa_predict(src_image_path, ref_image_path, control_type):
|
|
|
|
| 175 |
return any(ord('๊ฐ') <= ord(char) <= ord('ํฃ') for char in text)
|
| 176 |
|
| 177 |
|
| 178 |
+
# ๋ชจ๋ธ ์ด๊ธฐํ ํจ์ ์์
|
| 179 |
+
@spaces.GPU()
|
| 180 |
+
def initialize_fashion_pipe():
|
| 181 |
+
try:
|
| 182 |
+
pipe = DiffusionPipeline.from_pretrained(
|
| 183 |
+
BASE_MODEL,
|
| 184 |
+
torch_dtype=torch.float16,
|
| 185 |
+
safety_checker=None,
|
| 186 |
+
requires_safety_checker=False
|
| 187 |
+
).to("cuda")
|
| 188 |
+
pipe.enable_model_cpu_offload()
|
| 189 |
+
return pipe
|
| 190 |
+
except Exception as e:
|
| 191 |
+
print(f"Error initializing fashion pipe: {e}")
|
| 192 |
+
raise
|
| 193 |
+
|
| 194 |
+
# ์์ฑ ํจ์ ์์
|
| 195 |
@spaces.GPU()
|
| 196 |
def generate_fashion(prompt, mode, cfg_scale, steps, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
|
| 197 |
try:
|
| 198 |
# ํ๊ธ ์ฒ๋ฆฌ
|
| 199 |
if contains_korean(prompt):
|
| 200 |
+
with torch.inference_mode():
|
| 201 |
+
translator = get_translator()
|
| 202 |
+
translated = translator(prompt)[0]['translation_text']
|
| 203 |
+
actual_prompt = translated
|
| 204 |
else:
|
| 205 |
actual_prompt = prompt
|
| 206 |
|
| 207 |
+
# ํ์ดํ๋ผ์ธ ์ด๊ธฐํ
|
| 208 |
pipe = initialize_fashion_pipe()
|
| 209 |
|
| 210 |
# LoRA ์ค์
|
| 211 |
if mode == "Generate Model":
|
| 212 |
+
pipe.load_lora_weights(MODEL_LORA_REPO)
|
| 213 |
trigger_word = "fashion photography, professional model"
|
| 214 |
else:
|
| 215 |
+
pipe.load_lora_weights(CLOTHES_LORA_REPO)
|
| 216 |
trigger_word = "upper clothing, fashion item"
|
| 217 |
+
|
| 218 |
# ํ๋ผ๋ฏธํฐ ์ ํ
|
| 219 |
width = min(width, 768)
|
| 220 |
height = min(height, 768)
|
|
|
|
| 223 |
# ์๋ ์ค์
|
| 224 |
if randomize_seed:
|
| 225 |
seed = random.randint(0, MAX_SEED)
|
| 226 |
+
generator = torch.Generator("cuda").manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
# ์ด๋ฏธ์ง ์์ฑ
|
| 229 |
+
with torch.inference_mode():
|
| 230 |
+
output = pipe(
|
| 231 |
+
prompt=f"{actual_prompt} {trigger_word}",
|
| 232 |
+
num_inference_steps=steps,
|
| 233 |
+
guidance_scale=cfg_scale,
|
| 234 |
+
width=width,
|
| 235 |
+
height=height,
|
| 236 |
+
generator=generator,
|
| 237 |
+
cross_attention_kwargs={"scale": lora_scale},
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
image = output.images[0]
|
| 241 |
+
|
| 242 |
+
# ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
| 243 |
+
del pipe
|
| 244 |
+
torch.cuda.empty_cache()
|
| 245 |
+
gc.collect()
|
| 246 |
|
| 247 |
return image, seed
|
| 248 |
+
|
| 249 |
except Exception as e:
|
| 250 |
print(f"Error in generate_fashion: {str(e)}")
|
| 251 |
+
raise gr.Error(f"Generation failed: {str(e)}")
|
| 252 |
|
| 253 |
@safe_model_call
|
| 254 |
def leffa_predict(src_image_path, ref_image_path, control_type):
|