WiNE-iNEFF commited on
Commit
20f34a3
·
1 Parent(s): 377afdc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -35
app.py CHANGED
@@ -25,7 +25,14 @@ def show_images_save(x):
25
  grid = torchvision.utils.make_grid(x, nrow=4, padding=0)
26
  grid_im = grid.detach().cpu().permute(1, 2, 0).clip(0, 1) * 255
27
  grid_im = Image.fromarray(np.array(grid_im).astype(np.uint8))
28
- return grid_im
 
 
 
 
 
 
 
29
 
30
  def color_loss(images, target_color=(0, 0, 0, 1)):
31
  target = (torch.tensor(target_color).to(images.device) * 2 - 1)
@@ -34,6 +41,7 @@ def color_loss(images, target_color=(0, 0, 0, 1)):
34
  return error
35
 
36
  def generate():
 
37
  x = torch.randn(8, 4, 64, 64).to(device)
38
  for i, t in enumerate(scheduler.timesteps):
39
  model_input = scheduler.scale_model_input(x, t)
@@ -43,7 +51,7 @@ def generate():
43
  return show_images_save(x)
44
 
45
  def generate_g(color, guidance_loss_scale):
46
- print('--V2--')
47
  target_color = ImageColor.getcolor(color, "RGBA") # Target color as RGB
48
  target_color = [a / 255 for a in target_color] # Rescale from (0, 255) to (0, 1)
49
  x = torch.randn(8, 4, 64, 64).to(device)
@@ -59,54 +67,34 @@ def generate_g(color, guidance_loss_scale):
59
  x = scheduler.step(noise_pred, t, x).prev_sample
60
  return show_images_save(x)
61
 
62
- def crrop(file):
63
- width, height = file.size
64
- sav = []
65
- for w in range(width//64):
66
- for h in range(height//64):
67
- box = (w*64, h*64, (w+1)*64, (h+1)*64)
68
- sav.append(file.crop(box))
69
- return sav
70
-
71
- def ex():
72
- t = time()
73
- print(ctime(t))
74
- return crrop(generate())
75
-
76
- def ex_g(picker, slider):
77
- t = time()
78
- print(ctime(t))
79
- return crrop(generate_g(picker, slider))
80
-
81
- demo = gr.Blocks(css=".container {max-width: 730px;margin: auto;} #gallery {margin-bottom: 15px; margin-left: auto; margin-right: auto; border-bottom-right-radius: .5rem !important; border-bottom-left-radius: .5rem !important;}")
82
-
83
  with demo:
84
  gr.HTML(
85
  """
86
  <div style="text-align: center; margin: 0 auto;">
87
- <div style="display: inline-flex;align-items: center;gap: 0.8rem;font-size: 1.75rem;">
88
- <img src='https://huggingface.co/spaces/WiNE-iNEFF/MinecraftSkin-Diffusion/resolve/main/MSD_7.png'>
89
  </div>
90
  <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
91
- Gradio demo for Minecraft Skin Diffusion. This is simple Unconditional Diffusion Model that will help you generate skins for game Minecraft.
92
  </p>
93
  </div>
94
  """)
95
  with gr.Tabs():
96
  with gr.TabItem("V1"):
97
  with gr.Column():
98
- with gr.Row().style(equal_height=True):
99
- gall = gr.Gallery(elem_id='gallery').style(grid=[4])
100
  greet_btn = gr.Button("Generate")
101
- greet_btn.click(fn=ex, outputs=gall)
102
  with gr.TabItem("V2"):
103
- with gr.Column():
104
- with gr.Row().style(equal_height=True):
105
- picker = gr.ColorPicker(label="color", value="#55FFAA")
106
  slide = gr.Slider(label="guidance_scale", minimum=0, maximum=100, value=50)
107
- gall = gr.Gallery(elem_id='gallery').style(grid=[4])
108
- greet_btn = gr.Button("Generate")
109
- greet_btn.click(fn=ex_g, inputs=[picker, slide], outputs=gall)
 
110
  gr.HTML(
111
  """
112
  <div class="footer">
 
25
  grid = torchvision.utils.make_grid(x, nrow=4, padding=0)
26
  grid_im = grid.detach().cpu().permute(1, 2, 0).clip(0, 1) * 255
27
  grid_im = Image.fromarray(np.array(grid_im).astype(np.uint8))
28
+ #CROP
29
+ width, height = grid_im.size
30
+ sav = []
31
+ for w in range(width//64):
32
+ for h in range(height//64):
33
+ box = (w*64, h*64, (w+1)*64, (h+1)*64)
34
+ sav.append(grid_im.crop(box))
35
+ return sav
36
 
37
  def color_loss(images, target_color=(0, 0, 0, 1)):
38
  target = (torch.tensor(target_color).to(images.device) * 2 - 1)
 
41
  return error
42
 
43
  def generate():
44
+ print(ctime(time()))
45
  x = torch.randn(8, 4, 64, 64).to(device)
46
  for i, t in enumerate(scheduler.timesteps):
47
  model_input = scheduler.scale_model_input(x, t)
 
51
  return show_images_save(x)
52
 
53
  def generate_g(color, guidance_loss_scale):
54
+ print(f'--V2-- {ctime(time())}')
55
  target_color = ImageColor.getcolor(color, "RGBA") # Target color as RGB
56
  target_color = [a / 255 for a in target_color] # Rescale from (0, 255) to (0, 1)
57
  x = torch.randn(8, 4, 64, 64).to(device)
 
67
  x = scheduler.step(noise_pred, t, x).prev_sample
68
  return show_images_save(x)
69
 
70
+ demo = gr.Blocks(css=".tabs {max-width: 60%; margin-left: 20%; margin-right: 20%;} #column {max-width: 50%} .fixed-height {min-height: 0px !important} @media(min-width: 1280px){.fixed-height {min-height: 0px !important}}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  with demo:
72
  gr.HTML(
73
  """
74
  <div style="text-align: center; margin: 0 auto;">
75
+ <div style="display: inline-flex; align-items: center; justify-content: center;">
76
+ <img src='https://huggingface.co/spaces/WiNE-iNEFF/MinecraftSkin-Diffusion/resolve/main/MSD_7.png' width='45%'>
77
  </div>
78
  <p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
79
+ Gradio demo for Minecraft Skin Diffusion. This is simple Unconditional Diffusion Model<br>that can generate skins for game Minecraft.
80
  </p>
81
  </div>
82
  """)
83
  with gr.Tabs():
84
  with gr.TabItem("V1"):
85
  with gr.Column():
86
+ gall = gr.Gallery().style(grid=[4])
 
87
  greet_btn = gr.Button("Generate")
88
+ greet_btn.click(fn=generate, outputs=gall)
89
  with gr.TabItem("V2"):
90
+ with gr.Row():
91
+ with gr.Column(min_width = 0, elem_id='column'):
92
+ picker = gr.ColorPicker(label="color", value="#55FFAA", min_width='0')
93
  slide = gr.Slider(label="guidance_scale", minimum=0, maximum=100, value=50)
94
+ greet_btn = gr.Button("Generate", min_width = '0')
95
+ with gr.Column(min_width = 0):
96
+ gall = gr.Gallery(min_width = '250').style(grid=[3])
97
+ greet_btn.click(fn=generate_g, inputs=[picker, slide], outputs=gall)
98
  gr.HTML(
99
  """
100
  <div class="footer">