ginipick commited on
Commit
96169f0
ยท
verified ยท
1 Parent(s): e45e9e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -26
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=0 if torch.cuda.is_available() else -1)
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
- image = pipe(
173
- prompt=prompt,
174
- width=width,
175
- height=height,
176
- num_inference_steps=8,
177
- guidance_scale=4.0,
178
- ).images[0]
 
 
 
 
 
 
179
 
180
  return image
181
  except Exception as e:
182
- raise gr.Error(f"Background generation failed: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- prompt = translate_to_english(prompt)
309
- if bg_prompt:
310
- bg_prompt = translate_to_english(bg_prompt)
 
 
 
311
 
312
  # Process the image
313
  results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
314
 
315
  if bg_prompt:
316
- # ์—ฌ๊ธฐ์„œ ํ•œ ๋ฒˆ๋งŒ ํ•ฉ์„ฑ ์ˆ˜ํ–‰
317
- combined = combine_with_background(
318
- foreground=results[2], # ์ถ”์ถœ๋œ ์˜ค๋ธŒ์ ํŠธ
319
- background=results[1], # ์ƒ์„ฑ๋œ ๋ฐฐ๊ฒฝ
320
- position=position,
321
- scale_percent=scale_percent
322
- )
323
- print(f"Combined image created with position: {position}") # ๋””๋ฒ„๊ทธ ๋กœ๊ทธ
324
- return combined, results[2]
 
 
 
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]: