Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,18 @@ import torch
|
|
5 |
import numpy as np
|
6 |
from PIL import Image, ImageEnhance
|
7 |
import spaces
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
if torch.cuda.is_available():
|
11 |
device = "cuda"
|
@@ -14,19 +25,14 @@ else:
|
|
14 |
device = "cpu"
|
15 |
print("Using CPU")
|
16 |
|
17 |
-
|
18 |
MAX_SEED = np.iinfo(np.int32).max
|
19 |
|
20 |
-
|
21 |
subprocess.run(["git", "clone", "https://github.com/Nick088Official/Stable_Diffusion_Finetuned_Minecraft_Skin_Generator.git"])
|
22 |
-
|
23 |
os.chdir("Stable_Diffusion_Finetuned_Minecraft_Skin_Generator")
|
24 |
|
25 |
-
|
26 |
@spaces.GPU(duration=75)
|
27 |
def run_inference(prompt, stable_diffusion_model, num_inference_steps, guidance_scale, model_precision_type, seed, filename, model_3d, verbose):
|
28 |
-
|
29 |
-
# inference
|
30 |
if stable_diffusion_model == '2':
|
31 |
sd_model = "minecraft-skins"
|
32 |
elif stable_diffusion_model == 'xl':
|
@@ -36,12 +42,34 @@ def run_inference(prompt, stable_diffusion_model, num_inference_steps, guidance_
|
|
36 |
|
37 |
os.system(inference_command)
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Define Gradio UI components
|
47 |
prompt = gr.Textbox(label="Your Prompt", info="What the Minecraft Skin should look like")
|
@@ -50,12 +78,15 @@ num_inference_steps = gr.Slider(label="Number of Inference Steps", info="The num
|
|
50 |
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)
|
51 |
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")
|
52 |
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")
|
53 |
-
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")
|
54 |
model_3d = gr.Checkbox(label="See as 3D Model too", info="View the generated skin as a 3D Model too", value=True)
|
55 |
verbose = gr.Checkbox(label="Verbose Output", info="Produce more detailed output while running", value=False)
|
56 |
|
57 |
-
|
58 |
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
59 |
gr.Interface(
|
60 |
fn=run_inference,
|
61 |
inputs=[
|
@@ -70,10 +101,16 @@ gr.Interface(
|
|
70 |
verbose
|
71 |
],
|
72 |
outputs=[
|
73 |
-
|
74 |
-
|
|
|
75 |
],
|
76 |
title="Minecraft Skin Generator",
|
77 |
-
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
|
78 |
-
css=".pixelated {image-rendering: pixelated} .checkered img {background-image: url(
|
79 |
-
).launch(show_api=True, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
5 |
import numpy as np
|
6 |
from PIL import Image, ImageEnhance
|
7 |
import spaces
|
8 |
+
from pymongo import MongoClient
|
9 |
+
from pymongo.errors import ConnectionError
|
10 |
+
|
11 |
+
# MongoDB connection
|
12 |
+
mongo_client = None
|
13 |
+
try:
|
14 |
+
mongo_client = MongoClient("mongodb+srv://skandanv:Skandanv00031@cluster1.aquia.mongodb.net/?retryWrites=true&w=majority&appName=cluster1")
|
15 |
+
db = mongo_client['minecraft_skin_generator'] # Replace with your database name
|
16 |
+
collection = db['generated_skins'] # Collection to store generated skins
|
17 |
+
connection_message = "Connected to MineSkin Server"
|
18 |
+
except ConnectionError:
|
19 |
+
connection_message = "Failed to connect to MineSkin Server"
|
20 |
|
21 |
if torch.cuda.is_available():
|
22 |
device = "cuda"
|
|
|
25 |
device = "cpu"
|
26 |
print("Using CPU")
|
27 |
|
|
|
28 |
MAX_SEED = np.iinfo(np.int32).max
|
29 |
|
|
|
30 |
subprocess.run(["git", "clone", "https://github.com/Nick088Official/Stable_Diffusion_Finetuned_Minecraft_Skin_Generator.git"])
|
|
|
31 |
os.chdir("Stable_Diffusion_Finetuned_Minecraft_Skin_Generator")
|
32 |
|
|
|
33 |
@spaces.GPU(duration=75)
|
34 |
def run_inference(prompt, stable_diffusion_model, num_inference_steps, guidance_scale, model_precision_type, seed, filename, model_3d, verbose):
|
35 |
+
# Inference
|
|
|
36 |
if stable_diffusion_model == '2':
|
37 |
sd_model = "minecraft-skins"
|
38 |
elif stable_diffusion_model == 'xl':
|
|
|
42 |
|
43 |
os.system(inference_command)
|
44 |
|
45 |
+
# File paths for generated assets
|
46 |
+
image_path = os.path.join(f"output_minecraft_skins/{filename}")
|
47 |
+
model_path = os.path.join(f"output_minecraft_skins/{filename}_3d_model.glb") if model_3d else None
|
48 |
+
|
49 |
+
# Prepare data for MongoDB
|
50 |
+
skin_data = {
|
51 |
+
'prompt': prompt,
|
52 |
+
'filename': filename,
|
53 |
+
'image_path': image_path,
|
54 |
+
'model_path': model_path,
|
55 |
+
'num_inference_steps': num_inference_steps,
|
56 |
+
'guidance_scale': guidance_scale,
|
57 |
+
'model_precision_type': model_precision_type,
|
58 |
+
'seed': seed,
|
59 |
+
'model_3d': model_3d,
|
60 |
+
'verbose': verbose
|
61 |
+
}
|
62 |
+
|
63 |
+
# Insert generated skin data into MongoDB and show alert if successful
|
64 |
+
try:
|
65 |
+
collection.insert_one(skin_data)
|
66 |
+
success_message = "The Skin has been pushed to MineSkin Server"
|
67 |
+
alert_type = "success" # Gradio Alert type for success
|
68 |
+
except Exception as e:
|
69 |
+
success_message = f"Failed to push skin to database: {e}"
|
70 |
+
alert_type = "error"
|
71 |
+
|
72 |
+
return image_path, model_path, success_message, alert_type
|
73 |
|
74 |
# Define Gradio UI components
|
75 |
prompt = gr.Textbox(label="Your Prompt", info="What the Minecraft Skin should look like")
|
|
|
78 |
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)
|
79 |
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")
|
80 |
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")
|
81 |
+
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")
|
82 |
model_3d = gr.Checkbox(label="See as 3D Model too", info="View the generated skin as a 3D Model too", value=True)
|
83 |
verbose = gr.Checkbox(label="Verbose Output", info="Produce more detailed output while running", value=False)
|
84 |
|
|
|
85 |
# Create the Gradio interface
|
86 |
+
output_image = gr.Image(label="Generated Minecraft Skin Image Asset", elem_classes="pixelated checkered")
|
87 |
+
output_model = gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model View of the Skin")
|
88 |
+
output_message = gr.Alert()
|
89 |
+
|
90 |
gr.Interface(
|
91 |
fn=run_inference,
|
92 |
inputs=[
|
|
|
101 |
verbose
|
102 |
],
|
103 |
outputs=[
|
104 |
+
output_image,
|
105 |
+
output_model,
|
106 |
+
output_message
|
107 |
],
|
108 |
title="Minecraft Skin Generator",
|
109 |
+
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)",
|
110 |
+
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;}"
|
111 |
+
).launch(show_api=True, share=True)
|
112 |
+
|
113 |
+
# Show connection message as alert when the app starts
|
114 |
+
if mongo_client:
|
115 |
+
gr.Interface().launch()
|
116 |
+
output_message.update(value=connection_message, type="info")
|