Spaces:
Running
on
Zero
Running
on
Zero
import subprocess | |
import os | |
import gradio as gr | |
import torch | |
from PIL import Image, ImageEnhance | |
from pygltflib import GLTF2 | |
from pygltflib.utils import ImageFormat, Texture, Material, Image as GLTFImage | |
import sys | |
import spaces | |
if torch.cuda.is_available(): | |
device = "cuda" | |
print("Using GPU") | |
else: | |
device = "cpu" | |
print("Using CPU") | |
subprocess.run(["git", "clone", "https://github.com/Nick088Official/Stable_Diffusion_Finetuned_Minecraft_Skin_Generator.git"]) | |
os.chdir("Stable_Diffusion_Finetuned_Minecraft_Skin_Generator") | |
def run_inference(prompt, stable_diffusion_model, num_inference_steps, guidance_scale, model_precision_type, seed, filename, verbose): | |
if stable_diffusion_model == '2': | |
sd_model = "minecraft-skins" | |
else: | |
sd_model = "minecraft-skins-sdxl" | |
inference_command = f"python Scripts/{sd_model}.py '{prompt}' {num_inference_steps} {guidance_scale} {model_precision_type} {seed} {filename} {'--verbose' if verbose else ''}" | |
os.system(inference_command) | |
# view it in 3d | |
os.chdir("Scripts") | |
command_3d_model = f"python to_3d_model.py '{filename}'" | |
os.system(command_3d_model) | |
os.chdir("..") | |
glb_path = os.path.join(f"output_minecraft_skins/{filename}_3d_model.glb") | |
return os.path.join(f"output_minecraft_skins/{filename}"), glb_path | |
def custom_output(image_path, glb_path): | |
if glb_path is None: | |
return image_path | |
else: | |
return [image_path, gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model", path=glb_path)] | |
with gr.Blocks() as minecraft_skin_generator: | |
with gr.Row(): | |
prompt = gr.Textbox(label="Your Prompt", info="What the Minecraft Skin should look like") | |
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") | |
num_inference_steps = gr.Number(label="Number of Inference Steps", precision=0, value=25) | |
guidance_scale = gr.Number(minimum=0.1, value=7.5, label="Guidance Scale", 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") | |
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 gives better results") | |
seed = gr.Number(value=42, label="Seed", info="A starting point to initiate generation, put 0 for a random one") | |
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") | |
verbose = gr.Checkbox(label="Verbose Output", info="Produce more detailed output while running", value=False) | |
see_in_3d = gr.Checkbox(label="See in 3D", info="View the generated skin in 3D", value=False) | |
image_path, glb_path = run_inference(prompt, stable_diffusion_model, num_inference_steps, guidance_scale, model_precision_type, seed, filename, verbose) | |
with gr.Row(): | |
output = gr.Image(label="Generated Minecraft Skin Image Asset") | |
if see_in_3d: | |
output.style(height=500) | |
output.style(width=500) | |
output.style(display="flex") | |
output.style(justify_content="center") | |
output.style(align_items="center") | |
output.style(flex_wrap="wrap") | |
output.style(grid_template_columns="repeat(auto-fill, minmax(250px, 1fr))") | |
output.style(grid_gap="10px") | |
output.style(overflow="auto") | |
output.style(padding="10px") | |
output.style(box_sizing="border-box") | |
output.style(border="1px solid #ccc") | |
output.style(border_radius="5px") | |
output.style(margin="10px 0") | |
output.style(background_color="#f9f9f9") | |
output.render(custom_output, inputs=[image_path, glb_path]) | |
else: | |
output.render(image_path) | |
minecraft_skin_generator.launch(show_api=False, share=True) |