Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -156,38 +156,35 @@ AVAILABLE_LORAS = {
|
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
def load_lora(pipe, lora_name):
|
| 162 |
-
"""Загружает LoRA в pipeline"""
|
| 163 |
if lora_name == "None" or lora_name not in AVAILABLE_LORAS:
|
| 164 |
-
return
|
| 165 |
-
|
| 166 |
-
if lora_name in loaded_loras:
|
| 167 |
-
logger.info(f" Using cached LoRA: {lora_name}")
|
| 168 |
-
return pipe
|
| 169 |
|
| 170 |
lora_info = AVAILABLE_LORAS[lora_name]
|
| 171 |
logger.info(f" Loading LoRA: {lora_name} from {lora_info['repo']}")
|
| 172 |
|
| 173 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
pipe.load_lora_weights(
|
| 175 |
lora_info["repo"],
|
| 176 |
-
weight_name=
|
| 177 |
-
|
|
|
|
| 178 |
)
|
| 179 |
-
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
| 181 |
except Exception as e:
|
| 182 |
-
logger.warning(f" ⚠️ Failed to
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
def unload_loras(pipe):
|
| 187 |
-
"""Выгружает все LoRA"""
|
| 188 |
-
if hasattr(pipe, 'unload_lora_weights'):
|
| 189 |
-
pipe.unload_lora_weights()
|
| 190 |
-
loaded_loras.clear()
|
| 191 |
|
| 192 |
# =================================================================
|
| 193 |
# GENERATION FUNCTIONS
|
|
@@ -224,10 +221,9 @@ def generate_text2img(
|
|
| 224 |
logger.info(f" Seed: {seed}, LoRA: {lora_name}")
|
| 225 |
|
| 226 |
try:
|
| 227 |
-
#
|
| 228 |
if lora_name != "None":
|
| 229 |
-
|
| 230 |
-
pipe_txt2img.set_adapters([lora_name], adapter_weights=[lora_scale])
|
| 231 |
|
| 232 |
generator = torch.Generator(device="cuda:0" if torch.cuda.is_available() else "cpu").manual_seed(seed)
|
| 233 |
|
|
@@ -241,6 +237,11 @@ def generate_text2img(
|
|
| 241 |
generator=generator
|
| 242 |
).images[0]
|
| 243 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
logger.info(" ✓ Generation complete")
|
| 245 |
|
| 246 |
return image, seed
|
|
@@ -285,10 +286,9 @@ def generate_img2img(
|
|
| 285 |
logger.info(f" Seed: {seed}, LoRA: {lora_name}")
|
| 286 |
|
| 287 |
try:
|
| 288 |
-
#
|
| 289 |
if lora_name != "None":
|
| 290 |
-
|
| 291 |
-
pipe_img2img.set_adapters([lora_name], adapter_weights=[lora_scale])
|
| 292 |
|
| 293 |
generator = torch.Generator(device="cuda:0" if torch.cuda.is_available() else "cpu").manual_seed(seed)
|
| 294 |
|
|
@@ -302,6 +302,11 @@ def generate_img2img(
|
|
| 302 |
generator=generator
|
| 303 |
).images[0]
|
| 304 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
logger.info(" ✓ Generation complete")
|
| 306 |
|
| 307 |
return image, seed
|
|
|
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
+
def apply_lora(pipe, lora_name, lora_scale):
|
| 160 |
+
"""Применяет LoRA к pipeline (по референсной реализации)"""
|
|
|
|
|
|
|
| 161 |
if lora_name == "None" or lora_name not in AVAILABLE_LORAS:
|
| 162 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
lora_info = AVAILABLE_LORAS[lora_name]
|
| 165 |
logger.info(f" Loading LoRA: {lora_name} from {lora_info['repo']}")
|
| 166 |
|
| 167 |
try:
|
| 168 |
+
# Всегда выгружаем существующие LoRA перед загрузкой новых
|
| 169 |
+
pipe.unload_lora_weights()
|
| 170 |
+
|
| 171 |
+
# Загружаем LoRA с adapter_name
|
| 172 |
+
weight_name = lora_info.get("weights", None)
|
| 173 |
pipe.load_lora_weights(
|
| 174 |
lora_info["repo"],
|
| 175 |
+
weight_name=weight_name,
|
| 176 |
+
low_cpu_mem_usage=True,
|
| 177 |
+
adapter_name="style" # Используем фиксированное имя "style"
|
| 178 |
)
|
| 179 |
+
|
| 180 |
+
# Активируем adapter с заданным весом
|
| 181 |
+
pipe.set_adapters(["style"], adapter_weights=[lora_scale])
|
| 182 |
+
logger.info(f" ✓ LoRA activated: {lora_name} (scale: {lora_scale})")
|
| 183 |
+
|
| 184 |
except Exception as e:
|
| 185 |
+
logger.warning(f" ⚠️ Failed to apply LoRA {lora_name}: {e}")
|
| 186 |
+
import traceback
|
| 187 |
+
logger.warning(traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
# =================================================================
|
| 190 |
# GENERATION FUNCTIONS
|
|
|
|
| 221 |
logger.info(f" Seed: {seed}, LoRA: {lora_name}")
|
| 222 |
|
| 223 |
try:
|
| 224 |
+
# Применяем LoRA если выбрана
|
| 225 |
if lora_name != "None":
|
| 226 |
+
apply_lora(pipe_txt2img, lora_name, lora_scale)
|
|
|
|
| 227 |
|
| 228 |
generator = torch.Generator(device="cuda:0" if torch.cuda.is_available() else "cpu").manual_seed(seed)
|
| 229 |
|
|
|
|
| 237 |
generator=generator
|
| 238 |
).images[0]
|
| 239 |
|
| 240 |
+
# Выгружаем LoRA после генерации
|
| 241 |
+
if lora_name != "None":
|
| 242 |
+
pipe_txt2img.unload_lora_weights()
|
| 243 |
+
logger.info(" ✓ LoRA unloaded")
|
| 244 |
+
|
| 245 |
logger.info(" ✓ Generation complete")
|
| 246 |
|
| 247 |
return image, seed
|
|
|
|
| 286 |
logger.info(f" Seed: {seed}, LoRA: {lora_name}")
|
| 287 |
|
| 288 |
try:
|
| 289 |
+
# Применяем LoRA если выбрана
|
| 290 |
if lora_name != "None":
|
| 291 |
+
apply_lora(pipe_img2img, lora_name, lora_scale)
|
|
|
|
| 292 |
|
| 293 |
generator = torch.Generator(device="cuda:0" if torch.cuda.is_available() else "cpu").manual_seed(seed)
|
| 294 |
|
|
|
|
| 302 |
generator=generator
|
| 303 |
).images[0]
|
| 304 |
|
| 305 |
+
# Выгружаем LoRA после генерации
|
| 306 |
+
if lora_name != "None":
|
| 307 |
+
pipe_img2img.unload_lora_weights()
|
| 308 |
+
logger.info(" ✓ LoRA unloaded")
|
| 309 |
+
|
| 310 |
logger.info(" ✓ Generation complete")
|
| 311 |
|
| 312 |
return image, seed
|