File size: 6,434 Bytes
1746e54
 
c7f3034
 
68de506
eda856a
9677f4f
44ddca0
 
 
 
 
 
 
 
 
 
 
 
e5b283e
c7f3034
 
 
 
 
 
 
9f6e2bc
 
1746e54
84b72a9
 
fca1a96
f68a1d2
44ddca0
2e597c7
 
360ca6a
2e597c7
4ebdfdd
360ca6a
e5b283e
 
 
44ddca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9836998
 
 
 
9f6e2bc
 
27be1eb
9f6e2bc
44ddca0
30e12e1
4aa0489
1a381cb
4aa0489
44ddca0
 
 
 
4aa0489
 
 
 
 
 
 
 
 
 
360ca6a
4aa0489
 
 
44ddca0
 
 
4aa0489
 
44ddca0
 
 
 
 
 
 
 
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
import subprocess
import os
import gradio as gr
import torch
import numpy as np
from PIL import Image, ImageEnhance
import spaces
from pymongo import MongoClient
from pymongo.errors import ConnectionError

# MongoDB connection
mongo_client = None
try:
    mongo_client = MongoClient("mongodb+srv://skandanv:[email protected]/?retryWrites=true&w=majority&appName=cluster1")
    db = mongo_client['minecraft_skin_generator']  # Replace with your database name
    collection = db['generated_skins']  # Collection to store generated skins
    connection_message = "Connected to MineSkin Server"
except ConnectionError:
    connection_message = "Failed to connect to MineSkin Server"

if torch.cuda.is_available():
    device = "cuda"
    print("Using GPU")
else:
    device = "cpu"
    print("Using CPU")

MAX_SEED = np.iinfo(np.int32).max

subprocess.run(["git", "clone", "https://github.com/Nick088Official/Stable_Diffusion_Finetuned_Minecraft_Skin_Generator.git"])
os.chdir("Stable_Diffusion_Finetuned_Minecraft_Skin_Generator")

@spaces.GPU(duration=75)
def run_inference(prompt, stable_diffusion_model, num_inference_steps, guidance_scale, model_precision_type, seed, filename, model_3d, verbose):
    # Inference
    if stable_diffusion_model == '2':
        sd_model = "minecraft-skins"
    elif stable_diffusion_model == 'xl':
        sd_model = "minecraft-skins-sdxl"

    inference_command = f"python Scripts/{sd_model}.py '{prompt}' {num_inference_steps} {guidance_scale} {model_precision_type} {seed} {filename} {'--model_3d' if model_3d else ''} {'--verbose' if verbose else ''}"
    
    os.system(inference_command)

    # File paths for generated assets
    image_path = os.path.join(f"output_minecraft_skins/{filename}")
    model_path = os.path.join(f"output_minecraft_skins/{filename}_3d_model.glb") if model_3d else None

    # Prepare data for MongoDB
    skin_data = {
        'prompt': prompt,
        'filename': filename,
        'image_path': image_path,
        'model_path': model_path,
        'num_inference_steps': num_inference_steps,
        'guidance_scale': guidance_scale,
        'model_precision_type': model_precision_type,
        'seed': seed,
        'model_3d': model_3d,
        'verbose': verbose
    }

    # Insert generated skin data into MongoDB and show alert if successful
    try:
        collection.insert_one(skin_data)
        success_message = "The Skin has been pushed to MineSkin Server"
        alert_type = "success"  # Gradio Alert type for success
    except Exception as e:
        success_message = f"Failed to push skin to database: {e}"
        alert_type = "error"

    return image_path, model_path, success_message, alert_type

# Define Gradio UI components
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.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)
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)
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")
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")
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")
model_3d = gr.Checkbox(label="See as 3D Model too", info="View the generated skin as a 3D Model too", value=True)
verbose = gr.Checkbox(label="Verbose Output", info="Produce more detailed output while running", value=False)

# Create the Gradio interface
output_image = gr.Image(label="Generated Minecraft Skin Image Asset", elem_classes="pixelated checkered")
output_model = gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model View of the Skin")
output_message = gr.Alert()

gr.Interface(
    fn=run_inference,
    inputs=[
        prompt,
        stable_diffusion_model,
        num_inference_steps,
        guidance_scale,
        model_precision_type,
        seed,
        filename,
        model_3d,
        verbose
    ],
    outputs=[
        output_image,
        output_model,
        output_message
    ],
    title="Minecraft Skin Generator",
    description="Make AI generated Minecraft Skins by a Finetuned Stable Diffusion Version!<br>Github Repository & Model used: https://github.com/Nick088Official/Stable_Diffusion_Finetuned_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 issue), [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> [![Discord](https://img.shields.io/discord/1198701940511617164?color=%23738ADB&label=Discord&style=for-the-badge)](https://discord.gg/AQsmBmgEPy)",
    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;}"
).launch(show_api=True, share=True)

# Show connection message as alert when the app starts
if mongo_client:
    gr.Interface().launch()
    output_message.update(value=connection_message, type="info")