LububSpinball dvruette commited on
Commit
3bc0b1b
β€’
0 Parent(s):

Duplicate from dvruette/fabric

Browse files

Co-authored-by: Dimitri <[email protected]>

Files changed (4) hide show
  1. .gitattributes +35 -0
  2. README.md +16 -0
  3. app.py +262 -0
  4. requirements.txt +13 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: 'FABRIC: Personalizing Diffusion Models with Iterative Feedback'
3
+ emoji: 🎨
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 3.36.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: dvruette/fabric
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
15
+
16
+ Demo for Arxiv paper at https://arxiv.org/abs/2307.10159
app.py ADDED
@@ -0,0 +1,262 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import functools
2
+
3
+ import gradio as gr
4
+ import torch
5
+
6
+ from fabric.generator import AttentionBasedGenerator
7
+
8
+
9
+ #model_name = "dreamlike-art/dreamlike-photoreal-2.0"
10
+ model_name = ""
11
+ model_ckpt = "https://huggingface.co/Lykon/DreamShaper/blob/main/DreamShaper_7_pruned.safetensors"
12
+
13
+ class GeneratorWrapper:
14
+ def __init__(self, model_name=None, model_ckpt=None):
15
+ self.model_name = model_name if model_name else None
16
+ self.model_ckpt = model_ckpt if model_ckpt else None
17
+ self.dtype = torch.float16 if torch.cuda.is_available() else torch.float32
18
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
19
+
20
+ self.reload()
21
+
22
+ def generate(self, *args, **kwargs):
23
+ if not hasattr(self, "generator"):
24
+ self.reload()
25
+ return self.generator.generate(*args, **kwargs)
26
+
27
+ def to(self, device):
28
+ return self.generator.to(device)
29
+
30
+ def reload(self):
31
+ if hasattr(self, "generator"):
32
+ del self.generator
33
+ if self.device == "cuda":
34
+ torch.cuda.empty_cache()
35
+ self.generator = AttentionBasedGenerator(
36
+ model_name=self.model_name,
37
+ model_ckpt=self.model_ckpt,
38
+ torch_dtype=self.dtype,
39
+ ).to(self.device)
40
+
41
+ generator = GeneratorWrapper(model_name, model_ckpt)
42
+
43
+
44
+ css = """
45
+ .btn-green {
46
+ background-image: linear-gradient(to bottom right, #86efac, #22c55e) !important;
47
+ border-color: #22c55e !important;
48
+ color: #166534 !important;
49
+ }
50
+ .btn-green:hover {
51
+ background-image: linear-gradient(to bottom right, #86efac, #86efac) !important;
52
+ }
53
+ .btn-red {
54
+ background: linear-gradient(to bottom right, #fda4af, #fb7185) !important;
55
+ border-color: #fb7185 !important;
56
+ color: #9f1239 !important;
57
+ }
58
+ .btn-red:hover {background: linear-gradient(to bottom right, #fda4af, #fda4af) !important;}
59
+
60
+ /*****/
61
+
62
+ .dark .btn-green {
63
+ background-image: linear-gradient(to bottom right, #047857, #065f46) !important;
64
+ border-color: #047857 !important;
65
+ color: #ffffff !important;
66
+ }
67
+ .dark .btn-green:hover {
68
+ background-image: linear-gradient(to bottom right, #047857, #047857) !important;
69
+ }
70
+ .dark .btn-red {
71
+ background: linear-gradient(to bottom right, #be123c, #9f1239) !important;
72
+ border-color: #be123c !important;
73
+ color: #ffffff !important;
74
+ }
75
+ .dark .btn-red:hover {background: linear-gradient(to bottom right, #be123c, #be123c) !important;}
76
+ """
77
+
78
+ def generate_fn(
79
+ feedback_enabled,
80
+ max_feedback_imgs,
81
+ prompt,
82
+ neg_prompt,
83
+ liked,
84
+ disliked,
85
+ denoising_steps,
86
+ guidance_scale,
87
+ feedback_start,
88
+ feedback_end,
89
+ min_weight,
90
+ max_weight,
91
+ neg_scale,
92
+ batch_size,
93
+ seed,
94
+ progress=gr.Progress(track_tqdm=True),
95
+ ):
96
+ try:
97
+ if seed < 0:
98
+ seed = None
99
+
100
+ max_feedback_imgs = max(0, int(max_feedback_imgs))
101
+ total_images = (len(liked) if liked else 0) + (len(disliked) if disliked else 0)
102
+
103
+ if not feedback_enabled:
104
+ liked = []
105
+ disliked = []
106
+ elif total_images > max_feedback_imgs:
107
+ if liked and disliked:
108
+ max_disliked = min(len(disliked), max_feedback_imgs // 2)
109
+ max_liked = min(len(liked), max_feedback_imgs - max_disliked)
110
+ if max_liked > len(liked):
111
+ max_disliked = max_feedback_imgs - max_liked
112
+ liked = liked[-max_liked:]
113
+ disliked = disliked[-max_disliked:]
114
+ elif liked:
115
+ liked = liked[-max_feedback_imgs:]
116
+ disliked = []
117
+ else:
118
+ liked = []
119
+ disliked = disliked[-max_feedback_imgs:]
120
+ # else: keep all feedback images
121
+
122
+ generate_kwargs = {
123
+ "prompt": prompt,
124
+ "negative_prompt": neg_prompt,
125
+ "liked": liked,
126
+ "disliked": disliked,
127
+ "denoising_steps": denoising_steps,
128
+ "guidance_scale": guidance_scale,
129
+ "feedback_start": feedback_start,
130
+ "feedback_end": feedback_end,
131
+ "min_weight": min_weight,
132
+ "max_weight": max_weight,
133
+ "neg_scale": neg_scale,
134
+ "seed": seed,
135
+ "n_images": batch_size,
136
+ }
137
+
138
+ try:
139
+ images = generator.generate(**generate_kwargs)
140
+ except RuntimeError as err:
141
+ if 'out of memory' in str(err):
142
+ generator.reload()
143
+ raise
144
+ return [(img, f"Image {i+1}") for i, img in enumerate(images)], images
145
+ except Exception as err:
146
+ raise gr.Error(str(err))
147
+
148
+
149
+ def add_img_from_list(i, curr_imgs, all_imgs):
150
+ if all_imgs is None:
151
+ all_imgs = []
152
+ if i >= 0 and i < len(curr_imgs):
153
+ all_imgs.append(curr_imgs[i])
154
+ return all_imgs, all_imgs # return (gallery, state)
155
+
156
+ def add_img(img, all_imgs):
157
+ if all_imgs is None:
158
+ all_imgs = []
159
+ all_imgs.append(img)
160
+ return None, all_imgs, all_imgs
161
+
162
+ def remove_img_from_list(event: gr.SelectData, imgs):
163
+ if event.index >= 0 and event.index < len(imgs):
164
+ imgs.pop(event.index)
165
+ return imgs, imgs
166
+
167
+
168
+ with gr.Blocks(css=css) as demo:
169
+
170
+ liked_imgs = gr.State([])
171
+ disliked_imgs = gr.State([])
172
+ curr_imgs = gr.State([])
173
+
174
+ with gr.Row():
175
+ with gr.Column(scale=100):
176
+ prompt = gr.Textbox(label="Prompt")
177
+ neg_prompt = gr.Textbox(label="Negative prompt", value="lowres, bad anatomy, bad hands, cropped, worst quality")
178
+ submit_btn = gr.Button("Generate", variant="primary", min_width="96px")
179
+
180
+ with gr.Row(equal_height=False):
181
+ with gr.Column():
182
+ denoising_steps = gr.Slider(1, 100, value=20, step=1, label="Sampling steps")
183
+ guidance_scale = gr.Slider(0.0, 30.0, value=6, step=0.25, label="CFG scale")
184
+ batch_size = gr.Slider(1, 10, value=4, step=1, label="Batch size", interactive=False)
185
+ seed = gr.Number(-1, minimum=-1, precision=0, label="Seed")
186
+ max_feedback_imgs = gr.Slider(0, 20, value=6, step=1, label="Max. feedback images", info="Maximum number of liked/disliked images to be used. If exceeded, only the most recent images will be used as feedback. (NOTE: large number of feedback imgs => high VRAM requirements)")
187
+ feedback_enabled = gr.Checkbox(True, label="Enable feedback", interactive=True)
188
+
189
+ with gr.Accordion("Liked Images", open=True):
190
+ liked_img_input = gr.Image(type="pil", shape=(512, 512), height=128, label="Upload liked image")
191
+ like_gallery = gr.Gallery(label="πŸ‘ Liked images (click to remove)", columns=[3, 4, 3, 4, 5, 6], height=256, allow_preview=False)
192
+ clear_liked_btn = gr.Button("Clear likes")
193
+
194
+ with gr.Accordion("Disliked Images", open=True):
195
+ disliked_img_input = gr.Image(type="pil", shape=(512, 512), height=128, label="Upload disliked image")
196
+ dislike_gallery = gr.Gallery(label="πŸ‘Ž Disliked images (click to remove)", columns=[3, 4, 3, 4, 5, 6], height=256, allow_preview=False)
197
+ clear_disliked_btn = gr.Button("Clear dislikes")
198
+
199
+ with gr.Accordion("Feedback parameters", open=False):
200
+ feedback_start = gr.Slider(0.0, 1.0, value=0.0, label="Feedback start", info="Fraction of denoising steps starting from which to use max. feedback weight.")
201
+ feedback_end = gr.Slider(0.0, 1.0, value=0.8, label="Feedback end", info="Up to what fraction of denoising steps to use max. feedback weight.")
202
+ feedback_min_weight = gr.Slider(0.0, 1.0, value=0.0, label="Feedback min. weight", info="Attention weight of feedback images when turned off (set to 0.0 to disable)")
203
+ feedback_max_weight = gr.Slider(0.0, 1.0, value=0.8, label="Feedback max. weight", info="Attention weight of feedback images when turned on (set to 0.0 to disable)")
204
+ feedback_neg_scale = gr.Slider(0.0, 1.0, value=0.5, label="Neg. feedback scale", info="Attention weight of disliked images relative to liked images (set to 0.0 to disable negative feedback)")
205
+
206
+ with gr.Column():
207
+ gallery = gr.Gallery(label="Generated images")
208
+
209
+ like_btns = []
210
+ dislike_btns = []
211
+ with gr.Row():
212
+ for i in range(0, 2):
213
+ like_btn = gr.Button(f"πŸ‘ Image {i+1}", elem_classes="btn-green")
214
+ like_btns.append(like_btn)
215
+ with gr.Row():
216
+ for i in range(2, 4):
217
+ like_btn = gr.Button(f"πŸ‘ Image {i+1}", elem_classes="btn-green")
218
+ like_btns.append(like_btn)
219
+ with gr.Row():
220
+ for i in range(0, 2):
221
+ dislike_btn = gr.Button(f"πŸ‘Ž Image {i+1}", elem_classes="btn-red")
222
+ dislike_btns.append(dislike_btn)
223
+ with gr.Row():
224
+ for i in range(2, 4):
225
+ dislike_btn = gr.Button(f"πŸ‘Ž Image {i+1}", elem_classes="btn-red")
226
+ dislike_btns.append(dislike_btn)
227
+
228
+ generate_params = [
229
+ feedback_enabled,
230
+ max_feedback_imgs,
231
+ prompt,
232
+ neg_prompt,
233
+ liked_imgs,
234
+ disliked_imgs,
235
+ denoising_steps,
236
+ guidance_scale,
237
+ feedback_start,
238
+ feedback_end,
239
+ feedback_min_weight,
240
+ feedback_max_weight,
241
+ feedback_neg_scale,
242
+ batch_size,
243
+ seed,
244
+ ]
245
+ submit_btn.click(generate_fn, generate_params, [gallery, curr_imgs], queue=True)
246
+
247
+ for i, like_btn in enumerate(like_btns):
248
+ like_btn.click(functools.partial(add_img_from_list, i), [curr_imgs, liked_imgs], [like_gallery, liked_imgs], queue=False)
249
+ for i, dislike_btn in enumerate(dislike_btns):
250
+ dislike_btn.click(functools.partial(add_img_from_list, i), [curr_imgs, disliked_imgs], [dislike_gallery, disliked_imgs], queue=False)
251
+
252
+ like_gallery.select(remove_img_from_list, [liked_imgs], [like_gallery, liked_imgs], queue=False)
253
+ dislike_gallery.select(remove_img_from_list, [disliked_imgs], [dislike_gallery, disliked_imgs], queue=False)
254
+
255
+ liked_img_input.upload(add_img, [liked_img_input, liked_imgs], [liked_img_input, like_gallery, liked_imgs], queue=False)
256
+ disliked_img_input.upload(add_img, [disliked_img_input, disliked_imgs], [disliked_img_input, dislike_gallery, disliked_imgs], queue=False)
257
+
258
+ clear_liked_btn.click(lambda: [[], []], None, [liked_imgs, like_gallery], queue=False)
259
+ clear_disliked_btn.click(lambda: [[], []], None, [disliked_imgs, dislike_gallery], queue=False)
260
+
261
+ demo.queue(1)
262
+ demo.launch(debug=True)
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.18.0
2
+ diffusers==0.17.1
3
+ torch==2.0.1
4
+ transformers>=4.30.2
5
+ hydra-core
6
+ matplotlib
7
+ pandas
8
+ tqdm
9
+ Pillow
10
+ ftfy
11
+ regex
12
+ clip @ git+https://github.com/openai/CLIP.git
13
+ fabric @ git+https://github.com/sd-fabric/fabric.git