Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,186 +1,97 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
| 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 |
-
state = gr.State(value=[]) # For gallery history
|
| 55 |
-
|
| 56 |
-
with gr.Row():
|
| 57 |
-
with gr.Column(scale=1):
|
| 58 |
-
with gr.Accordion("🎛️ Generation Settings", open=True):
|
| 59 |
-
prompt = gr.Textbox(
|
| 60 |
-
label="Prompt",
|
| 61 |
-
placeholder="e.g., steve with diamond armor, cyberpunk creeper",
|
| 62 |
-
lines=2,
|
| 63 |
-
scale=3
|
| 64 |
-
)
|
| 65 |
-
with gr.Row():
|
| 66 |
-
model_dropdown = gr.Dropdown(
|
| 67 |
-
choices=["xl", "2"],
|
| 68 |
-
value="xl",
|
| 69 |
-
label="Model",
|
| 70 |
-
info="XL understands prompts better"
|
| 71 |
-
)
|
| 72 |
-
precision_dropdown = gr.Dropdown(
|
| 73 |
-
choices=["fp16", "fp32"],
|
| 74 |
-
value="fp16",
|
| 75 |
-
label="Precision"
|
| 76 |
-
)
|
| 77 |
-
with gr.Row():
|
| 78 |
-
steps_slider = gr.Slider(1, 50, value=25, step=1, label="Inference Steps")
|
| 79 |
-
guidance_slider = gr.Slider(0.0, 10.0, value=7.5, step=0.1, label="Guidance Scale")
|
| 80 |
-
with gr.Row():
|
| 81 |
-
seed_slider = gr.Slider(0, 2147483647, value=42, step=1, label="Seed", interactive=True)
|
| 82 |
-
random_btn = gr.Button("🎲 Random Seed", scale=0, min_width=120)
|
| 83 |
-
filename_tb = gr.Textbox("output-skin.png", label="Filename (.png)", interactive=True)
|
| 84 |
-
gen_btn = gr.Button("Generate Skin! 🚀", variant="primary", scale=2)
|
| 85 |
-
adv_cb = gr.CheckboxGroup(
|
| 86 |
-
choices=["3D Model", "Verbose"],
|
| 87 |
-
label="Advanced Options",
|
| 88 |
-
value=["3D Model"]
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
gr.Examples(
|
| 92 |
-
examples=[
|
| 93 |
-
["steve with diamond armor"],
|
| 94 |
-
["alex as cyberpunk hacker"],
|
| 95 |
-
["creeper with sunglasses"],
|
| 96 |
-
["enderman in space suit"],
|
| 97 |
-
["zombie knight armor"]
|
| 98 |
-
],
|
| 99 |
-
inputs=prompt
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
gr.Markdown("### 📱 Cool Features")
|
| 103 |
-
gr.Markdown("""
|
| 104 |
-
- **Pixelated Minecraft Style** preview
|
| 105 |
-
- **3D Model Viewer** with rotations
|
| 106 |
-
- **Random Seed** for variety
|
| 107 |
-
- **History Gallery** of generations
|
| 108 |
-
- **Batch Mode** coming soon!
|
| 109 |
-
""")
|
| 110 |
-
|
| 111 |
-
with gr.Column(scale=2):
|
| 112 |
-
with gr.Group(elem_classes="checkered"):
|
| 113 |
-
skin_img = gr.Image(
|
| 114 |
-
label="Generated Skin",
|
| 115 |
-
elem_id="mc-skin",
|
| 116 |
-
elem_classes="pixelated"
|
| 117 |
-
)
|
| 118 |
-
model_3d = gr.Model3D(
|
| 119 |
-
label="3D Preview",
|
| 120 |
-
clear_color=[0.1, 0.2, 0.3, 1.0],
|
| 121 |
-
elem_id="mc-model"
|
| 122 |
-
)
|
| 123 |
-
gallery = gr.Gallery(
|
| 124 |
-
label="Generation History",
|
| 125 |
-
columns=3,
|
| 126 |
-
height=300,
|
| 127 |
-
visible=True
|
| 128 |
-
)
|
| 129 |
-
|
| 130 |
-
# Events
|
| 131 |
-
random_btn.click(random_seed, outputs=seed_slider)
|
| 132 |
-
|
| 133 |
-
gen_btn.click(
|
| 134 |
-
fn=run_inference,
|
| 135 |
-
inputs=[
|
| 136 |
-
prompt,
|
| 137 |
-
model_dropdown,
|
| 138 |
-
steps_slider,
|
| 139 |
-
guidance_slider,
|
| 140 |
-
precision_dropdown,
|
| 141 |
-
seed_slider,
|
| 142 |
-
filename_tb,
|
| 143 |
-
gr.State(lambda x: "3D Model" in x)(adv_cb),
|
| 144 |
-
gr.State(lambda x: "Verbose" in x)(adv_cb)
|
| 145 |
-
],
|
| 146 |
-
outputs=[skin_img, model_3d],
|
| 147 |
-
api_name="generate_skin",
|
| 148 |
-
api_visibility="public",
|
| 149 |
-
show_progress="full"
|
| 150 |
-
).then(
|
| 151 |
-
fn=lambda img_path, history: (history + [img_path] if img_path else history)[:12],
|
| 152 |
-
inputs=[skin_img, state],
|
| 153 |
-
outputs=[gallery, state]
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
# Clear button
|
| 157 |
-
gr.ClearButton([prompt, skin_img, model_3d, gallery, state], value="🗑️ Clear All")
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import subprocess
|
| 2 |
import os
|
| 3 |
+
import sys
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import torch
|
| 6 |
+
import numpy as np
|
| 7 |
+
from PIL import Image, ImageEnhance
|
| 8 |
+
import spaces
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
if torch.cuda.is_available():
|
| 12 |
+
device = "cuda"
|
| 13 |
+
print("Using GPU")
|
| 14 |
+
else:
|
| 15 |
+
device = "cpu"
|
| 16 |
+
print("Using CPU")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
subprocess.run(["git", "clone", "https://github.com/Nick088Official/Minecraft_Skin_Generator.git"])
|
| 23 |
+
|
| 24 |
+
os.chdir("Minecraft_Skin_Generator")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@spaces.GPU(duration=75)
|
| 28 |
+
def run_inference(prompt, stable_diffusion_model, num_inference_steps, guidance_scale, model_precision_type, seed, filename, model_3d, verbose):
|
| 29 |
+
|
| 30 |
+
# Determine which Stable Diffusion model to use
|
| 31 |
+
if stable_diffusion_model == '2':
|
| 32 |
+
sd_model = "minecraft-skins"
|
| 33 |
+
elif stable_diffusion_model == 'xl':
|
| 34 |
+
sd_model = "minecraft-skins-sdxl"
|
| 35 |
+
|
| 36 |
+
# Build the command as a list of argumenta
|
| 37 |
+
inference_command_args = [
|
| 38 |
+
sys.executable,
|
| 39 |
+
f"Scripts/{sd_model}.py",
|
| 40 |
+
prompt,
|
| 41 |
+
str(num_inference_steps),
|
| 42 |
+
str(guidance_scale),
|
| 43 |
+
model_precision_type,
|
| 44 |
+
str(seed),
|
| 45 |
+
filename
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
# Conditionally add flags if requested
|
| 49 |
+
if model_3d:
|
| 50 |
+
inference_command_args.append('--model_3d')
|
| 51 |
+
if verbose:
|
| 52 |
+
inference_command_args.append('--verbose')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Run the command using subprocess
|
| 55 |
+
subprocess.run(inference_command_args, check=True)
|
| 56 |
+
|
| 57 |
+
# Return paths to the output files
|
| 58 |
+
if model_3d:
|
| 59 |
+
return os.path.join(f"output_minecraft_skins/{filename}"), os.path.join(f"output_minecraft_skins/{filename}_3d_model.glb")
|
| 60 |
+
else:
|
| 61 |
+
return os.path.join(f"output_minecraft_skins/{filename}"), None
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Define Gradio UI components
|
| 65 |
+
prompt = gr.Textbox(label="Your Prompt", info="What the Minecraft Skin should look like")
|
| 66 |
+
stable_diffusion_model = gr.Dropdown(['2', 'xl'], value="xl", label="Stable Diffusion Model", info="Choose which Stable Diffusion Model to use, xl understands prompts better")
|
| 67 |
+
num_inference_steps = gr.Slider(label="Number of Inference Steps", info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference", minimum=1, maximum=50, value=25, step=1)
|
| 68 |
+
guidance_scale = gr.Slider(label="Guidance Scale", info="Controls how much the image generation process follows the text prompt. Higher values make the image stick more closely to the input text.", minimum=0.0, maximum=10.0, value=7.5, step=0.1)
|
| 69 |
+
model_precision_type = gr.Dropdown(["fp16", "fp32"], value="fp16", label="Model Precision Type", info="The precision type to load the model, like fp16 which is faster, or fp32 which is more precise but more resource consuming")
|
| 70 |
+
seed = gr.Slider(value=42, minimum=0, maximum=MAX_SEED, step=1, label="Seed", info="A starting point to initiate the generation process, put 0 for a random one")
|
| 71 |
+
filename = gr.Textbox(label="Output Image Name", info="The name of the file of the output image skin, keep the.png", value="output-skin.png")
|
| 72 |
+
model_3d = gr.Checkbox(label="See as 3D Model too", info="View the generated skin as a 3D Model too", value=True)
|
| 73 |
+
verbose = gr.Checkbox(label="Verbose Output", info="Produce more detailed output while running", value=False)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# Create the Gradio interface
|
| 77 |
+
gr.Interface(
|
| 78 |
+
fn=run_inference,
|
| 79 |
+
inputs=[
|
| 80 |
+
prompt,
|
| 81 |
+
stable_diffusion_model,
|
| 82 |
+
num_inference_steps,
|
| 83 |
+
guidance_scale,
|
| 84 |
+
model_precision_type,
|
| 85 |
+
seed,
|
| 86 |
+
filename,
|
| 87 |
+
model_3d,
|
| 88 |
+
verbose
|
| 89 |
+
],
|
| 90 |
+
outputs=[
|
| 91 |
+
gr.Image(label="Generated Minecraft Skin Image Asset", elem_classes="pixelated checkered"),
|
| 92 |
+
gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model View of the Skin")
|
| 93 |
+
],
|
| 94 |
+
title="Minecraft Skin Generator",
|
| 95 |
+
description="Make AI generated Minecraft Skins by a Finetuned Stable Diffusion Version!<br>Github Repository & Model used: https://github.com/Nick088Official/Minecraft_Skin_Generator<br>Credits: [Monadical-SAS](https://github.com/Monadical-SAS/minecraft_skin_generator) (Creators of the model), [Nick088](https://linktr.ee/Nick088) (Improving usage of the model), daroche (helping me fix the 3d model texture isue), [Brottweiler](https://gist.github.com/Brottweiler/483d0856c6692ef70cf90bf1a85ce364)(script to fix the 3d model texture), [not-holar](https://huggingface.co/not-holar) (made the rendering of the image asset in the web ui look pixelated like minecraft and have a checkered background),[meew](https://huggingface.co/spaces/meeww/Minecraft_Skin_Generator/blob/main/models/player_model.glb) (Minecraft Player 3d model) <br> [](https://discord.gg/AQsmBmgEPy)",
|
| 96 |
+
css=".pixelated {image-rendering: pixelated} .checkered img {background-image: url(\'data:image/svg+xml,<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"2\" height=\"2\" fill-opacity=\".15\"><rect x=\"1\" width=\"1\" height=\"1\"/><rect y=\"1\" width=\"1\" height=\"1\"/></svg>\');background-size: 16px;}"
|
| 97 |
+
).launch(show_api=False, share=True)
|