mrbui1990 commited on
Commit
e1aeaf4
·
verified ·
1 Parent(s): f8a03e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -3
app.py CHANGED
@@ -5,7 +5,7 @@ import torch
5
  import spaces
6
 
7
  from PIL import Image
8
- from diffusers import FlowMatchEulerDiscreteScheduler
9
  from optimization import optimize_pipeline_
10
  from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
11
  from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
@@ -16,6 +16,11 @@ import math
16
  from huggingface_hub import hf_hub_download
17
  from safetensors.torch import load_file
18
 
 
 
 
 
 
19
  import os
20
  import base64
21
  from io import BytesIO
@@ -28,6 +33,85 @@ from PIL import Image
28
  import os
29
  import gradio as gr
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  def turn_into_video(input_images, output_images, prompt, progress=gr.Progress(track_tqdm=True)):
32
  """Calls multimodalart/wan-2-2-first-last-frame space to generate a video."""
33
  if not input_images or not output_images:
@@ -498,9 +582,9 @@ def infer(
498
  true_cfg_scale=true_guidance_scale,
499
  num_images_per_prompt=num_images_per_prompt,
500
  ).images
501
-
502
  # Return images, seed, and make button visible
503
- return image, seed, gr.update(visible=True), gr.update(visible=True)
504
 
505
 
506
  # --- Examples and UI Layout ---
 
5
  import spaces
6
 
7
  from PIL import Image
8
+ from diffusers import FlowMatchEulerDiscreteScheduler,DiffusionPipeline
9
  from optimization import optimize_pipeline_
10
  from qwenimage.pipeline_qwenimage_edit_plus import QwenImageEditPlusPipeline
11
  from qwenimage.transformer_qwenimage import QwenImageTransformer2DModel
 
16
  from huggingface_hub import hf_hub_download
17
  from safetensors.torch import load_file
18
 
19
+ from basicsr.archs.rrdbnet_arch import RRDBNet
20
+ from basicsr.utils.download_util import load_file_from_url
21
+ from realesrgan import RealESRGANer
22
+ from realesrgan.archs.srvgg_arch import SRVGGNetCompact
23
+
24
  import os
25
  import base64
26
  from io import BytesIO
 
33
  import os
34
  import gradio as gr
35
 
36
+
37
+ # --- Upscaling ---
38
+ MAX_SEED = np.iinfo(np.int32).max
39
+ UPSAMPLER_CACHE = {}
40
+ GFPGAN_FACE_ENHANCER = {}
41
+
42
+ def rnd_string(x): return "".join(random.choice("abcdefghijklmnopqrstuvwxyz_0123456789") for _ in range(x))
43
+
44
+
45
+ def get_model_and_paths(model_name, denoise_strength):
46
+ if model_name in ('RealESRGAN_x4plus', 'RealESRNet_x4plus'):
47
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=4)
48
+ netscale = 4
49
+ file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth'] \
50
+ if model_name == 'RealESRGAN_x4plus' else \
51
+ ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth']
52
+ elif model_name == 'RealESRGAN_x4plus_anime_6B':
53
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=6, num_grow_ch=32, scale=4)
54
+ netscale = 4
55
+ file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth']
56
+ elif model_name == 'RealESRGAN_x2plus':
57
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
58
+ netscale = 2
59
+ file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth']
60
+ elif model_name == 'realesr-general-x4v3':
61
+ model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=2, act_type='prelu')
62
+ netscale = 4
63
+ file_url = [
64
+ 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-wdn-x4v3.pth',
65
+ 'https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth'
66
+ ]
67
+ else:
68
+ raise ValueError(f"Unsupported model: {model_name}")
69
+
70
+ model_path = os.path.join("weights", model_name + ".pth")
71
+ if not os.path.isfile(model_path):
72
+ ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
73
+ for url in file_url:
74
+ model_path = load_file_from_url(url=url, model_dir=os.path.join(ROOT_DIR, "weights"), progress=True)
75
+
76
+ return model, netscale, model_path, None
77
+
78
+
79
+ def get_upsampler(model_name, denoise_strength):
80
+ key = (model_name, float(denoise_strength), device)
81
+ if key in UPSAMPLER_CACHE:
82
+ return UPSAMPLER_CACHE[key]
83
+
84
+ model, netscale, model_path, dni_weight = get_model_and_paths(model_name, denoise_strength)
85
+ upsampler = RealESRGANer(
86
+ scale=netscale,
87
+ model_path=model_path,
88
+ model=model,
89
+ tile=0,
90
+ tile_pad=10,
91
+ pre_pad=10,
92
+ half=(dtype == torch.bfloat16),
93
+ gpu_id=0 if device == "cuda" else None,
94
+ )
95
+ UPSAMPLER_CACHE[key] = upsampler
96
+ return upsampler
97
+
98
+ @spaces.GPU
99
+ def realesrgan(img, model_name, denoise_strength, outscale=4, progress=gr.Progress(track_tqdm=True)):
100
+ if not img:
101
+ return
102
+ upsampler = get_upsampler(model_name, denoise_strength)
103
+ cv_img = np.array(img.convert("RGB"))
104
+ bgr = cv2.cvtColor(cv_img, cv2.COLOR_RGB2BGR)
105
+ try:
106
+ output, _ = upsampler.enhance(bgr, outscale=int(outscale))
107
+ except Exception as e:
108
+ print("Upscale error:", e)
109
+ return img
110
+ out_filename = f"output_{rnd_string(8)}.jpg"
111
+ cv2.imwrite(out_filename, output)
112
+ return out_filename
113
+
114
+
115
  def turn_into_video(input_images, output_images, prompt, progress=gr.Progress(track_tqdm=True)):
116
  """Calls multimodalart/wan-2-2-first-last-frame space to generate a video."""
117
  if not input_images or not output_images:
 
582
  true_cfg_scale=true_guidance_scale,
583
  num_images_per_prompt=num_images_per_prompt,
584
  ).images
585
+ upscaled = realesrgan(image, "realesr-general-x4v3", 0.5)
586
  # Return images, seed, and make button visible
587
+ return upscaled, seed, gr.update(visible=True), gr.update(visible=True)
588
 
589
 
590
  # --- Examples and UI Layout ---