Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -22,11 +22,11 @@ from diffusers import FluxPipeline
|
|
22 |
# ์๋จ์ import ์ถ๊ฐ
|
23 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
24 |
|
25 |
-
|
26 |
model_name = "Helsinki-NLP/opus-mt-ko-en"
|
27 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
28 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
29 |
-
translator = pipeline("translation", model=model, tokenizer=tokenizer, device
|
30 |
|
31 |
|
32 |
def translate_to_english(text: str) -> str:
|
@@ -168,18 +168,46 @@ def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
|
|
168 |
# 8์ ๋ฐฐ์๋ก ์กฐ์
|
169 |
width, height = adjust_size_to_multiple_of_8(width, height)
|
170 |
|
|
|
|
|
|
|
|
|
171 |
with timer("Background generation"):
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
return image
|
181 |
except Exception as e:
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
|
185 |
def create_position_grid():
|
@@ -302,30 +330,36 @@ def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
|
|
302 |
if img is None or prompt.strip() == "":
|
303 |
raise gr.Error("Please provide both image and prompt")
|
304 |
|
305 |
-
print(f"Processing with position: {position}, scale: {scale_percent}")
|
306 |
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
|
|
|
|
|
|
311 |
|
312 |
# Process the image
|
313 |
results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
|
314 |
|
315 |
if bg_prompt:
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
|
|
|
|
|
|
325 |
|
326 |
return results[1], results[2]
|
327 |
except Exception as e:
|
328 |
-
print(f"Error in process_prompt: {str(e)}")
|
329 |
raise gr.Error(str(e))
|
330 |
|
331 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|
|
|
22 |
# ์๋จ์ import ์ถ๊ฐ
|
23 |
from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
|
24 |
|
25 |
+
|
26 |
model_name = "Helsinki-NLP/opus-mt-ko-en"
|
27 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
28 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to('cpu') # CPU๋ก ๊ฐ์ ์ง์
|
29 |
+
translator = pipeline("translation", model=model, tokenizer=tokenizer, device=-1) # CPU ์ฌ์ฉ
|
30 |
|
31 |
|
32 |
def translate_to_english(text: str) -> str:
|
|
|
168 |
# 8์ ๋ฐฐ์๋ก ์กฐ์
|
169 |
width, height = adjust_size_to_multiple_of_8(width, height)
|
170 |
|
171 |
+
# ํ๋กฌํํธ ์ ์ฒ๋ฆฌ
|
172 |
+
if not prompt or prompt.strip() == "":
|
173 |
+
prompt = "plain white background"
|
174 |
+
|
175 |
with timer("Background generation"):
|
176 |
+
try:
|
177 |
+
image = pipe(
|
178 |
+
prompt=prompt,
|
179 |
+
width=width,
|
180 |
+
height=height,
|
181 |
+
num_inference_steps=8,
|
182 |
+
guidance_scale=4.0,
|
183 |
+
max_length=77, # CLIP ํ
์คํธ ์ธ์ฝ๋์ ์ต๋ ๊ธธ์ด ์ ํ
|
184 |
+
).images[0]
|
185 |
+
except Exception as e:
|
186 |
+
print(f"Pipeline error: {str(e)}")
|
187 |
+
# ์ค๋ฅ ๋ฐ์ ์ ๊ธฐ๋ณธ ํฐ์ ๋ฐฐ๊ฒฝ ์์ฑ
|
188 |
+
image = Image.new('RGB', (width, height), 'white')
|
189 |
|
190 |
return image
|
191 |
except Exception as e:
|
192 |
+
print(f"Background generation error: {str(e)}")
|
193 |
+
# ์ตํ์ ํด๋ฐฑ: ๊ธฐ๋ณธ ํฐ์ ๋ฐฐ๊ฒฝ ๋ฐํ
|
194 |
+
return Image.new('RGB', (512, 512), 'white')
|
195 |
+
|
196 |
+
# FLUX ํ์ดํ๋ผ์ธ ์ด๊ธฐํ ๋ถ๋ถ ์์
|
197 |
+
pipe = FluxPipeline.from_pretrained(
|
198 |
+
"black-forest-labs/FLUX.1-dev",
|
199 |
+
torch_dtype=torch.float32, # bfloat16 ๋์ float32 ์ฌ์ฉ
|
200 |
+
use_auth_token=HF_TOKEN
|
201 |
+
)
|
202 |
+
pipe.load_lora_weights(
|
203 |
+
hf_hub_download(
|
204 |
+
"ByteDance/Hyper-SD",
|
205 |
+
"Hyper-FLUX.1-dev-8steps-lora.safetensors",
|
206 |
+
use_auth_token=HF_TOKEN
|
207 |
+
)
|
208 |
+
)
|
209 |
+
pipe.fuse_lora(lora_scale=0.125)
|
210 |
+
pipe.to(device=device)
|
211 |
|
212 |
|
213 |
def create_position_grid():
|
|
|
330 |
if img is None or prompt.strip() == "":
|
331 |
raise gr.Error("Please provide both image and prompt")
|
332 |
|
333 |
+
print(f"Processing with position: {position}, scale: {scale_percent}")
|
334 |
|
335 |
+
try:
|
336 |
+
# ํ๋กฌํํธ ๋ฒ์ญ ์๋
|
337 |
+
prompt = translate_to_english(prompt)
|
338 |
+
if bg_prompt:
|
339 |
+
bg_prompt = translate_to_english(bg_prompt)
|
340 |
+
except Exception as e:
|
341 |
+
print(f"Translation error (continuing with original text): {str(e)}")
|
342 |
|
343 |
# Process the image
|
344 |
results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
|
345 |
|
346 |
if bg_prompt:
|
347 |
+
try:
|
348 |
+
combined = combine_with_background(
|
349 |
+
foreground=results[2],
|
350 |
+
background=results[1],
|
351 |
+
position=position,
|
352 |
+
scale_percent=scale_percent
|
353 |
+
)
|
354 |
+
print(f"Combined image created with position: {position}")
|
355 |
+
return combined, results[2]
|
356 |
+
except Exception as e:
|
357 |
+
print(f"Combination error: {str(e)}")
|
358 |
+
return results[1], results[2]
|
359 |
|
360 |
return results[1], results[2]
|
361 |
except Exception as e:
|
362 |
+
print(f"Error in process_prompt: {str(e)}")
|
363 |
raise gr.Error(str(e))
|
364 |
|
365 |
def process_bbox(img: Image.Image, box_input: str) -> tuple[Image.Image, Image.Image]:
|