Spaces:
Sleeping
Sleeping
File size: 5,759 Bytes
3ed80a5 b0a0e6f 3ed80a5 bba17cc 3ed80a5 bba17cc 3ed80a5 b0a0e6f 3ed80a5 b0a0e6f 3ed80a5 bba17cc 3ed80a5 bba17cc 3ed80a5 bba17cc 3ed80a5 |
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 192 193 194 195 196 197 198 199 200 201 202 203 |
import gradio as gr
import torch
import numpy as np
from PIL import Image
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
MAX_IMAGES = 1
def generate_images(
type1: str,
type2: str,
hp_num: int,
attack_num: int,
defense_num: int,
sp_attack_num: int,
sp_defense_num: int,
speed_num: int,
) -> list:
"""Generates a sprite based on the input stats.
Parameters
----------
Returns
-------
list
List of PIL images.
"""
# Initalize the images list
images_list = []
# Calculate the base total
base_total = (
hp_num + attack_num + defense_num + sp_attack_num + sp_defense_num + speed_num
)
# Create the text prompt
prompt = f"type1: {type1}, type2: {type2}, base_total: {base_total}, hp: {hp_num}, attack: {attack_num}, defense: {defense_num}, sp_attack: {sp_attack_num}, sp_defense: {sp_defense_num}, speed: {speed_num}"
# Generate the images
for _ in range(MAX_IMAGES):
image = pipe(
prompt,
height=288,
width=288,
num_inference_steps=10,
guidance_scale=7.5,
cross_attention_kwargs={"scale": 1.0},
).images[0]
images_list.append(Image.fromarray(np.array(image)))
return images_list
# Create the demo interface
demo = gr.Blocks()
# Set the models to load
model_base = "stabilityai/stable-diffusion-2-base"
lora_model_path = "michaelriedl/MonsterForgeFusion-sd-2-base"
# Create the pipeline
pipe = StableDiffusionPipeline.from_pretrained(
model_base, torch_dtype=torch.float32, use_safetensors=False, local_files_only=False
)
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe.unet.load_attn_procs(lora_model_path)
# 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 MonsterForgeFusion models. This was built with LoRA fine-tuning of Stable Diffusion models.
</p>
</div>
"""
)
with gr.Column():
with gr.Row():
gallery = gr.Gallery(
columns=MAX_IMAGES, preview=True, object_fit="scale-down"
)
with gr.Row():
type1 = gr.Dropdown(
[
"bug",
"dark",
"dragon",
"electric",
"fairy",
"fighting",
"fire",
"flying",
"ghost",
"grass",
"ground",
"ice",
"normal",
"poison",
"psychic",
"rock",
"steel",
"water",
],
value="steel",
label="Type 1",
)
type2 = gr.Dropdown(
[
"bug",
"dark",
"dragon",
"electric",
"fairy",
"fighting",
"fire",
"flying",
"ghost",
"grass",
"ground",
"ice",
"normal",
"poison",
"psychic",
"rock",
"steel",
"water",
],
value="fire",
label="Type 2",
)
with gr.Row():
hp_num = gr.Slider(
minimum=1,
maximum=100,
value=50,
step=1,
label="HP",
)
attack_num = gr.Slider(
minimum=1,
maximum=100,
value=50,
step=1,
label="Attack",
)
with gr.Row():
defense_num = gr.Slider(
minimum=1,
maximum=100,
value=50,
step=1,
label="Defense",
)
sp_attack_num = gr.Slider(
minimum=1,
maximum=100,
value=50,
step=1,
label="Special Attack",
)
with gr.Row():
sp_defense_num = gr.Slider(
minimum=1,
maximum=100,
value=50,
step=1,
label="Special Defense",
)
speed_num = gr.Slider(
minimum=1,
maximum=100,
value=50,
step=1,
label="Speed",
)
gen_btn = gr.Button("Generate")
gen_btn.click(
fn=generate_images,
inputs=[
type1,
type2,
hp_num,
attack_num,
defense_num,
sp_attack_num,
sp_defense_num,
speed_num,
],
outputs=gallery,
)
gr.HTML(
"""
<div class="footer">
<div style='text-align: center;'>MonsterForgeFusion by <a href='https://michaelriedl.com/' target='_blank'>Michael Riedl</a></div>
</div>
"""
)
# Launch the interface
demo.launch()
|