muneebable commited on
Commit
601f59a
·
verified ·
1 Parent(s): 9c90500

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = 'muneebable/ddpm-celebahq-finetuned-anime-art'
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=20)
18
+
19
+ # The guidance function
20
+ def color_loss(images, target_color=(0.1, 0.9, 0.5)):
21
+ """Given a target color (R, G, B) return a loss for how far away on average
22
+ the images' pixels are from that color. Defaults to a light teal: (0.1, 0.9, 0.5) """
23
+ target = torch.tensor(target_color).to(images.device) * 2 - 1 # Map target color to (-1, 1)
24
+ target = target[None, :, None, None] # Get shape right to work with the images (b, c, h, w)
25
+ error = torch.abs(images - target).mean() # Mean absolute difference between the image pixels and the target color
26
+ return error
27
+
28
+ # And the core function to generate an image given the relevant inputs
29
+ def generate(color, guidance_loss_scale):
30
+ target_color = ImageColor.getcolor(color, "RGB") # Target color as RGB
31
+ target_color = [a/255 for a in target_color] # Rescale from (0, 255) to (0, 1)
32
+ x = torch.randn(1, 3, 256, 256).to(device)
33
+ for i, t in enumerate(scheduler.timesteps):
34
+ model_input = scheduler.scale_model_input(x, t)
35
+ with torch.no_grad():
36
+ noise_pred = image_pipe.unet(model_input, t)["sample"]
37
+ x = x.detach().requires_grad_()
38
+ x0 = scheduler.step(noise_pred, t, x).pred_original_sample
39
+ loss = color_loss(x0, target_color) * guidance_loss_scale
40
+ cond_grad = -torch.autograd.grad(loss, x)[0]
41
+ x = x.detach() + cond_grad
42
+ x = scheduler.step(noise_pred, t, x).prev_sample
43
+ grid = torchvision.utils.make_grid(x, nrow=4)
44
+ im = grid.permute(1, 2, 0).cpu().clip(-1, 1)*0.5 + 0.5
45
+ im = Image.fromarray(np.array(im*255).astype(np.uint8))
46
+ im.save('test.jpeg')
47
+ return im
48
+
49
+ # See the gradio docs for the types of inputs and outputs available
50
+ inputs = [
51
+ gr.ColorPicker(label="color", value='55FFAA'), # Add any inputs you need here
52
+ gr.Slider(label="guidance_scale", minimum=0, maximum=30, value=3)
53
+ ]
54
+ outputs = gr.Image(label="result")
55
+
56
+ # Setting up a minimal interface to our function:
57
+ demo = gr.Interface(
58
+ fn=generate,
59
+ inputs=inputs,
60
+ outputs=outputs,
61
+ examples=[
62
+ ["#BB2266", 3],["#44CCAA", 5] # You can provide some example inputs to get people started
63
+ ],
64
+ )
65
+
66
+ # And launching
67
+ if __name__ == "__main__":
68
+ demo.launch(enable_queue=True)
69
+