Rooni commited on
Commit
c2f0585
1 Parent(s): bc76885

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py CHANGED
@@ -7,6 +7,38 @@ from PIL import Image
7
  from deep_translator import GoogleTranslator
8
  from langdetect import detect
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  API_URL = "https://api-inference.huggingface.co/models/openskyml/dalle-3-xl"
11
  API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
12
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
@@ -60,6 +92,64 @@ def query(prompt, model, is_negative=False, steps=20, cfg_scale=7, seed=None):
60
  return image
61
 
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  css = """
64
  footer {visibility: hidden !important;}
65
  """
@@ -75,11 +165,21 @@ with gr.Blocks(css=css) as dalle:
75
  with gr.Tab("Расширенные настройки"):
76
  negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Чего не должно быть на изображении", value="[deformed | disfigured], poorly drawn, [bad : wrong] anatomy, [extra | missing | floating | disconnected] limb, (mutated hands and fingers), blurry, text, fuzziness", lines=3, elem_id="negative-prompt-text-input")
77
 
 
 
 
 
 
78
  with gr.Row():
79
  text_button = gr.Button("Генерация", variant='primary', elem_id="gen-button")
80
  with gr.Row():
81
  image_output = gr.Image(type="pil", label="Изображение", elem_id="gallery")
 
 
 
 
82
 
83
  text_button.click(query, inputs=[text_prompt, model, negative_prompt], outputs=image_output)
 
84
 
85
  dalle.launch(show_api=False)
 
7
  from deep_translator import GoogleTranslator
8
  from langdetect import detect
9
 
10
+ import cv2
11
+ import torch
12
+ from basicsr.archs.srvgg_arch import SRVGGNetCompact
13
+ from gfpgan.utils import GFPGANer
14
+ from realesrgan.utils import RealESRGANer
15
+
16
+ os.system("pip freeze")
17
+ # download weights
18
+ if not os.path.exists('realesr-general-x4v3.pth'):
19
+ os.system("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
20
+ if not os.path.exists('GFPGANv1.2.pth'):
21
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.2.pth -P .")
22
+ if not os.path.exists('GFPGANv1.3.pth'):
23
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth -P .")
24
+ if not os.path.exists('GFPGANv1.4.pth'):
25
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
26
+ if not os.path.exists('RestoreFormer.pth'):
27
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/RestoreFormer.pth -P .")
28
+ if not os.path.exists('CodeFormer.pth'):
29
+ os.system("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/CodeFormer.pth -P .")
30
+
31
+
32
+ # background enhancer with RealESRGAN
33
+ model_us = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
34
+ model_us_path = 'realesr-general-x4v3.pth'
35
+ half = True if torch.cuda.is_available() else False
36
+ upsampler = RealESRGANer(scale=4, model_us_path=model_us_path, model_us=model_us, tile=0, tile_pad=10, pre_pad=0, half=half)
37
+
38
+ os.makedirs('output', exist_ok=True)
39
+
40
+
41
+
42
  API_URL = "https://api-inference.huggingface.co/models/openskyml/dalle-3-xl"
43
  API_TOKEN = os.getenv("HF_READ_TOKEN") # it is free
44
  headers = {"Authorization": f"Bearer {API_TOKEN}"}
 
92
  return image
93
 
94
 
95
+
96
+ def inference(img, version, scale, weight):
97
+ weight /= 100
98
+ print(img, version, scale, weight)
99
+ try:
100
+ extension = os.path.splitext(os.path.basename(str(img)))[1]
101
+ img = cv2.imread(img, cv2.IMREAD_UNCHANGED)
102
+ if len(img.shape) == 3 and img.shape[2] == 4:
103
+ img_mode = 'RGBA'
104
+ elif len(img.shape) == 2: # for gray inputs
105
+ img_mode = None
106
+ img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
107
+ else:
108
+ img_mode = None
109
+
110
+ if version == 'v1.2':
111
+ face_enhancer = GFPGANer(
112
+ model_us_path='GFPGANv1.2.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
113
+ elif version == 'v1.3':
114
+ face_enhancer = GFPGANer(
115
+ model_us_path='GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
116
+ elif version == 'v1.4':
117
+ face_enhancer = GFPGANer(
118
+ model_us_path='GFPGANv1.4.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler)
119
+ elif version == 'RestoreFormer':
120
+ face_enhancer = GFPGANer(
121
+ model_us_path='RestoreFormer.pth', upscale=2, arch='RestoreFormer', channel_multiplier=2, bg_upsampler=upsampler)
122
+ elif version == 'CodeFormer':
123
+ face_enhancer = GFPGANer(
124
+ model_us_path='CodeFormer.pth', upscale=2, arch='CodeFormer', channel_multiplier=2, bg_upsampler=upsampler)
125
+
126
+ try:
127
+ _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True, weight=weight)
128
+ except RuntimeError as error:
129
+ print('Error', error)
130
+
131
+ try:
132
+ interpolation = cv2.INTER_AREA if scale < 2 else cv2.INTER_LANCZOS4
133
+ h, w = img.shape[0:2]
134
+ output = cv2.resize(output, (int(w * scale), int(h * scale)), interpolation=interpolation)
135
+ except Exception as error:
136
+ print('wrong scale input.', error)
137
+ if img_mode == 'RGBA': # RGBA images should be saved in png format
138
+ extension = 'png'
139
+ else:
140
+ extension = 'jpg'
141
+ save_path = f'output/out.{extension}'
142
+ cv2.imwrite(save_path, output)
143
+
144
+ output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)
145
+ return output
146
+ except Exception as error:
147
+ print('global exception', error)
148
+ return None
149
+
150
+
151
+
152
+
153
  css = """
154
  footer {visibility: hidden !important;}
155
  """
 
165
  with gr.Tab("Расширенные настройки"):
166
  negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="Чего не должно быть на изображении", value="[deformed | disfigured], poorly drawn, [bad : wrong] anatomy, [extra | missing | floating | disconnected] limb, (mutated hands and fingers), blurry, text, fuzziness", lines=3, elem_id="negative-prompt-text-input")
167
 
168
+ with gr.Tab("Настройки апскейлинга"):
169
+ up_1 = gr.Radio(choices=['v1.2', 'v1.3', 'v1.4', 'RestoreFormer', 'CodeFormer'], value='v1.4', label='Версия'),
170
+ up_2 = gr.Slider(label="Коэффициент масштабирования", value=2, minimum=2, maximum=6),
171
+ up_3 = gr.Slider(0, 100, label='Weight, только для CodeFormer. 0 для лучшего качества, 100 для лучшей идентичности', value=50)
172
+
173
  with gr.Row():
174
  text_button = gr.Button("Генерация", variant='primary', elem_id="gen-button")
175
  with gr.Row():
176
  image_output = gr.Image(type="pil", label="Изображение", elem_id="gallery")
177
+ with gr.Row():
178
+ up_button = gr.Button("Улучшить изображение", variant='primary', elem_id="gen-button")
179
+ with gr.Row():
180
+ up_output = gr.Image(type="pil", label="Улучшенное изображение", elem_id="gallery"),
181
 
182
  text_button.click(query, inputs=[text_prompt, model, negative_prompt], outputs=image_output)
183
+ up_button.click(query, inputs=[image_output, up_1, up_2, up_3], outputs=up_output)
184
 
185
  dalle.launch(show_api=False)