Spaces:
Running
Running
Commit
·
dfef42f
1
Parent(s):
ca910fd
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,46 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch, torchvision
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image, ImageColor
|
| 6 |
+
from diffusers import DDPMPipeline
|
| 7 |
+
from diffusers import DDIMScheduler
|
| 8 |
|
| 9 |
+
device = 'mps' if torch.backends.mps.is_available() else 'cuda' if torch.cuda.is_available() else 'cpu'
|
|
|
|
| 10 |
|
| 11 |
+
# Load the pretrained pipeline
|
| 12 |
+
pipeline_name = 'WiNE-iNEFF/Minecraft-Skin-Diffusion'
|
| 13 |
+
image_pipe = DDPMPipeline.from_pretrained(pipeline_name).to(device)
|
| 14 |
+
|
| 15 |
+
# Set up the scheduler
|
| 16 |
+
scheduler = DDIMScheduler.from_pretrained(pipeline_name)
|
| 17 |
+
scheduler.set_timesteps(num_inference_steps=40)
|
| 18 |
+
|
| 19 |
+
# And the core function to generate an image given the relevant inputs
|
| 20 |
+
def generate():
|
| 21 |
+
x = torch.randn(8, 4, 64, 64).to(device)
|
| 22 |
+
# Minimal sampling loop
|
| 23 |
+
for i, t in tqdm(enumerate(scheduler.timesteps)):
|
| 24 |
+
model_input = scheduler.scale_model_input(x, t)
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
noise_pred = image_pipe.unet(model_input, t)["sample"]
|
| 27 |
+
x = scheduler.step(noise_pred, t, x).prev_sample
|
| 28 |
+
# View the results
|
| 29 |
+
grid = torchvision.utils.make_grid(x, nrow=4)
|
| 30 |
+
im = grid.permute(1, 2, 0).cpu().clip(-1, 1) * 0.5 + 0.5
|
| 31 |
+
im.convert("RGBA").save("test.png")
|
| 32 |
+
return im
|
| 33 |
+
|
| 34 |
+
# See the gradio docs for the types of inputs and outputs available
|
| 35 |
+
outputs = gr.Image(label="result")
|
| 36 |
+
|
| 37 |
+
# Setting up a minimal interface to our function:
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=generate,
|
| 40 |
+
inputs=None,
|
| 41 |
+
outputs=outputs,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# And launching
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch(enable_queue=True)
|