Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -94,20 +94,39 @@ except Exception as e:
|
|
| 94 |
# LoRA ๊ฐ์ค์น ๋ก๋ ๋ถ๋ถ ์์
|
| 95 |
print("Loading LoRA weights...")
|
| 96 |
try:
|
| 97 |
-
# ๋ก์ปฌ LoRA
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
print(f"Loading LoRA weights from local file: {lora_path}")
|
| 101 |
-
pipe.load_lora_weights(lora_path)
|
| 102 |
-
pipe.fuse_lora(lora_scale=0.125)
|
| 103 |
print("LoRA weights loaded and fused successfully")
|
|
|
|
|
|
|
| 104 |
|
| 105 |
except Exception as e:
|
| 106 |
print(f"Error loading LoRA weights: {str(e)}")
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
|
| 110 |
-
# generate_image ํจ์ ์์
|
| 111 |
@spaces.GPU(duration=60)
|
| 112 |
def generate_image(
|
| 113 |
prompt: str,
|
|
@@ -124,12 +143,17 @@ def generate_image(
|
|
| 124 |
|
| 125 |
translated_prompt = translate_to_english(prompt)
|
| 126 |
print(f"Processing prompt: {translated_prompt}")
|
|
|
|
| 127 |
|
| 128 |
if randomize_seed:
|
| 129 |
seed = random.randint(0, MAX_SEED)
|
| 130 |
|
| 131 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
with torch.inference_mode(), torch.cuda.amp.autocast(enabled=True):
|
| 134 |
image = pipe(
|
| 135 |
prompt=translated_prompt,
|
|
@@ -142,17 +166,16 @@ def generate_image(
|
|
| 142 |
).images[0]
|
| 143 |
|
| 144 |
filepath = save_generated_image(image, translated_prompt)
|
|
|
|
| 145 |
return image, seed
|
| 146 |
|
| 147 |
except Exception as e:
|
| 148 |
print(f"Generation error: {str(e)}")
|
|
|
|
| 149 |
raise gr.Error(f"Image generation failed: {str(e)}")
|
| 150 |
finally:
|
| 151 |
clear_memory()
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
# ์ ์ฅ ๋๋ ํ ๋ฆฌ ์ค์
|
| 157 |
SAVE_DIR = "saved_images"
|
| 158 |
if not os.path.exists(SAVE_DIR):
|
|
|
|
| 94 |
# LoRA ๊ฐ์ค์น ๋ก๋ ๋ถ๋ถ ์์
|
| 95 |
print("Loading LoRA weights...")
|
| 96 |
try:
|
| 97 |
+
# ๋ก์ปฌ LoRA ํ์ผ์ ์ ๋ ๊ฒฝ๋ก ํ์ธ
|
| 98 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 99 |
+
lora_path = os.path.join(current_dir, "myt-flux-fantasy.safetensors")
|
| 100 |
+
|
| 101 |
+
if not os.path.exists(lora_path):
|
| 102 |
+
raise FileNotFoundError(f"LoRA file not found at: {lora_path}")
|
| 103 |
+
|
| 104 |
+
print(f"Loading LoRA weights from: {lora_path}")
|
| 105 |
+
|
| 106 |
+
# LoRA ๊ฐ์ค์น ๋ก๋ ๋ฐฉ์ ์์
|
| 107 |
+
pipe.load_lora_weights(
|
| 108 |
+
lora_path,
|
| 109 |
+
adapter_name="fantasy"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# LoRA ๊ฐ์ค์น ํ์ฑํ
|
| 113 |
+
pipe.set_adapters(["fantasy"])
|
| 114 |
+
pipe.fuse_lora(lora_scale=0.75) # lora_scale ๊ฐ ์กฐ์
|
| 115 |
+
|
| 116 |
+
# ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
|
| 117 |
+
torch.cuda.empty_cache()
|
| 118 |
+
gc.collect()
|
| 119 |
|
|
|
|
|
|
|
|
|
|
| 120 |
print("LoRA weights loaded and fused successfully")
|
| 121 |
+
print(f"Current device: {pipe.device}")
|
| 122 |
+
print(f"Active adapters: {pipe.active_adapters}")
|
| 123 |
|
| 124 |
except Exception as e:
|
| 125 |
print(f"Error loading LoRA weights: {str(e)}")
|
| 126 |
+
print(f"Full error details: {repr(e)}")
|
| 127 |
+
raise ValueError(f"Failed to load LoRA weights: {str(e)}")
|
| 128 |
|
| 129 |
|
|
|
|
| 130 |
@spaces.GPU(duration=60)
|
| 131 |
def generate_image(
|
| 132 |
prompt: str,
|
|
|
|
| 143 |
|
| 144 |
translated_prompt = translate_to_english(prompt)
|
| 145 |
print(f"Processing prompt: {translated_prompt}")
|
| 146 |
+
print(f"Active adapters before generation: {pipe.active_adapters}")
|
| 147 |
|
| 148 |
if randomize_seed:
|
| 149 |
seed = random.randint(0, MAX_SEED)
|
| 150 |
|
| 151 |
generator = torch.Generator(device=device).manual_seed(seed)
|
| 152 |
|
| 153 |
+
# LoRA ์ค์ ํ์ธ
|
| 154 |
+
print(f"LoRA scale: {pipe.lora_scale}")
|
| 155 |
+
print(f"Current device: {pipe.device}")
|
| 156 |
+
|
| 157 |
with torch.inference_mode(), torch.cuda.amp.autocast(enabled=True):
|
| 158 |
image = pipe(
|
| 159 |
prompt=translated_prompt,
|
|
|
|
| 166 |
).images[0]
|
| 167 |
|
| 168 |
filepath = save_generated_image(image, translated_prompt)
|
| 169 |
+
print(f"Image generated and saved to: {filepath}")
|
| 170 |
return image, seed
|
| 171 |
|
| 172 |
except Exception as e:
|
| 173 |
print(f"Generation error: {str(e)}")
|
| 174 |
+
print(f"Full error details: {repr(e)}")
|
| 175 |
raise gr.Error(f"Image generation failed: {str(e)}")
|
| 176 |
finally:
|
| 177 |
clear_memory()
|
| 178 |
|
|
|
|
|
|
|
|
|
|
| 179 |
# ์ ์ฅ ๋๋ ํ ๋ฆฌ ์ค์
|
| 180 |
SAVE_DIR = "saved_images"
|
| 181 |
if not os.path.exists(SAVE_DIR):
|