openfree commited on
Commit
1961692
ยท
verified ยท
1 Parent(s): 09a09ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -69
app.py CHANGED
@@ -15,39 +15,59 @@ import spaces
15
  from huggingface_hub import hf_hub_url
16
  import subprocess
17
  from huggingface_hub import hf_hub_download
 
 
 
 
18
 
19
  def parse_args():
20
  parser = argparse.ArgumentParser()
21
- parser.add_argument( '--height', type=int, default=2560, help='image height')
22
  parser.add_argument('--width', type=int, default=5120, help='image width')
23
  parser.add_argument('--seed', type=int, default=123, help='random seed')
24
- parser.add_argument('--dtype', type=str, default='bf16', help=' if bf16 does not work, change it to float32 ')
25
  parser.add_argument('--config_c', type=str,
26
- default='configs/training/t2i.yaml' ,help='config file for stage c, latent generation')
27
  parser.add_argument('--config_b', type=str,
28
- default='configs/inference/stage_b_1b.yaml' ,help='config file for stage b, latent decoding')
29
- parser.add_argument( '--prompt', type=str,
30
  default='A photo-realistic image of a west highland white terrier in the garden, high quality, detail rich, 8K', help='text prompt')
31
- parser.add_argument( '--num_image', type=int, default=1, help='how many images generated')
32
- parser.add_argument( '--output_dir', type=str, default='figures/output_results/', help='output directory for generated image')
33
- parser.add_argument( '--stage_a_tiled', action='store_true', help='whther or nor to use tiled decoding for stage a to save memory')
34
- parser.add_argument( '--pretrained_path', type=str, default='models/ultrapixel_t2i.safetensors', help='pretrained path of newly added paramter of UltraPixel')
35
  args = parser.parse_args()
36
  return args
37
 
38
  def clear_image():
39
  return None
 
40
  def load_message(height, width, seed, prompt, args, stage_a_tiled):
41
  args.height = height
42
  args.width = width
43
- args.seed = seed
44
  args.prompt = prompt + ' rich detail, 4k, high quality'
45
  args.stage_a_tiled = stage_a_tiled
46
  return args
 
 
 
 
 
 
 
 
 
 
 
47
  @spaces.GPU(duration=120)
48
  def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
49
  global args
50
- args = load_message(height, width, seed, prompt, args, stage_a_tiled)
 
 
 
 
51
  torch.manual_seed(args.seed)
52
  random.seed(args.seed)
53
  np.random.seed(args.seed)
@@ -55,7 +75,7 @@ def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
55
 
56
  captions = [args.prompt] * args.num_image
57
  height, width = args.height, args.width
58
- batch_size=1
59
  height_lr, width_lr = get_target_lr_size(height / width, std_size=32)
60
  stage_c_latent_shape, stage_b_latent_shape = calculate_latent_sizes(height, width, batch_size=batch_size)
61
  stage_c_latent_shape_lr, stage_b_latent_shape_lr = calculate_latent_sizes(height_lr, width_lr, batch_size=batch_size)
@@ -67,8 +87,6 @@ def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
67
  extras.sampling_configs['t_start'] = 1.0
68
  extras.sampling_configs['sampler'] = DDPMSampler(extras.gdf)
69
 
70
-
71
-
72
  # Stage B Parameters
73
  extras_b.sampling_configs['cfg'] = 1.1
74
  extras_b.sampling_configs['shift'] = 1
@@ -76,61 +94,52 @@ def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
76
  extras_b.sampling_configs['t_start'] = 1.0
77
 
78
  for _, caption in enumerate(captions):
79
-
 
 
80
 
81
- batch = {'captions': [caption] * batch_size}
82
- #conditions = core.get_conditions(batch, models, extras, is_eval=True, is_unconditional=False, eval_image_embeds=False)
83
- #unconditions = core.get_conditions(batch, models, extras, is_eval=True, is_unconditional=True, eval_image_embeds=False)
 
 
 
 
 
84
 
85
  conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
86
  unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
 
 
 
87
 
 
 
88
 
89
- with torch.no_grad():
90
-
91
-
92
- models.generator.cuda()
93
- print('STAGE C GENERATION***************************')
94
- with torch.cuda.amp.autocast(dtype=dtype):
95
- sampled_c = generation_c(batch, models, extras, core, stage_c_latent_shape, stage_c_latent_shape_lr, device)
96
-
97
-
98
-
99
- models.generator.cpu()
100
- torch.cuda.empty_cache()
101
-
102
- conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
103
- unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
104
- conditions_b['effnet'] = sampled_c
105
- unconditions_b['effnet'] = torch.zeros_like(sampled_c)
106
- print('STAGE B + A DECODING***************************')
107
-
108
- with torch.cuda.amp.autocast(dtype=dtype):
109
- sampled = decode_b(conditions_b, unconditions_b, models_b, stage_b_latent_shape, extras_b, device, stage_a_tiled=args.stage_a_tiled)
110
-
111
- torch.cuda.empty_cache()
112
- imgs = show_images(sampled)
113
- #for idx, img in enumerate(imgs):
114
- #print(os.path.join(save_dir, args.prompt[:20]+'_' + str(cnt).zfill(5) + '.jpg'), idx)
115
- #img.save(os.path.join(save_dir, args.prompt[:20]+'_' + str(cnt).zfill(5) + '.jpg'))
116
 
117
  return imgs[0]
118
- #print('finished! Results ')
119
 
 
 
 
 
 
120
 
121
- with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
122
  with gr.Column(elem_id="col-container"):
123
  gr.Markdown("<h1><center>UltraPixel</center></h1>")
124
 
125
  with gr.Row():
126
  prompt = gr.Textbox(
127
- label="Text Prompt",
128
  show_label=False,
129
  max_lines=1,
130
- placeholder="Enter your prompt",
131
  container=False
132
  )
133
- polish_button = gr.Button("Submit!", scale=0)
134
 
135
  output_img = gr.Image(label="Output Image", show_label=False)
136
 
@@ -140,11 +149,8 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
140
  value=123,
141
  step=1,
142
  minimum=0,
143
- #maximum=MAX_SEED
144
  )
145
 
146
- #randomize_seed = gr.Checkbox(label="Randomize seed", value=False)
147
-
148
  with gr.Row():
149
  width = gr.Slider(
150
  label="Width",
@@ -186,14 +192,14 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
186
  gr.Examples(
187
  examples=[
188
  "A detailed view of a blooming magnolia tree, with large, white flowers and dark green leaves, set against a clear blue sky.",
189
- "A close-up portrait of a young woman with flawless skin, vibrant red lipstick, and wavy brown hair, wearing a vintage floral dress and standing in front of a blooming garden.",
190
  "The image features a snow-covered mountain range with a large, snow-covered mountain in the background. The mountain is surrounded by a forest of trees, and the sky is filled with clouds. The scene is set during the winter season, with snow covering the ground and the trees.",
191
- "Crocodile in a sweater.",
192
  "A vibrant anime scene of a young girl with long, flowing pink hair, big sparkling blue eyes, and a school uniform, standing under a cherry blossom tree with petals falling around her. The background shows a traditional Japanese school with cherry blossoms in full bloom.",
193
- "A playful Labrador retriever puppy with a shiny, golden coat, chasing a red ball in a spacious backyard, with green grass and a wooden fence.",
194
  "A cozy, rustic log cabin nestled in a snow-covered forest, with smoke rising from the stone chimney, warm lights glowing from the windows, and a path of footprints leading to the front door.",
195
- "A highly detailed, high-quality image of the Banff National Park in Canada. The turquoise waters of Lake Louise are surrounded by snow-capped mountains and dense pine forests. A wooden canoe is docked at the edge of the lake. The sky is a clear, bright blue, and the air is crisp and fresh.",
196
- "A highly detailed, high-quality image of a Shih Tzu receiving a bath in a home bathroom. The dog is standing in a tub, covered in suds, with a slightly wet and adorable look. The background includes bathroom fixtures, towels, and a clean, tiled floor.",
197
  ],
198
  inputs=[prompt],
199
  outputs=[output_img],
@@ -202,33 +208,27 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme") as demo:
202
 
203
  polish_button.click(get_image, inputs=[height, width, seed, prompt, cfg, timesteps, stage_a_tiled], outputs=output_img)
204
  polish_button.click(clear_image, inputs=[], outputs=output_img)
205
-
206
-
207
 
208
  def download_with_wget(url, save_path):
209
-
210
  try:
211
  subprocess.run(['wget', url, '-O', save_path], check=True)
212
  print(f"Downloaded to {save_path}")
213
  except subprocess.CalledProcessError as e:
214
  print(f"Error downloading file: {e}")
 
215
  def download_model():
216
-
217
  urls = [
218
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/stage_a.safetensors',
219
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/previewer.safetensors',
220
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/effnet_encoder.safetensors',
221
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/stage_b_lite_bf16.safetensors',
222
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/stage_c_bf16.safetensors',
223
-
224
  ]
225
  for file_url in urls:
226
  hf_hub_download(repo_id="stabilityai/stable-cascade", filename=file_url.split('/')[-1], local_dir='models')
227
- # 'https://huggingface.co/roubaofeipi/UltraPixel/blob/main/ultrapixel_t2i.safetensors'
228
  hf_hub_download(repo_id="roubaofeipi/UltraPixel", filename='ultrapixel_t2i.safetensors', local_dir='models')
229
 
230
  if __name__ == "__main__":
231
-
232
  args = parse_args()
233
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
234
  download_model()
@@ -268,7 +268,4 @@ if __name__ == "__main__":
268
  models.generator.eval()
269
  models.train_norm.eval()
270
 
271
-
272
- demo.launch(
273
- debug=True, share=True,
274
- )
 
15
  from huggingface_hub import hf_hub_url
16
  import subprocess
17
  from huggingface_hub import hf_hub_download
18
+ from transformers import pipeline
19
+
20
+ # Initialize the translation pipeline
21
+ translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ko-en")
22
 
23
  def parse_args():
24
  parser = argparse.ArgumentParser()
25
+ parser.add_argument('--height', type=int, default=2560, help='image height')
26
  parser.add_argument('--width', type=int, default=5120, help='image width')
27
  parser.add_argument('--seed', type=int, default=123, help='random seed')
28
+ parser.add_argument('--dtype', type=str, default='bf16', help='if bf16 does not work, change it to float32')
29
  parser.add_argument('--config_c', type=str,
30
+ default='configs/training/t2i.yaml', help='config file for stage c, latent generation')
31
  parser.add_argument('--config_b', type=str,
32
+ default='configs/inference/stage_b_1b.yaml', help='config file for stage b, latent decoding')
33
+ parser.add_argument('--prompt', type=str,
34
  default='A photo-realistic image of a west highland white terrier in the garden, high quality, detail rich, 8K', help='text prompt')
35
+ parser.add_argument('--num_image', type=int, default=1, help='how many images generated')
36
+ parser.add_argument('--output_dir', type=str, default='figures/output_results/', help='output directory for generated image')
37
+ parser.add_argument('--stage_a_tiled', action='store_true', help='whether or not to use tiled decoding for stage a to save memory')
38
+ parser.add_argument('--pretrained_path', type=str, default='models/ultrapixel_t2i.safetensors', help='pretrained path of newly added parameter of UltraPixel')
39
  args = parser.parse_args()
40
  return args
41
 
42
  def clear_image():
43
  return None
44
+
45
  def load_message(height, width, seed, prompt, args, stage_a_tiled):
46
  args.height = height
47
  args.width = width
48
+ args.seed = seed
49
  args.prompt = prompt + ' rich detail, 4k, high quality'
50
  args.stage_a_tiled = stage_a_tiled
51
  return args
52
+
53
+ def is_korean(text):
54
+ return any('\uac00' <= char <= '\ud7a3' for char in text)
55
+
56
+ def translate_if_korean(text):
57
+ if is_korean(text):
58
+ translated = translator(text, max_length=512)[0]['translation_text']
59
+ print(f"Translated from Korean: {text} -> {translated}")
60
+ return translated
61
+ return text
62
+
63
  @spaces.GPU(duration=120)
64
  def get_image(height, width, seed, prompt, cfg, timesteps, stage_a_tiled):
65
  global args
66
+
67
+ # Translate the prompt if it's in Korean
68
+ prompt = translate_if_korean(prompt)
69
+
70
+ args = load_message(height, width, seed, prompt, args, stage_a_tiled)
71
  torch.manual_seed(args.seed)
72
  random.seed(args.seed)
73
  np.random.seed(args.seed)
 
75
 
76
  captions = [args.prompt] * args.num_image
77
  height, width = args.height, args.width
78
+ batch_size = 1
79
  height_lr, width_lr = get_target_lr_size(height / width, std_size=32)
80
  stage_c_latent_shape, stage_b_latent_shape = calculate_latent_sizes(height, width, batch_size=batch_size)
81
  stage_c_latent_shape_lr, stage_b_latent_shape_lr = calculate_latent_sizes(height_lr, width_lr, batch_size=batch_size)
 
87
  extras.sampling_configs['t_start'] = 1.0
88
  extras.sampling_configs['sampler'] = DDPMSampler(extras.gdf)
89
 
 
 
90
  # Stage B Parameters
91
  extras_b.sampling_configs['cfg'] = 1.1
92
  extras_b.sampling_configs['shift'] = 1
 
94
  extras_b.sampling_configs['t_start'] = 1.0
95
 
96
  for _, caption in enumerate(captions):
97
+ batch = {'captions': [caption] * batch_size}
98
+ conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
99
+ unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
100
 
101
+ with torch.no_grad():
102
+ models.generator.cuda()
103
+ print('STAGE C GENERATION***************************')
104
+ with torch.cuda.amp.autocast(dtype=dtype):
105
+ sampled_c = generation_c(batch, models, extras, core, stage_c_latent_shape, stage_c_latent_shape_lr, device)
106
+
107
+ models.generator.cpu()
108
+ torch.cuda.empty_cache()
109
 
110
  conditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=False)
111
  unconditions_b = core_b.get_conditions(batch, models_b, extras_b, is_eval=True, is_unconditional=True)
112
+ conditions_b['effnet'] = sampled_c
113
+ unconditions_b['effnet'] = torch.zeros_like(sampled_c)
114
+ print('STAGE B + A DECODING***************************')
115
 
116
+ with torch.cuda.amp.autocast(dtype=dtype):
117
+ sampled = decode_b(conditions_b, unconditions_b, models_b, stage_b_latent_shape, extras_b, device, stage_a_tiled=args.stage_a_tiled)
118
 
119
+ torch.cuda.empty_cache()
120
+ imgs = show_images(sampled)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  return imgs[0]
 
123
 
124
+ css = """
125
+ footer {
126
+ visibility: hidden;
127
+ }
128
+ """
129
 
130
+ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
131
  with gr.Column(elem_id="col-container"):
132
  gr.Markdown("<h1><center>UltraPixel</center></h1>")
133
 
134
  with gr.Row():
135
  prompt = gr.Textbox(
136
+ label="Text Prompt (ํ•œ๊ธ€ ๋˜๋Š” ์˜์–ด๋กœ ์ž…๋ ฅํ•˜์„ธ์š”)",
137
  show_label=False,
138
  max_lines=1,
139
+ placeholder="ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š” (Enter your prompt in Korean or English)",
140
  container=False
141
  )
142
+ polish_button = gr.Button("์ œ์ถœ! (Submit!)", scale=0)
143
 
144
  output_img = gr.Image(label="Output Image", show_label=False)
145
 
 
149
  value=123,
150
  step=1,
151
  minimum=0,
 
152
  )
153
 
 
 
154
  with gr.Row():
155
  width = gr.Slider(
156
  label="Width",
 
192
  gr.Examples(
193
  examples=[
194
  "A detailed view of a blooming magnolia tree, with large, white flowers and dark green leaves, set against a clear blue sky.",
195
+ "๋ˆˆ ๋ฎ์ธ ์‚ฐ๋งฅ์˜ ์žฅ์—„ํ•œ ์ „๊ฒฝ, ํ‘ธ๋ฅธ ํ•˜๋Š˜์„ ๋ฐฐ๊ฒฝ์œผ๋กœ ํ•œ ๊ณ ์š”ํ•œ ํ˜ธ์ˆ˜๊ฐ€ ์žˆ๋Š” ๋ชจ์Šต",
196
  "The image features a snow-covered mountain range with a large, snow-covered mountain in the background. The mountain is surrounded by a forest of trees, and the sky is filled with clouds. The scene is set during the winter season, with snow covering the ground and the trees.",
197
+ "์Šค์›จํ„ฐ๋ฅผ ์ž…์€ ์•…์–ด",
198
  "A vibrant anime scene of a young girl with long, flowing pink hair, big sparkling blue eyes, and a school uniform, standing under a cherry blossom tree with petals falling around her. The background shows a traditional Japanese school with cherry blossoms in full bloom.",
199
+ "๊ณจ๋“  ๋ฆฌํŠธ๋ฆฌ๋ฒ„ ๊ฐ•์•„์ง€๊ฐ€ ํ‘ธ๋ฅธ ์ž”๋””๋ฐญ์—์„œ ๋นจ๊ฐ„ ๊ณต์„ ์ซ“๋Š” ๊ท€์—ฌ์šด ๋ชจ์Šต",
200
  "A cozy, rustic log cabin nestled in a snow-covered forest, with smoke rising from the stone chimney, warm lights glowing from the windows, and a path of footprints leading to the front door.",
201
+ "์บ๋‚˜๋‹ค ๋ฐดํ”„ ๊ตญ๋ฆฝ๊ณต์›์˜ ์•„๋ฆ„๋‹ค์šด ํ’๊ฒฝ, ์ฒญ๋ก์ƒ‰ ํ˜ธ์ˆ˜์™€ ๋ˆˆ ๋ฎ์ธ ์‚ฐ๋“ค, ์šธ์ฐฝํ•œ ์†Œ๋‚˜๋ฌด ์ˆฒ์ด ์–ด์šฐ๋Ÿฌ์ง„ ๋ชจ์Šต",
202
+ "๊ท€์—ฌ์šด ์‹œ์ธ„๊ฐ€ ์š•์กฐ์—์„œ ๋ชฉ์š•ํ•˜๋Š” ๋ชจ์Šต, ๊ฑฐํ’ˆ์— ๋‘˜๋Ÿฌ์‹ธ์ธ ์ฑ„ ์‚ด์ง ์ –์€ ๋ชจ์Šต์œผ๋กœ ์นด๋ฉ”๋ผ๋ฅผ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ์Œ",
203
  ],
204
  inputs=[prompt],
205
  outputs=[output_img],
 
208
 
209
  polish_button.click(get_image, inputs=[height, width, seed, prompt, cfg, timesteps, stage_a_tiled], outputs=output_img)
210
  polish_button.click(clear_image, inputs=[], outputs=output_img)
 
 
211
 
212
  def download_with_wget(url, save_path):
 
213
  try:
214
  subprocess.run(['wget', url, '-O', save_path], check=True)
215
  print(f"Downloaded to {save_path}")
216
  except subprocess.CalledProcessError as e:
217
  print(f"Error downloading file: {e}")
218
+
219
  def download_model():
 
220
  urls = [
221
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/stage_a.safetensors',
222
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/previewer.safetensors',
223
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/effnet_encoder.safetensors',
224
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/stage_b_lite_bf16.safetensors',
225
  'https://huggingface.co/stabilityai/StableWurst/resolve/main/stage_c_bf16.safetensors',
 
226
  ]
227
  for file_url in urls:
228
  hf_hub_download(repo_id="stabilityai/stable-cascade", filename=file_url.split('/')[-1], local_dir='models')
 
229
  hf_hub_download(repo_id="roubaofeipi/UltraPixel", filename='ultrapixel_t2i.safetensors', local_dir='models')
230
 
231
  if __name__ == "__main__":
 
232
  args = parse_args()
233
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
234
  download_model()
 
268
  models.generator.eval()
269
  models.train_norm.eval()
270
 
271
+ demo.launch(debug=True, share=True, auth=("gini","pick"))