Spaces:
Runtime error
Runtime error
apolinario
commited on
Commit
·
3f98781
1
Parent(s):
36fe8fd
First commit
Browse files- app.py +44 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# !pip install diffusers
|
2 |
+
from diffusers import DiffusionPipeline, DDIMPipeline, DDPMPipeline, PNDMPipeline
|
3 |
+
import gradio as gr
|
4 |
+
import PIL.Image
|
5 |
+
import numpy as np
|
6 |
+
import random
|
7 |
+
import torch
|
8 |
+
|
9 |
+
model_id = "google/ddpm-celebahq-256"
|
10 |
+
|
11 |
+
# load model and scheduler
|
12 |
+
ddpm = DDPMPipeline.from_pretrained(model_id)
|
13 |
+
ddim = DDIMPipeline.from_pretrained(model_id)
|
14 |
+
pndm = PNDMPipeline.from_pretrained(model_id)
|
15 |
+
|
16 |
+
# run pipeline in inference (sample random noise and denoise)
|
17 |
+
def predict(steps=100,seed=42,scheduler="ddim"):
|
18 |
+
generator = torch.manual_seed(seed)
|
19 |
+
if(scheduler == "ddim"):
|
20 |
+
image = ddim(generator=generator, num_inference_steps=steps) #does not work (returns random noise)
|
21 |
+
image = image["sample"]
|
22 |
+
elif(scheduler == "ddpm"):
|
23 |
+
image = ddpm(generator=generator) #works, but does not let me set the number of steps
|
24 |
+
elif(scheduler == "pndm"):
|
25 |
+
image = pndm(generator=generator, num_inference_steps=steps) #does not work, still detects its DDPM behind the scenes and does not run pndm steps
|
26 |
+
image = image["sample"]
|
27 |
+
|
28 |
+
# process image to PIL
|
29 |
+
image_processed = image.cpu().permute(0, 2, 3, 1)
|
30 |
+
image_processed = (image_processed + 1.0) * 127.5
|
31 |
+
image_processed = image_processed.clamp(0, 255).numpy().astype(np.uint8)
|
32 |
+
return PIL.Image.fromarray(image_processed[0])
|
33 |
+
|
34 |
+
|
35 |
+
random_seed = random.randint(0, 2147483647)
|
36 |
+
gr.Interface(
|
37 |
+
predict,
|
38 |
+
inputs=[
|
39 |
+
gr.inputs.Slider(1, 1000, label='Inference Steps', default=1000, step=1),
|
40 |
+
gr.inputs.Slider(0, 2147483647, label='Seed', default=random_seed),
|
41 |
+
gr.inputs.Radio(["ddim", "ddpm", "pndm"], default="ddpm",label="Diffusion scheduler")
|
42 |
+
],
|
43 |
+
outputs="image",
|
44 |
+
).launch()
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/diffusers.git
|