Dimitri commited on
Commit
61d0d14
β€’
1 Parent(s): 9d9dc64

initial commit

Browse files
Files changed (2) hide show
  1. app.py +229 -0
  2. requirements.txt +13 -0
app.py ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ dtype = torch.float16 if torch.cuda.is_available() else torch.float32
14
+ device = "cuda" if torch.cuda.is_available() else "cpu"
15
+ generator = AttentionBasedGenerator(
16
+ model_name=model_name if model_name else None,
17
+ model_ckpt=model_ckpt if model_ckpt else None,
18
+ torch_dtype=dtype,
19
+ ).to(device)
20
+
21
+
22
+ css = """
23
+ .btn-green {
24
+ background-image: linear-gradient(to bottom right, #86efac, #22c55e) !important;
25
+ border-color: #22c55e !important;
26
+ color: #166534 !important;
27
+ }
28
+ .btn-green:hover {
29
+ background-image: linear-gradient(to bottom right, #86efac, #86efac) !important;
30
+ }
31
+ .btn-red {
32
+ background: linear-gradient(to bottom right, #fda4af, #fb7185) !important;
33
+ border-color: #fb7185 !important;
34
+ color: #9f1239 !important;
35
+ }
36
+ .btn-red:hover {background: linear-gradient(to bottom right, #fda4af, #fda4af) !important;}
37
+
38
+ /*****/
39
+
40
+ .dark .btn-green {
41
+ background-image: linear-gradient(to bottom right, #047857, #065f46) !important;
42
+ border-color: #047857 !important;
43
+ color: #ffffff !important;
44
+ }
45
+ .dark .btn-green:hover {
46
+ background-image: linear-gradient(to bottom right, #047857, #047857) !important;
47
+ }
48
+ .dark .btn-red {
49
+ background: linear-gradient(to bottom right, #be123c, #9f1239) !important;
50
+ border-color: #be123c !important;
51
+ color: #ffffff !important;
52
+ }
53
+ .dark .btn-red:hover {background: linear-gradient(to bottom right, #be123c, #be123c) !important;}
54
+ """
55
+
56
+ def generate_fn(
57
+ feedback_enabled,
58
+ max_feedback_imgs,
59
+ prompt,
60
+ neg_prompt,
61
+ liked,
62
+ disliked,
63
+ denoising_steps,
64
+ guidance_scale,
65
+ feedback_start,
66
+ feedback_end,
67
+ min_weight,
68
+ max_weight,
69
+ neg_scale,
70
+ batch_size,
71
+ seed,
72
+ progress=gr.Progress(track_tqdm=True),
73
+ ):
74
+ try:
75
+ if seed < 0:
76
+ seed = None
77
+
78
+ max_feedback_imgs = max(0, int(max_feedback_imgs))
79
+ total_images = (len(liked) if liked else 0) + (len(disliked) if disliked else 0)
80
+
81
+ if not feedback_enabled:
82
+ liked = []
83
+ disliked = []
84
+ elif total_images > max_feedback_imgs:
85
+ if liked and disliked:
86
+ max_disliked = min(len(disliked), max_feedback_imgs // 2)
87
+ max_liked = min(len(liked), max_feedback_imgs - max_disliked)
88
+ if max_liked > len(liked):
89
+ max_disliked = max_feedback_imgs - max_liked
90
+ liked = liked[-max_liked:]
91
+ disliked = disliked[-max_disliked:]
92
+ elif liked:
93
+ liked = liked[-max_feedback_imgs:]
94
+ disliked = []
95
+ else:
96
+ liked = []
97
+ disliked = disliked[-max_feedback_imgs:]
98
+ # else: keep all feedback images
99
+
100
+ images = generator.generate(
101
+ prompt=prompt,
102
+ negative_prompt=neg_prompt,
103
+ liked=liked,
104
+ disliked=disliked,
105
+ denoising_steps=denoising_steps,
106
+ guidance_scale=guidance_scale,
107
+ feedback_start=feedback_start,
108
+ feedback_end=feedback_end,
109
+ min_weight=min_weight,
110
+ max_weight=max_weight,
111
+ neg_scale=neg_scale,
112
+ seed=seed,
113
+ n_images=batch_size,
114
+ )
115
+ return [(img, f"Image {i+1}") for i, img in enumerate(images)], images
116
+ except Exception as err:
117
+ raise gr.Error(str(err))
118
+
119
+
120
+ def add_img_from_list(i, curr_imgs, all_imgs):
121
+ if i >= 0 and i < len(curr_imgs):
122
+ all_imgs.append(curr_imgs[i])
123
+ return all_imgs, all_imgs # return (gallery, state)
124
+
125
+ def add_img(img, all_imgs):
126
+ all_imgs.append(img)
127
+ return None, all_imgs, all_imgs
128
+
129
+ def remove_img_from_list(event: gr.SelectData, imgs):
130
+ if event.index >= 0 and event.index < len(imgs):
131
+ imgs.pop(event.index)
132
+ return imgs, imgs
133
+
134
+
135
+ with gr.Blocks(css=css) as demo:
136
+
137
+ liked_imgs = gr.State([])
138
+ disliked_imgs = gr.State([])
139
+ curr_imgs = gr.State([])
140
+
141
+ with gr.Row():
142
+ with gr.Column(scale=100):
143
+ prompt = gr.Textbox(label="Prompt")
144
+ neg_prompt = gr.Textbox(label="Negative prompt", value="lowres, bad anatomy, bad hands, cropped, worst quality")
145
+ submit_btn = gr.Button("Generate", variant="primary", min_width="96px")
146
+
147
+ with gr.Row(equal_height=False):
148
+ with gr.Column():
149
+ denoising_steps = gr.Slider(1, 100, value=20, step=1, label="Sampling steps")
150
+ guidance_scale = gr.Slider(0.0, 30.0, value=6, step=0.25, label="CFG scale")
151
+ batch_size = gr.Slider(1, 10, value=4, step=1, label="Batch size")
152
+ seed = gr.Number(-1, minimum=-1, precision=0, label="Seed")
153
+ 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)")
154
+ feedback_enabled = gr.Checkbox(True, label="Enable feedback", interactive=True)
155
+
156
+ with gr.Accordion("Liked Images", open=True):
157
+ liked_img_input = gr.Image(type="pil", shape=(512, 512), height=128, label="Upload liked image")
158
+ like_gallery = gr.Gallery(label="πŸ‘ Liked images (click to remove)", columns=[3, 4, 3, 4, 5, 6], height=256, allow_preview=False)
159
+ clear_liked_btn = gr.Button("Clear likes")
160
+
161
+ with gr.Accordion("Disliked Images", open=True):
162
+ disliked_img_input = gr.Image(type="pil", shape=(512, 512), height=128, label="Upload disliked image")
163
+ dislike_gallery = gr.Gallery(label="πŸ‘Ž Disliked images (click to remove)", columns=[3, 4, 3, 4, 5, 6], height=256, allow_preview=False)
164
+ clear_disliked_btn = gr.Button("Clear dislikes")
165
+
166
+ with gr.Accordion("Feedback parameters", open=False):
167
+ 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.")
168
+ 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.")
169
+ 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)")
170
+ 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)")
171
+ 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)")
172
+
173
+ with gr.Column():
174
+ gallery = gr.Gallery(label="Generated images")
175
+
176
+ like_btns = []
177
+ dislike_btns = []
178
+ with gr.Row():
179
+ for i in range(0, 2):
180
+ like_btn = gr.Button(f"πŸ‘ Image {i+1}", elem_classes="btn-green")
181
+ like_btns.append(like_btn)
182
+ with gr.Row():
183
+ for i in range(2, 4):
184
+ like_btn = gr.Button(f"πŸ‘ Image {i+1}", elem_classes="btn-green")
185
+ like_btns.append(like_btn)
186
+ with gr.Row():
187
+ for i in range(0, 2):
188
+ dislike_btn = gr.Button(f"πŸ‘Ž Image {i+1}", elem_classes="btn-red")
189
+ dislike_btns.append(dislike_btn)
190
+ with gr.Row():
191
+ for i in range(2, 4):
192
+ dislike_btn = gr.Button(f"πŸ‘Ž Image {i+1}", elem_classes="btn-red")
193
+ dislike_btns.append(dislike_btn)
194
+
195
+ generate_params = [
196
+ feedback_enabled,
197
+ max_feedback_imgs,
198
+ prompt,
199
+ neg_prompt,
200
+ liked_imgs,
201
+ disliked_imgs,
202
+ denoising_steps,
203
+ guidance_scale,
204
+ feedback_start,
205
+ feedback_end,
206
+ feedback_min_weight,
207
+ feedback_max_weight,
208
+ feedback_neg_scale,
209
+ batch_size,
210
+ seed,
211
+ ]
212
+ submit_btn.click(generate_fn, generate_params, [gallery, curr_imgs], queue=True)
213
+
214
+ for i, like_btn in enumerate(like_btns):
215
+ like_btn.click(functools.partial(add_img_from_list, i), [curr_imgs, liked_imgs], [like_gallery, liked_imgs], queue=False)
216
+ for i, dislike_btn in enumerate(dislike_btns):
217
+ dislike_btn.click(functools.partial(add_img_from_list, i), [curr_imgs, disliked_imgs], [dislike_gallery, disliked_imgs], queue=False)
218
+
219
+ like_gallery.select(remove_img_from_list, [liked_imgs], [like_gallery, liked_imgs], queue=False)
220
+ dislike_gallery.select(remove_img_from_list, [disliked_imgs], [dislike_gallery, disliked_imgs], queue=False)
221
+
222
+ liked_img_input.upload(add_img, [liked_img_input, liked_imgs], [liked_img_input, like_gallery, liked_imgs], queue=False)
223
+ disliked_img_input.upload(add_img, [disliked_img_input, disliked_imgs], [disliked_img_input, dislike_gallery, disliked_imgs], queue=False)
224
+
225
+ clear_liked_btn.click(lambda: [None, None], None, [liked_imgs, like_gallery], queue=False)
226
+ clear_disliked_btn.click(lambda: [None, None], None, [disliked_imgs, dislike_gallery], queue=False)
227
+
228
+ demo.queue(8)
229
+ demo.launch()
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