kd5678 commited on
Commit
5d82ba5
1 Parent(s): c6ad3ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -4
app.py CHANGED
@@ -1,7 +1,91 @@
 
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
+ import torch
4
+ import yaml
5
+ import numpy as np
6
 
7
+ from munch import munchify
8
+ import torchvision.transforms as transforms
9
+ from torchvision.transforms import functional as F
10
+ from diffusers import (
11
+ AutoPipelineForInpainting,
12
+ )
13
+ from generate_dataset import outpainting_generator_rectangle
14
 
15
+
16
+ transform = transforms.Compose([
17
+ transforms.ToPILImage(),
18
+ transforms.Resize((512, 512), interpolation=F.InterpolationMode.LANCZOS),
19
+ ])
20
+
21
+ def pref_inpainting(image,
22
+ box_width_ratio,
23
+ mask_random_start,
24
+ steps,
25
+ ):
26
+ with open("/data0/kendong/Diffusions/zero123-live/configs/imagereward_train_configs.yaml") as file:
27
+ config_dict= yaml.safe_load(file)
28
+ config = munchify(config_dict)
29
+
30
+ pipe = AutoPipelineForInpainting.from_pretrained(
31
+ '/data1/kendong/joint-rl-diffusion/alignment_log/exp_reward_group_regression_all_1w_1.6boundary/iteration_2560', num_inference_steps=steps)
32
+
33
+ pipe = pipe.to("cuda")
34
+
35
+
36
+ color, mask = outpainting_generator_rectangle(image, box_width_ratio/100, mask_random_start)
37
+ mask = mask.convert('L')
38
+
39
+ color, mask = np.array(color).transpose(2, 0, 1), np.array(mask)
40
+ mask = mask[None, ...]
41
+ mask_ = np.zeros_like(mask)
42
+ mask_[mask < 125] = 0
43
+ mask_[mask >= 125] = 1
44
+
45
+ color = torch.from_numpy(color).to("cuda")
46
+ mask = torch.from_numpy(mask).to("cuda")
47
+
48
+
49
+ color, mask = transform(color), transform(mask)
50
+ res = pipe(prompt='', image=color, mask_image=mask, eta=config.eta).images[0]
51
+
52
+
53
+ res.save(os.path.join('./', 'test.png'))
54
+
55
+ return color, res
56
+
57
+
58
+ inputs = [
59
+ gr.Image(type="pil", image_mode="RGBA", label='Input Image'), # shape=[512, 512]
60
+ gr.Slider(30, 45, value=35, step=1, label="box_width_ratio"),
61
+ gr.Slider(0, 256, value=125, step=1, label="mask_random_start"),
62
+ gr.Slider(30, 100, value=50, step=5, label="steps"),
63
+ ]
64
+
65
+ outputs = [
66
+ gr.Image(type="pil", image_mode="RGBA", label='Mask RGB Image', container=True, width="65%"),
67
+ gr.Image(type="pil", image_mode="RGBA", label='Results', container=True, width="65%"),
68
+ ]
69
+
70
+ examples = [
71
+ ["/data0/kendong/Diffusions/zero123-live/test_demo/assets/ILSVRC2012_test_00000003.JPEG", 35, 125, 50],
72
+ ["/data0/kendong/Diffusions/zero123-live/test_demo/assets/ILSVRC2012_test_00000181.JPEG", 35, 125, 50],
73
+ ["/data0/kendong/Diffusions/zero123-live/test_demo/assets/ILSVRC2012_test_00002334.JPEG", 35, 125, 50],
74
+ ["/data0/kendong/Diffusions/zero123-live/test_demo/assets/ILSVRC2012_test_00002613.JPEG", 35, 125, 50],
75
+ ]
76
+
77
+
78
+
79
+ iface = gr.Interface(
80
+ fn=pref_inpainting,
81
+ inputs=inputs,
82
+ outputs=outputs,
83
+ title="Inpainting with Human Preference (Utilizing Free CPU Resources)",
84
+ description="Upload an image and start your inpainting (currently only supporting outpainting masks; other mask types coming soon).",
85
+ theme="default",
86
+ examples= examples,
87
+ allow_flagging="never"
88
+ )
89
+
90
+
91
+ iface.launch(share=True)