Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,42 +4,46 @@ import gradio as gr
|
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image, ImageEnhance
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
if os.environ.get("HF_TOKEN") or os.environ.get("HF_HOME") or os.environ.get("HUGGINGFACE_HUB_CACHE"):
|
| 10 |
-
import spaces
|
| 11 |
-
|
| 12 |
|
| 13 |
# Constants
|
| 14 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
MAX_SEED = np.iinfo(np.int32).max
|
| 16 |
-
OUTPUT_DIR = "output_minecraft_skins"
|
| 17 |
|
| 18 |
print(f"Using: {DEVICE}")
|
| 19 |
|
| 20 |
# Repo URL
|
| 21 |
REPO_URL = "https://github.com/Nick088Official/Minecraft_Skin_Generator.git"
|
| 22 |
REPO_NAME = "Minecraft_Skin_Generator"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
try:
|
| 27 |
-
subprocess.run(["git", "clone", REPO_URL], check=True)
|
| 28 |
-
print("Repository cloned successfully.")
|
| 29 |
-
except subprocess.CalledProcessError as e:
|
| 30 |
-
print(f"Error cloning repository: {e}")
|
| 31 |
-
# Handle error, e.g., exit or raise an exception
|
| 32 |
-
else:
|
| 33 |
-
print(f"{REPO_NAME} repository already exists. Skipping clone.")
|
| 34 |
-
|
| 35 |
-
# Change to the repository directory for script execution
|
| 36 |
-
os.chdir(REPO_NAME)
|
| 37 |
|
| 38 |
|
| 39 |
-
if os.environ.get("HF_TOKEN") or os.environ.get("HF_HOME") or os.environ.get("HUGGINGFACE_HUB_CACHE"):
|
| 40 |
-
@spaces.GPU(duration=75)
|
| 41 |
-
else:
|
| 42 |
-
|
| 43 |
def run_inference(
|
| 44 |
prompt: str,
|
| 45 |
stable_diffusion_model: str,
|
|
@@ -96,24 +100,27 @@ def run_inference(
|
|
| 96 |
print(f"An unexpected error occurred: {e}")
|
| 97 |
return None, None
|
| 98 |
|
| 99 |
-
#
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
# Basic check for file existence (can be more robust)
|
| 104 |
-
if not
|
| 105 |
print(f"Warning: Image file not found at {image_path}")
|
| 106 |
image_path = None
|
| 107 |
-
if model_3d and not
|
| 108 |
print(f"Warning: 3D model file not found at {model_3d_path}")
|
| 109 |
model_3d_path = None
|
| 110 |
|
| 111 |
-
return image_path, model_3d_path
|
| 112 |
|
| 113 |
|
| 114 |
def create_gradio_ui():
|
| 115 |
"""Defines and returns the Gradio UI components."""
|
| 116 |
-
with gr.Blocks(title="Minecraft Skin Generator", css=".pixelated {image-rendering: pixelated} .checkered img {background-image: url(\'data:image/svg+xml,<svg xmlns=\
|
| 117 |
gr.Label("Minecraft Skin Generator")
|
| 118 |
gr.Markdown("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)")
|
| 119 |
|
|
@@ -133,7 +140,7 @@ def create_gradio_ui():
|
|
| 133 |
verbose = gr.Checkbox(label="Verbose Output", info="Produce more detailed output while running", value=False)
|
| 134 |
|
| 135 |
generate_skn = gr.Button("Generate")
|
| 136 |
-
image_output = gr.Image(label="Generated Minecraft Skin Image Asset")
|
| 137 |
image3d_output = gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model View of the Skin")
|
| 138 |
|
| 139 |
generate_skn.click(
|
|
@@ -156,6 +163,19 @@ def create_gradio_ui():
|
|
| 156 |
)
|
| 157 |
return imsteve
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
demo.launch(show_api=False, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
from PIL import Image, ImageEnhance
|
| 7 |
+
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Constants
|
| 10 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
MAX_SEED = np.iinfo(np.int32).max
|
| 12 |
+
OUTPUT_DIR = Path("output_minecraft_skins")
|
| 13 |
|
| 14 |
print(f"Using: {DEVICE}")
|
| 15 |
|
| 16 |
# Repo URL
|
| 17 |
REPO_URL = "https://github.com/Nick088Official/Minecraft_Skin_Generator.git"
|
| 18 |
REPO_NAME = "Minecraft_Skin_Generator"
|
| 19 |
+
REPO_PATH = Path(REPO_NAME)
|
| 20 |
+
|
| 21 |
+
def setup_repository():
|
| 22 |
+
"""Clones or updates the Git repository."""
|
| 23 |
+
if not REPO_PATH.exists():
|
| 24 |
+
print(f"Cloning {REPO_NAME} repository...")
|
| 25 |
+
try:
|
| 26 |
+
subprocess.run(["git", "clone", REPO_URL], check=True)
|
| 27 |
+
print("Repository cloned successfully.")
|
| 28 |
+
except subprocess.CalledProcessError as e:
|
| 29 |
+
print(f"Error cloning repository: {e}")
|
| 30 |
+
raise
|
| 31 |
+
else:
|
| 32 |
+
print(f"{REPO_NAME} repository already exists. Checking for updates...")
|
| 33 |
+
try:
|
| 34 |
+
# Change to the repository directory to pull updates
|
| 35 |
+
os.chdir(REPO_PATH)
|
| 36 |
+
subprocess.run(["git", "pull"], check=True)
|
| 37 |
+
print("Repository updated successfully.")
|
| 38 |
+
os.chdir("..") # Change back to original directory
|
| 39 |
+
except subprocess.CalledProcessError as e:
|
| 40 |
+
print(f"Error updating repository: {e}")
|
| 41 |
+
raise
|
| 42 |
|
| 43 |
+
# Change to the repository directory for script execution
|
| 44 |
+
os.chdir(REPO_PATH)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def run_inference(
|
| 48 |
prompt: str,
|
| 49 |
stable_diffusion_model: str,
|
|
|
|
| 100 |
print(f"An unexpected error occurred: {e}")
|
| 101 |
return None, None
|
| 102 |
|
| 103 |
+
# Ensure output directory exists
|
| 104 |
+
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 105 |
+
|
| 106 |
+
# Construct output paths using pathlib
|
| 107 |
+
image_path = OUTPUT_DIR / filename
|
| 108 |
+
model_3d_path = OUTPUT_DIR / f"{filename.split('.')[0]}_3d_model.glb" if model_3d else None
|
| 109 |
|
| 110 |
# Basic check for file existence (can be more robust)
|
| 111 |
+
if not image_path.exists():
|
| 112 |
print(f"Warning: Image file not found at {image_path}")
|
| 113 |
image_path = None
|
| 114 |
+
if model_3d and model_3d_path and not model_3d_path.exists():
|
| 115 |
print(f"Warning: 3D model file not found at {model_3d_path}")
|
| 116 |
model_3d_path = None
|
| 117 |
|
| 118 |
+
return str(image_path) if image_path else None, str(model_3d_path) if model_3d_path else None
|
| 119 |
|
| 120 |
|
| 121 |
def create_gradio_ui():
|
| 122 |
"""Defines and returns the Gradio UI components."""
|
| 123 |
+
with gr.Blocks(title="Minecraft Skin Generator", 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;}") as imsteve:
|
| 124 |
gr.Label("Minecraft Skin Generator")
|
| 125 |
gr.Markdown("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)")
|
| 126 |
|
|
|
|
| 140 |
verbose = gr.Checkbox(label="Verbose Output", info="Produce more detailed output while running", value=False)
|
| 141 |
|
| 142 |
generate_skn = gr.Button("Generate")
|
| 143 |
+
image_output = gr.Image(label="Generated Minecraft Skin Image Asset")
|
| 144 |
image3d_output = gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model View of the Skin")
|
| 145 |
|
| 146 |
generate_skn.click(
|
|
|
|
| 163 |
)
|
| 164 |
return imsteve
|
| 165 |
|
| 166 |
+
def main():
|
| 167 |
+
# Handle Hugging Face Spaces environment
|
| 168 |
+
if os.environ.get("HF_TOKEN") or os.environ.get("HF_HOME") or os.environ.get("HUGGINGFACE_HUB_CACHE"):
|
| 169 |
+
import spaces
|
| 170 |
+
@spaces.GPU(duration=75)
|
| 171 |
+
def wrapped_create_gradio_ui():
|
| 172 |
+
return create_gradio_ui()
|
| 173 |
+
demo = wrapped_create_gradio_ui()
|
| 174 |
+
else:
|
| 175 |
+
demo = create_gradio_ui()
|
| 176 |
+
|
| 177 |
+
setup_repository()
|
| 178 |
demo.launch(show_api=False, share=True)
|
| 179 |
+
|
| 180 |
+
if __name__ == "__main__":
|
| 181 |
+
main()
|