Spaces:
Running
Running
File size: 6,150 Bytes
1dd4254 14fc1e9 1dd4254 14fc1e9 4e39abd 14fc1e9 1dd4254 14fc1e9 1dd4254 14fc1e9 1dd4254 4e39abd 14fc1e9 1dd4254 14fc1e9 1dd4254 4e39abd 1dd4254 4e39abd 1dd4254 4e39abd 1dd4254 14fc1e9 1dd4254 14fc1e9 4e39abd 1dd4254 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
import gradio as gr
from transformers import AutoModel
import torch
import numpy as np
from PIL import Image
MAX_IMAGES = 4
def generate_small(color_indexed: bool, color_num: int) -> list:
"""Generates a small sprite.
Parameters
----------
color_indexed : bool
Whether to use color indexing.
color_num : int
Number of colors in the palette.
Returns
-------
list
List of PIL images.
"""
# Get the latent dimension
latent_dim = model_small.model.latent_dim
# Initialize the list of images
images_list = []
# Generate MAX_IMAGES images
for _ in range(MAX_IMAGES):
# Generate a random latent vector
latents = torch.randn((1, latent_dim))
# Generate the image
with torch.no_grad():
generated_image = model_small(latents)
# Clamp the image to [0, 1]
generated_image = generated_image.clamp_(0.0, 1.0).cpu().numpy()
# Convert the generated image to PIL image
color_image = Image.fromarray(
np.uint8(generated_image[0] * 255).transpose(1, 2, 0), "RGB"
)
# Convert to color indexed image if needed
if color_indexed:
# Convert using adaptive palette of given color depth
color_image_indexed = color_image.convert(
"P", palette=Image.ADAPTIVE, colors=color_num
)
# Add the color indexed image to the list
images_list.append(color_image_indexed)
# Add the image to the list
images_list.append(color_image)
return images_list
def generate_med(color_indexed: bool, color_num: int) -> list:
"""Generates a medium sprite.
Parameters
----------
color_indexed : bool
Whether to use color indexing.
color_num : int
Number of colors in the palette.
Returns
-------
list
List of PIL images.
"""
# Get the latent dimension
latent_dim = model_med.model.latent_dim
# Initialize the list of images
images_list = []
# Generate MAX_IMAGES images
for _ in range(MAX_IMAGES):
# Generate a random latent vector
latents = torch.randn((1, latent_dim))
# Generate the image
with torch.no_grad():
generated_image = model_med(latents)
# Clamp the image to [0, 1]
generated_image = generated_image.clamp_(0.0, 1.0).cpu().numpy()
# Convert the generated image to PIL image
color_image = Image.fromarray(
np.uint8(generated_image[0] * 255).transpose(1, 2, 0), "RGBA"
)
# Convert to color indexed image if needed
if color_indexed:
# Convert using adaptive palette of given color depth
color_image_indexed = color_image.convert(
"P", palette=Image.ADAPTIVE, colors=color_num
)
# Add the color indexed image to the list
images_list.append(color_image_indexed)
# Add the image to the list
images_list.append(color_image)
return images_list
# Create the demo interface
demo = gr.Blocks()
# Create the small model
model_small = AutoModel.from_pretrained(
"michaelriedl/MonsterForge-small", trust_remote_code=True
)
model_small.eval()
# Create the medium model
model_med = AutoModel.from_pretrained(
"michaelriedl/MonsterForge-medium", trust_remote_code=True
)
model_med.eval()
# Create the interface
with demo:
gr.HTML(
"""
<div style="text-align: center; margin: 0 auto;">
<p style="margin-bottom: 14px; line-height: 23px;">
Gradio demo for MonsterForge models. This was built with Lightweight GAN using the implementation from <a href='https://github.com/lucidrains/lightweight-gan' target='_blank'>lucidrains</a>.
</p>
</div>
"""
)
with gr.Tabs():
with gr.TabItem("Small Sprite"):
with gr.Column():
with gr.Row():
gallery_small = gr.Gallery(
columns=4,
object_fit="scale-down",
)
with gr.Row():
color_index_small = gr.Checkbox(label="Color indexed", value=False)
color_num_small = gr.Slider(
minimum=8,
maximum=32,
value=32,
step=4,
label="Number of colors in the palette",
)
gen_btn_small = gr.Button("Generate")
gen_btn_small.click(
fn=generate_small,
inputs=[color_index_small, color_num_small],
outputs=gallery_small,
)
with gr.TabItem("Medium Sprite"):
with gr.Column():
with gr.Row():
gallery_med = gr.Gallery(
columns=4,
object_fit="scale-down",
)
with gr.Row():
color_index_med = gr.Checkbox(label="Color indexed", value=False)
color_num_med = gr.Slider(
minimum=8,
maximum=32,
value=32,
step=4,
label="Number of colors in the palette",
)
gen_btn_med = gr.Button("Generate")
gen_btn_med.click(
fn=generate_med,
inputs=[color_index_med, color_num_med],
outputs=gallery_med,
)
gr.HTML(
"""
<div class="footer">
<div style='text-align: center;'>MonsterForge by <a href='https://michaelriedl.com/' target='_blank'>Michael Riedl</a></div>
</div>
"""
)
# Launch the interface
demo.launch()
|