openfree commited on
Commit
ddcfc43
1 Parent(s): af86047

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -4
app.py CHANGED
@@ -17,13 +17,15 @@ import pandas as pd
17
  from transformers import pipeline
18
 
19
  import logging
20
- import random
21
  import warnings
22
  import numpy as np
23
  from diffusers import FluxControlNetModel
24
  from diffusers.pipelines import FluxControlNetPipeline
25
  from PIL import Image
26
  from huggingface_hub import snapshot_download
 
 
 
27
 
28
  # 번역 모델 로드
29
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
@@ -488,9 +490,70 @@ css = '''
488
  #component-11{align-self: stretch;}
489
  footer {visibility: hidden;}
490
  '''
491
- # 업스케일 함수 추가
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
492
  @spaces.GPU
493
  def upscale(input_image, progress=gr.Progress(track_tqdm=True)):
 
 
 
494
  # 입력 이미지 처리
495
  input_image, w_original, h_original, was_resized = process_input(input_image, 4)
496
 
@@ -500,9 +563,9 @@ def upscale(input_image, progress=gr.Progress(track_tqdm=True)):
500
  generator = torch.Generator(device=device).manual_seed(random.randint(0, MAX_SEED))
501
 
502
  gr.Info("Upscaling image to 4096x4096...")
503
- image = pipe(
504
  prompt="",
505
- control_image=control_image,
506
  controlnet_conditioning_scale=0.6,
507
  num_inference_steps=28,
508
  guidance_scale=3.5,
 
17
  from transformers import pipeline
18
 
19
  import logging
 
20
  import warnings
21
  import numpy as np
22
  from diffusers import FluxControlNetModel
23
  from diffusers.pipelines import FluxControlNetPipeline
24
  from PIL import Image
25
  from huggingface_hub import snapshot_download
26
+ from gradio_imageslider import ImageSlider
27
+
28
+
29
 
30
  # 번역 모델 로드
31
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
 
490
  #component-11{align-self: stretch;}
491
  footer {visibility: hidden;}
492
  '''
493
+
494
+
495
+
496
+ huggingface_token = os.getenv("HUGGINFACE_TOKEN")
497
+
498
+ model_path = snapshot_download(
499
+ repo_id="black-forest-labs/FLUX.1-dev",
500
+ repo_type="model",
501
+ ignore_patterns=["*.md", "*..gitattributes"],
502
+ local_dir="FLUX.1-dev",
503
+ token=huggingface_token, # type a new token-id.
504
+ )
505
+
506
+
507
+ # Load pipeline
508
+ controlnet = FluxControlNetModel.from_pretrained(
509
+ "jasperai/Flux.1-dev-Controlnet-Upscaler", torch_dtype=torch.bfloat16
510
+ ).to(device)
511
+ pipe = FluxControlNetPipeline.from_pretrained(
512
+ model_path, controlnet=controlnet, torch_dtype=torch.bfloat16
513
+ )
514
+ pipe.to(device)
515
+
516
+ MAX_SEED = 1000000
517
+ MAX_PIXEL_BUDGET = 1024 * 1024
518
+
519
+
520
+
521
+ def process_input(input_image, upscale_factor):
522
+ w, h = input_image.size
523
+ w_original, h_original = w, h
524
+ aspect_ratio = w / h
525
+
526
+ was_resized = False
527
+
528
+ if w * h * upscale_factor**2 > MAX_PIXEL_BUDGET:
529
+ warnings.warn(
530
+ f"Requested output image is too large ({w * upscale_factor}x{h * upscale_factor}). Resizing to ({int(aspect_ratio * MAX_PIXEL_BUDGET ** 0.5 // upscale_factor), int(MAX_PIXEL_BUDGET ** 0.5 // aspect_ratio // upscale_factor)}) pixels."
531
+ )
532
+ gr.Info(
533
+ f"Requested output image is too large ({w * upscale_factor}x{h * upscale_factor}). Resizing input to ({int(aspect_ratio * MAX_PIXEL_BUDGET ** 0.5 // upscale_factor), int(MAX_PIXEL_BUDGET ** 0.5 // aspect_ratio // upscale_factor)}) pixels budget."
534
+ )
535
+ input_image = input_image.resize(
536
+ (
537
+ int(aspect_ratio * MAX_PIXEL_BUDGET**0.5 // upscale_factor),
538
+ int(MAX_PIXEL_BUDGET**0.5 // aspect_ratio // upscale_factor),
539
+ )
540
+ )
541
+ was_resized = True
542
+
543
+ # resize to multiple of 8
544
+ w, h = input_image.size
545
+ w = w - w % 8
546
+ h = h - h % 8
547
+
548
+ return input_image.resize((w, h)), w_original, h_original, was_resized
549
+
550
+ MAX_PIXEL_BUDGET = 1024 * 1024
551
+
552
  @spaces.GPU
553
  def upscale(input_image, progress=gr.Progress(track_tqdm=True)):
554
+ if input_image is None:
555
+ raise gr.Error("No image to upscale. Please generate an image first.")
556
+
557
  # 입력 이미지 처리
558
  input_image, w_original, h_original, was_resized = process_input(input_image, 4)
559
 
 
563
  generator = torch.Generator(device=device).manual_seed(random.randint(0, MAX_SEED))
564
 
565
  gr.Info("Upscaling image to 4096x4096...")
566
+ image = pipe_controlnet(
567
  prompt="",
568
+ image=control_image,
569
  controlnet_conditioning_scale=0.6,
570
  num_inference_steps=28,
571
  guidance_scale=3.5,