Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
"""
|
2 |
FLUX.1 Kontext Style Transfer
|
3 |
==============================
|
4 |
-
Updated: 2025โ07โ12
|
5 |
---------------------------------
|
6 |
์ด ์คํฌ๋ฆฝํธ๋ HuggingโฏFace **FLUX.1โKontextโdev** ๋ชจ๋ธ๊ณผ
|
7 |
22โฏ์ข
์ ์คํ์ผ LoRA ๊ฐ์ค์น๋ฅผ ์ด์ฉํด ์ด๋ฏธ์ง๋ฅผ ๋ค์ํ ์์
|
@@ -9,15 +9,15 @@ Updated: 2025โ07โ12
|
|
9 |
|
10 |
์ฃผ์ ๊ฐ์ ์ฌํญ
|
11 |
--------------
|
12 |
-
1.
|
13 |
-
|
14 |
-
|
15 |
-
2.
|
16 |
-
|
17 |
-
3.
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
------------------------------------------------------------
|
23 |
"""
|
@@ -25,7 +25,8 @@ import os
|
|
25 |
import gradio as gr
|
26 |
import spaces
|
27 |
import torch
|
28 |
-
from huggingface_hub import snapshot_download
|
|
|
29 |
from diffusers import FluxKontextPipeline
|
30 |
from diffusers.utils import load_image
|
31 |
from PIL import Image
|
@@ -40,19 +41,29 @@ MODEL_ID = "black-forest-labs/FLUX.1-Kontext-dev"
|
|
40 |
LORA_REPO = "Owen777/Kontext-Style-Loras"
|
41 |
CACHE_DIR = os.getenv("HF_HOME", os.path.expanduser("~/.cache/huggingface"))
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
# --- ์ต์ด ์คํ ์์๋ง ๋ค์ด๋ก๋(์ด๋ฏธ ์บ์์ ์์ผ๋ฉด ๊ฑด๋๋) ---
|
44 |
-
MODEL_DIR =
|
45 |
-
|
46 |
-
cache_dir=CACHE_DIR,
|
47 |
-
resume_download=True,
|
48 |
-
token=True # HF ํ ํฐ(ํ์ ์ ํ๊ฒฝ๋ณ์ HF_TOKEN ์ง์ )
|
49 |
-
)
|
50 |
-
LORA_DIR = snapshot_download(
|
51 |
-
repo_id=LORA_REPO,
|
52 |
-
cache_dir=CACHE_DIR,
|
53 |
-
resume_download=True,
|
54 |
-
token=True
|
55 |
-
)
|
56 |
|
57 |
# ------------------------------------------------------------------
|
58 |
# ์คํ์ผย โย LoRA ํ์ผ ๋งคํ & ์ค๋ช
|
@@ -112,7 +123,6 @@ STYLE_DESCRIPTIONS = {
|
|
112 |
# ------------------------------------------------------------------
|
113 |
_pipeline = None # ๋ด๋ถ ๊ธ๋ก๋ฒ ์บ์
|
114 |
|
115 |
-
|
116 |
def load_pipeline():
|
117 |
"""Load (or return cached) FluxKontextPipeline."""
|
118 |
global _pipeline
|
@@ -157,10 +167,9 @@ def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps,
|
|
157 |
pipe = load_pipeline()
|
158 |
|
159 |
# --- Torchย Generator ์ค์ ---
|
160 |
-
|
|
|
161 |
generator = torch.Generator(device="cuda").manual_seed(int(seed))
|
162 |
-
else:
|
163 |
-
generator = None # random
|
164 |
|
165 |
# --- ์
๋ ฅ ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ---
|
166 |
img = input_image if isinstance(input_image, Image.Image) else load_image(input_image)
|
@@ -178,7 +187,7 @@ def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps,
|
|
178 |
if prompt_suffix and prompt_suffix.strip():
|
179 |
prompt += f" {prompt_suffix.strip()}"
|
180 |
|
181 |
-
gr.Info("Generating styled imageโฆ (
|
182 |
|
183 |
result = pipe(
|
184 |
image=img,
|
@@ -204,7 +213,7 @@ def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps,
|
|
204 |
# ------------------------------------------------------------------
|
205 |
# Gradio UI ์ ์
|
206 |
# ------------------------------------------------------------------
|
207 |
-
with gr.Blocks(title="FLUX.1
|
208 |
gr.Markdown("""
|
209 |
# ๐จ FLUX.1 Kontext Style Transfer
|
210 |
|
@@ -231,44 +240,247 @@ with gr.Blocks(title="FLUX.1 Kontext Style Transfer", theme=gr.themes.Soft()) as
|
|
231 |
generate_btn = gr.Button("๐จ Transform Image", variant="primary", size="lg")
|
232 |
|
233 |
with gr.Column(scale=1):
|
234 |
-
output_image = gr.Image(label="Styled Result", type="pil", height=
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
260 |
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
outputs=output_image,
|
266 |
)
|
267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
gr.Markdown("""
|
269 |
-
|
270 |
-
|
|
|
|
|
271 |
""")
|
272 |
|
273 |
-
|
274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
"""
|
2 |
FLUX.1 Kontext Style Transfer
|
3 |
==============================
|
4 |
+
Updated: 2025โ07โ12 (HF_TOKEN ์ง์)
|
5 |
---------------------------------
|
6 |
์ด ์คํฌ๋ฆฝํธ๋ HuggingโฏFace **FLUX.1โKontextโdev** ๋ชจ๋ธ๊ณผ
|
7 |
22โฏ์ข
์ ์คํ์ผ LoRA ๊ฐ์ค์น๋ฅผ ์ด์ฉํด ์ด๋ฏธ์ง๋ฅผ ๋ค์ํ ์์
|
|
|
9 |
|
10 |
์ฃผ์ ๊ฐ์ ์ฌํญ
|
11 |
--------------
|
12 |
+
1. **HF_TOKEN ํ๊ฒฝ๋ณ์ ์ง์**โโย `LocalTokenNotFoundError`๋ฅผ
|
13 |
+
๋ฐฉ์งํ๊ธฐ ์ํด `HF_TOKEN`(๋๋ ๋ฐํ์ ๋ก๊ทธ์ธ) ๊ฐ์ ์๋์ผ๋ก
|
14 |
+
๊ฐ์งํด `snapshot_download()`์ ์ ๋ฌํฉ๋๋ค.
|
15 |
+
2. **๋ชจ๋ธ ์บ์ฑ**โโย `snapshot_download()`๋ก ์คํ ์์ ์ ํ ๋ฒ๋ง
|
16 |
+
๋ชจ๋ธ๊ณผ LoRA๋ฅผ ์บ์ฑ.
|
17 |
+
3. **GPUโฏVRAM ์๋ ํ๋ณ**โโย 24โฏGBย ๋ฏธ๋ง์ด๋ฉด FP16ย / CPUย offload.
|
18 |
+
4. **๋จ์ผ ๋ก๋ฉ ๋ฉ์์ง**โโย Gradioย `gr.Info()` ๋ฉ์์ง๊ฐ ์ต์ด 1ํ๋ง
|
19 |
+
ํ์๋๋๋ก ์ ์ง.
|
20 |
+
5. **๋ฒ๊ทธ ํฝ์ค**โโย seed ์ฒ๋ฆฌ, LoRA ์ธ๋ก๋, ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ฆ.
|
21 |
|
22 |
------------------------------------------------------------
|
23 |
"""
|
|
|
25 |
import gradio as gr
|
26 |
import spaces
|
27 |
import torch
|
28 |
+
from huggingface_hub import snapshot_download, login as hf_login
|
29 |
+
from huggingface_hub.errors import LocalTokenNotFoundError
|
30 |
from diffusers import FluxKontextPipeline
|
31 |
from diffusers.utils import load_image
|
32 |
from PIL import Image
|
|
|
41 |
LORA_REPO = "Owen777/Kontext-Style-Loras"
|
42 |
CACHE_DIR = os.getenv("HF_HOME", os.path.expanduser("~/.cache/huggingface"))
|
43 |
|
44 |
+
# --- HF ํ ํฐ ์ฒ๋ฆฌ --------------------------------------------------
|
45 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # ๋ฐํ์์ ํ๊ฒฝ๋ณ์๋ก ์ฃผ์
ํ๊ฑฐ๋
|
46 |
+
# dockerย /ย Space ์ค์ ์ Secrets โ HF_TOKEN ๋ก ๋ฑ๋ก
|
47 |
+
|
48 |
+
def _download_with_token(repo_id: str) -> str:
|
49 |
+
"""Download repo snapshot with optional token handling."""
|
50 |
+
try:
|
51 |
+
return snapshot_download(
|
52 |
+
repo_id=repo_id,
|
53 |
+
cache_dir=CACHE_DIR,
|
54 |
+
resume_download=True,
|
55 |
+
token=HF_TOKEN if HF_TOKEN else True, # True โ HF_CACHED_TOKEN(orย login)
|
56 |
+
)
|
57 |
+
except LocalTokenNotFoundError:
|
58 |
+
# ๋ฏธ๋ก๊ทธ์ธย +ย ํ์ย ๋์ ๋ชจ๋ธ์ด๋ฉด ์๋ฌ ๋ฉ์์ง ์ถ๋ ฅ ํ ์ข
๋ฃ
|
59 |
+
raise RuntimeError(
|
60 |
+
"Huggingย Face ํ ํฐ์ด ํ์ํฉ๋๋ค. ํ๊ฒฝ๋ณ์ HF_TOKEN์ ์ค์ ํ๊ฑฐ๋\n"
|
61 |
+
"`huggingface-cli login`์ผ๋ก ๋ก๊ทธ์ธํด ์ฃผ์ธ์."
|
62 |
+
)
|
63 |
+
|
64 |
# --- ์ต์ด ์คํ ์์๋ง ๋ค์ด๋ก๋(์ด๋ฏธ ์บ์์ ์์ผ๋ฉด ๊ฑด๋๋) ---
|
65 |
+
MODEL_DIR = _download_with_token(MODEL_ID)
|
66 |
+
LORA_DIR = _download_with_token(LORA_REPO)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# ------------------------------------------------------------------
|
69 |
# ์คํ์ผย โย LoRA ํ์ผ ๋งคํ & ์ค๋ช
|
|
|
123 |
# ------------------------------------------------------------------
|
124 |
_pipeline = None # ๋ด๋ถ ๊ธ๋ก๋ฒ ์บ์
|
125 |
|
|
|
126 |
def load_pipeline():
|
127 |
"""Load (or return cached) FluxKontextPipeline."""
|
128 |
global _pipeline
|
|
|
167 |
pipe = load_pipeline()
|
168 |
|
169 |
# --- Torchย Generator ์ค์ ---
|
170 |
+
generator = None
|
171 |
+
if seed and int(seed) > 0:
|
172 |
generator = torch.Generator(device="cuda").manual_seed(int(seed))
|
|
|
|
|
173 |
|
174 |
# --- ์
๋ ฅ ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ---
|
175 |
img = input_image if isinstance(input_image, Image.Image) else load_image(input_image)
|
|
|
187 |
if prompt_suffix and prompt_suffix.strip():
|
188 |
prompt += f" {prompt_suffix.strip()}"
|
189 |
|
190 |
+
gr.Info("Generating styled imageโฆ (20โ60โฏs)")
|
191 |
|
192 |
result = pipe(
|
193 |
image=img,
|
|
|
213 |
# ------------------------------------------------------------------
|
214 |
# Gradio UI ์ ์
|
215 |
# ------------------------------------------------------------------
|
216 |
+
with gr.Blocks(title="FLUX.1 Context Style Transfer", theme=gr.themes.Soft()) as demo:
|
217 |
gr.Markdown("""
|
218 |
# ๐จ FLUX.1 Kontext Style Transfer
|
219 |
|
|
|
240 |
generate_btn = gr.Button("๐จ Transform Image", variant="primary", size="lg")
|
241 |
|
242 |
with gr.Column(scale=1):
|
243 |
+
output_image = gr.Image(label="Styled Result", type="pil", height=
|
244 |
+
"""
|
245 |
+
FLUX.1 Kontext Style Transfer
|
246 |
+
==============================
|
247 |
+
Updated: 2025โ07โ12 (HF_TOKEN ์ง์)
|
248 |
+
---------------------------------
|
249 |
+
์ด ์คํฌ๋ฆฝํธ๋ HuggingโฏFace **FLUX.1โKontextโdev** ๋ชจ๋ธ๊ณผ
|
250 |
+
22โฏ์ข
์ ์คํ์ผ LoRA ๊ฐ์ค์น๋ฅผ ์ด์ฉํด ์ด๋ฏธ์ง๋ฅผ ๋ค์ํ ์์
|
251 |
+
์คํ์ผ๋ก ๋ณํํ๋ Gradio ๋ฐ๋ชจ์
๋๋ค.
|
252 |
+
|
253 |
+
์ฃผ์ ๊ฐ์ ์ฌํญ
|
254 |
+
--------------
|
255 |
+
1. **HF_TOKEN ํ๊ฒฝ๋ณ์ ์ง์**โโย `LocalTokenNotFoundError`๋ฅผ
|
256 |
+
๋ฐฉ์งํ๊ธฐ ์ํด `HF_TOKEN`(๋๋ ๋ฐํ์ ๋ก๊ทธ์ธ) ๊ฐ์ ์๋์ผ๋ก
|
257 |
+
๊ฐ์งํด `snapshot_download()`์ ์ ๋ฌํฉ๋๋ค.
|
258 |
+
2. **๋ชจ๋ธ ์บ์ฑ**โโย `snapshot_download()`๋ก ์คํ ์์ ์ ํ ๋ฒ๋ง
|
259 |
+
๋ชจ๋ธ๊ณผ LoRA๋ฅผ ์บ์ฑ.
|
260 |
+
3. **GPUโฏVRAM ์๋ ํ๋ณ**โโย 24โฏGBย ๋ฏธ๋ง์ด๋ฉด FP16ย / CPUย offload.
|
261 |
+
4. **๋จ์ผ ๋ก๋ฉ ๋ฉ์์ง**โโย Gradioย `gr.Info()` ๋ฉ์์ง๊ฐ ์ต์ด 1ํ๋ง
|
262 |
+
ํ์๋๋๋ก ์ ์ง.
|
263 |
+
5. **๋ฒ๊ทธ ํฝ์ค**โโย seed ์ฒ๋ฆฌ, LoRA ์ธ๋ก๋, ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ฆ.
|
264 |
+
|
265 |
+
------------------------------------------------------------
|
266 |
+
"""
|
267 |
+
import os
|
268 |
+
import gradio as gr
|
269 |
+
import spaces
|
270 |
+
import torch
|
271 |
+
from huggingface_hub import snapshot_download, login as hf_login
|
272 |
+
from huggingface_hub.errors import LocalTokenNotFoundError
|
273 |
+
from diffusers import FluxKontextPipeline
|
274 |
+
from diffusers.utils import load_image
|
275 |
+
from PIL import Image
|
276 |
+
|
277 |
+
# ------------------------------------------------------------------
|
278 |
+
# ํ๊ฒฝ ์ค์ & ๋ชจ๋ธย /ย LoRA ์ฌ์ ๋ค์ด๋ก๋
|
279 |
+
# ------------------------------------------------------------------
|
280 |
+
# ํฐ ํ์ผ์ ๋น ๋ฅด๊ฒ ๋ฐ๋๋ก ๊ฐ์ ํ๋๊ทธ ํ์ฑํ
|
281 |
+
os.environ.setdefault("HF_HUB_ENABLE_HF_TRANSFER", "1")
|
282 |
+
|
283 |
+
MODEL_ID = "black-forest-labs/FLUX.1-Kontext-dev"
|
284 |
+
LORA_REPO = "Owen777/Kontext-Style-Loras"
|
285 |
+
CACHE_DIR = os.getenv("HF_HOME", os.path.expanduser("~/.cache/huggingface"))
|
286 |
+
|
287 |
+
# --- HF ํ ํฐ ์ฒ๋ฆฌ --------------------------------------------------
|
288 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # ๋ฐํ์์ ํ๊ฒฝ๋ณ์๋ก ์ฃผ์
ํ๊ฑฐ๋
|
289 |
+
# dockerย /ย Space ์ค์ ์ Secrets โ HF_TOKEN ๋ก ๋ฑ๋ก
|
290 |
+
|
291 |
+
def _download_with_token(repo_id: str) -> str:
|
292 |
+
"""Download repo snapshot with optional token handling."""
|
293 |
+
try:
|
294 |
+
return snapshot_download(
|
295 |
+
repo_id=repo_id,
|
296 |
+
cache_dir=CACHE_DIR,
|
297 |
+
resume_download=True,
|
298 |
+
token=HF_TOKEN if HF_TOKEN else True, # True โ HF_CACHED_TOKEN(orย login)
|
299 |
+
)
|
300 |
+
except LocalTokenNotFoundError:
|
301 |
+
# ๋ฏธ๋ก๊ทธ์ธย +ย ํ์ย ๋์ ๋ชจ๋ธ์ด๋ฉด ์๋ฌ ๋ฉ์์ง ์ถ๋ ฅ ํ ์ข
๋ฃ
|
302 |
+
raise RuntimeError(
|
303 |
+
"Huggingย Face ํ ํฐ์ด ํ์ํฉ๋๋ค. ํ๊ฒฝ๋ณ์ HF_TOKEN์ ์ค์ ํ๊ฑฐ๋\n"
|
304 |
+
"`huggingface-cli login`์ผ๋ก ๋ก๊ทธ์ธํด ์ฃผ์ธ์."
|
305 |
+
)
|
306 |
+
|
307 |
+
# --- ์ต์ด ์คํ ์์๋ง ๋ค์ด๋ก๋(์ด๋ฏธ ์บ์์ ์์ผ๋ฉด ๊ฑด๋๋) ---
|
308 |
+
MODEL_DIR = _download_with_token(MODEL_ID)
|
309 |
+
LORA_DIR = _download_with_token(LORA_REPO)
|
310 |
+
|
311 |
+
# ------------------------------------------------------------------
|
312 |
+
# ์คํ์ผย โย LoRA ํ์ผ ๋งคํ & ๏ฟฝ๏ฟฝ๏ฟฝ๋ช
|
313 |
+
# ------------------------------------------------------------------
|
314 |
+
STYLE_LORA_MAP = {
|
315 |
+
"3D_Chibi": "3D_Chibi_lora_weights.safetensors",
|
316 |
+
"American_Cartoon": "American_Cartoon_lora_weights.safetensors",
|
317 |
+
"Chinese_Ink": "Chinese_Ink_lora_weights.safetensors",
|
318 |
+
"Clay_Toy": "Clay_Toy_lora_weights.safetensors",
|
319 |
+
"Fabric": "Fabric_lora_weights.safetensors",
|
320 |
+
"Ghibli": "Ghibli_lora_weights.safetensors",
|
321 |
+
"Irasutoya": "Irasutoya_lora_weights.safetensors",
|
322 |
+
"Jojo": "Jojo_lora_weights.safetensors",
|
323 |
+
"Oil_Painting": "Oil_Painting_lora_weights.safetensors",
|
324 |
+
"Pixel": "Pixel_lora_weights.safetensors",
|
325 |
+
"Snoopy": "Snoopy_lora_weights.safetensors",
|
326 |
+
"Poly": "Poly_lora_weights.safetensors",
|
327 |
+
"LEGO": "LEGO_lora_weights.safetensors",
|
328 |
+
"Origami": "Origami_lora_weights.safetensors",
|
329 |
+
"Pop_Art": "Pop_Art_lora_weights.safetensors",
|
330 |
+
"Van_Gogh": "Van_Gogh_lora_weights.safetensors",
|
331 |
+
"Paper_Cutting": "Paper_Cutting_lora_weights.safetensors",
|
332 |
+
"Line": "Line_lora_weights.safetensors",
|
333 |
+
"Vector": "Vector_lora_weights.safetensors",
|
334 |
+
"Picasso": "Picasso_lora_weights.safetensors",
|
335 |
+
"Macaron": "Macaron_lora_weights.safetensors",
|
336 |
+
"Rick_Morty": "Rick_Morty_lora_weights.safetensors",
|
337 |
+
}
|
338 |
+
|
339 |
+
STYLE_DESCRIPTIONS = {
|
340 |
+
"3D_Chibi": "Cute, miniature 3D character style with big heads",
|
341 |
+
"American_Cartoon": "Classic American animation style",
|
342 |
+
"Chinese_Ink": "Traditional Chinese ink painting aesthetic",
|
343 |
+
"Clay_Toy": "Playful clay/plasticine toy appearance",
|
344 |
+
"Fabric": "Soft, textile-like rendering",
|
345 |
+
"Ghibli": "Studio Ghibli's distinctive anime style",
|
346 |
+
"Irasutoya": "Simple, flat Japanese illustration style",
|
347 |
+
"Jojo": "JoJo's Bizarre Adventure manga style",
|
348 |
+
"Oil_Painting": "Classic oil painting texture and strokes",
|
349 |
+
"Pixel": "Retro pixel art style",
|
350 |
+
"Snoopy": "Peanuts comic strip style",
|
351 |
+
"Poly": "Low-poly 3D geometric style",
|
352 |
+
"LEGO": "LEGO brick construction style",
|
353 |
+
"Origami": "Paper folding art style",
|
354 |
+
"Pop_Art": "Bold, colorful pop art style",
|
355 |
+
"Van_Gogh": "Van Gogh's expressive brushstroke style",
|
356 |
+
"Paper_Cutting": "Paper cut-out art style",
|
357 |
+
"Line": "Clean line art/sketch style",
|
358 |
+
"Vector": "Clean vector graphics style",
|
359 |
+
"Picasso": "Cubist art style inspired by Picasso",
|
360 |
+
"Macaron": "Soft, pastel macaron-like style",
|
361 |
+
"Rick_Morty": "Rick and Morty cartoon style",
|
362 |
+
}
|
363 |
+
|
364 |
+
# ------------------------------------------------------------------
|
365 |
+
# ํ์ดํ๋ผ์ธ ๋ก๋ (๋จ์ผ ์ธ์คํด์ค)
|
366 |
+
# ------------------------------------------------------------------
|
367 |
+
_pipeline = None # ๋ด๋ถ ๊ธ๋ก๋ฒ ์บ์
|
368 |
+
|
369 |
+
def load_pipeline():
|
370 |
+
"""Load (or return cached) FluxKontextPipeline."""
|
371 |
+
global _pipeline
|
372 |
+
if _pipeline is not None:
|
373 |
+
return _pipeline
|
374 |
+
|
375 |
+
# VRAM์ด 24โฏGBย ๋ฏธ๋ง์ด๋ฉด FP16 ์ฌ์ฉ + CPU ์คํ๋ก๋ฉ
|
376 |
+
dtype = torch.bfloat16
|
377 |
+
vram_gb = torch.cuda.get_device_properties(0).total_memory / 1024**3
|
378 |
+
if vram_gb < 24:
|
379 |
+
dtype = torch.float16
|
380 |
+
|
381 |
+
gr.Info("FLUX.1โKontext ํ์ดํ๋ผ์ธ ๋ก๋ฉ ์คโฆย (์ต์ด 1ํ)")
|
382 |
|
383 |
+
pipe = FluxKontextPipeline.from_pretrained(
|
384 |
+
MODEL_DIR,
|
385 |
+
torch_dtype=dtype,
|
386 |
+
local_files_only=True,
|
|
|
387 |
)
|
388 |
|
389 |
+
pipe.to("cuda")
|
390 |
+
|
391 |
+
if vram_gb < 24:
|
392 |
+
pipe.enable_sequential_cpu_offload()
|
393 |
+
else:
|
394 |
+
pipe.enable_model_cpu_offload()
|
395 |
+
|
396 |
+
_pipeline = pipe
|
397 |
+
return _pipeline
|
398 |
+
|
399 |
+
# ------------------------------------------------------------------
|
400 |
+
# ์คํ์ผ ๋ณํ ํจ์ (Spaces GPU ์ก)
|
401 |
+
# ------------------------------------------------------------------
|
402 |
+
@spaces.GPU(duration=600)
|
403 |
+
def style_transfer(input_image, style_name, prompt_suffix, num_inference_steps, guidance_scale, seed):
|
404 |
+
"""Apply selected style to the uploaded image."""
|
405 |
+
if input_image is None:
|
406 |
+
gr.Warning("Please upload an image first!")
|
407 |
+
return None
|
408 |
+
|
409 |
+
try:
|
410 |
+
pipe = load_pipeline()
|
411 |
+
|
412 |
+
# --- Torchย Generator ์ค์ ---
|
413 |
+
generator = None
|
414 |
+
if seed and int(seed) > 0:
|
415 |
+
generator = torch.Generator(device="cuda").manual_seed(int(seed))
|
416 |
+
|
417 |
+
# --- ์
๋ ฅ ์ด๋ฏธ์ง ์ ์ฒ๋ฆฌ ---
|
418 |
+
img = input_image if isinstance(input_image, Image.Image) else load_image(input_image)
|
419 |
+
img = img.convert("RGB").resize((1024, 1024), Image.Resampling.LANCZOS)
|
420 |
+
|
421 |
+
# --- LoRA ๋ก๋ ---
|
422 |
+
lora_file = STYLE_LORA_MAP[style_name]
|
423 |
+
adapter_name = "style"
|
424 |
+
pipe.load_lora_weights(LORA_DIR, weight_name=lora_file, adapter_name=adapter_name)
|
425 |
+
pipe.set_adapters([adapter_name], [1.0])
|
426 |
+
|
427 |
+
# --- ํ๋กฌํํธ ๋น๋ ---
|
428 |
+
human_readable_style = style_name.replace("_", " ")
|
429 |
+
prompt = f"Turn this image into the {human_readable_style} style."
|
430 |
+
if prompt_suffix and prompt_suffix.strip():
|
431 |
+
prompt += f" {prompt_suffix.strip()}"
|
432 |
+
|
433 |
+
gr.Info("Generating styled imageโฆ (20โ60โฏs)")
|
434 |
+
|
435 |
+
result = pipe(
|
436 |
+
image=img,
|
437 |
+
prompt=prompt,
|
438 |
+
guidance_scale=float(guidance_scale),
|
439 |
+
num_inference_steps=int(num_inference_steps),
|
440 |
+
generator=generator,
|
441 |
+
height=1024,
|
442 |
+
width=1024,
|
443 |
+
)
|
444 |
+
|
445 |
+
# --- LoRA ์ธ๋ก๋ & GPU ๋ฉ๋ชจ๋ฆฌ ํด์ ---
|
446 |
+
pipe.unload_lora_weights(adapter_name=adapter_name)
|
447 |
+
torch.cuda.empty_cache()
|
448 |
+
|
449 |
+
return result.images[0]
|
450 |
+
|
451 |
+
except Exception as e:
|
452 |
+
torch.cuda.empty_cache()
|
453 |
+
gr.Error(f"Error during style transfer: {e}")
|
454 |
+
return None
|
455 |
+
|
456 |
+
# ------------------------------------------------------------------
|
457 |
+
# Gradio UI ์ ์
|
458 |
+
# ------------------------------------------------------------------
|
459 |
+
with gr.Blocks(title="FLUX.1 Context Style Transfer", theme=gr.themes.Soft()) as demo:
|
460 |
gr.Markdown("""
|
461 |
+
# ๐จ FLUX.1 Kontext Style Transfer
|
462 |
+
|
463 |
+
์
๋ก๋ํ ์ด๋ฏธ์ง๋ฅผ 22โฏ์ข
์ ์์ ์คํ์ผ๋ก ๋ณํํ์ธ์!
|
464 |
+
(๋ชจ๋ธโฏ/โฏLoRA๋ ์ต์ด ์คํ ์์๋ง ๋ค์ด๋ก๋๋๋ฉฐ, ์ดํ ์คํ์ ๋น ๋ฆ
๋๋ค.)
|
465 |
""")
|
466 |
|
467 |
+
with gr.Row():
|
468 |
+
with gr.Column(scale=1):
|
469 |
+
input_image = gr.Image(label="Upload Image", type="pil", height=400)
|
470 |
+
style_dropdown = gr.Dropdown(
|
471 |
+
choices=list(STYLE_LORA_MAP.keys()),
|
472 |
+
value="Ghibli",
|
473 |
+
label="Select Style",
|
474 |
+
)
|
475 |
+
style_info = gr.Textbox(label="Style Description", value=STYLE_DESCRIPTIONS["Ghibli"], interactive=False, lines=2)
|
476 |
+
prompt_suffix = gr.Textbox(label="Additional Instructions (Optional)", placeholder="e.g. add dramatic lighting", lines=2)
|
477 |
+
|
478 |
+
with gr.Accordion("Advanced Settings", open=False):
|
479 |
+
num_steps = gr.Slider(minimum=10, maximum=50, value=24, step=1, label="Inference Steps")
|
480 |
+
guidance = gr.Slider(minimum=1.0, maximum=7.5, value=2.5, step=0.1, label="Guidance Scale")
|
481 |
+
seed = gr.Number(label="Seed (0 = random)", value=42)
|
482 |
+
|
483 |
+
generate_btn = gr.Button("๐จ Transform Image", variant="primary", size="lg")
|
484 |
+
|
485 |
+
with gr.Column(scale=1):
|
486 |
+
output_image = gr.Image(label="Styled Result", type="pil", height=
|