Spaces:
Running
on
Zero
Running
on
Zero
Upload 5 files
Browse files- .gitattributes +3 -0
- Lonely astronaut, floating in .png +3 -0
- Train running through the coun (6).png +3 -0
- app.py +489 -0
- requirements.txt +9 -0
- test.png +3 -0
.gitattributes
CHANGED
@@ -98,3 +98,6 @@ examples/(17).png filter=lfs diff=lfs merge=lfs -text
|
|
98 |
examples/(18).png filter=lfs diff=lfs merge=lfs -text
|
99 |
examples/(2).png filter=lfs diff=lfs merge=lfs -text
|
100 |
examples/(9).png filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
98 |
examples/(18).png filter=lfs diff=lfs merge=lfs -text
|
99 |
examples/(2).png filter=lfs diff=lfs merge=lfs -text
|
100 |
examples/(9).png filter=lfs diff=lfs merge=lfs -text
|
101 |
+
Lonely[[:space:]]astronaut,[[:space:]]floating[[:space:]]in[[:space:]].png filter=lfs diff=lfs merge=lfs -text
|
102 |
+
test.png filter=lfs diff=lfs merge=lfs -text
|
103 |
+
Train[[:space:]]running[[:space:]]through[[:space:]]the[[:space:]]coun[[:space:]](6).png filter=lfs diff=lfs merge=lfs -text
|
Lonely astronaut, floating in .png
ADDED
Git LFS Details
|
Train running through the coun (6).png
ADDED
Git LFS Details
|
app.py
ADDED
@@ -0,0 +1,489 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
from diffusers import DiffusionPipeline
|
5 |
+
import torch
|
6 |
+
from diffusers import StableDiffusionXLPipeline
|
7 |
+
import requests
|
8 |
+
import torch
|
9 |
+
from PIL import Image
|
10 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq, BitsAndBytesConfig
|
11 |
+
from transformers.image_utils import load_image
|
12 |
+
from peft import PeftModel
|
13 |
+
import re
|
14 |
+
from diffusers import StableDiffusionXLPipeline, DiffusionPipeline
|
15 |
+
|
16 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
+
print(device)
|
18 |
+
|
19 |
+
bnb_config = BitsAndBytesConfig(
|
20 |
+
load_in_4bit=True,
|
21 |
+
bnb_4bit_use_double_quant=True,
|
22 |
+
bnb_4bit_quant_type="nf4",
|
23 |
+
bnb_4bit_compute_dtype=torch.float16,
|
24 |
+
llm_int8_skip_modules=["lm_head", "embed_tokens"],
|
25 |
+
)
|
26 |
+
|
27 |
+
processor = AutoProcessor.from_pretrained("HuggingFaceM4/idefics2-8b", size= {"longest_edge": 448, "shortest_edge": 378}, do_image_splitting=False)
|
28 |
+
|
29 |
+
if torch.cuda.is_available():
|
30 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
31 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
32 |
+
torch_dtype=torch.float16
|
33 |
+
).to("cuda")
|
34 |
+
else:
|
35 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", use_safetensors=True)
|
36 |
+
pipe = pipe.to(device)
|
37 |
+
|
38 |
+
|
39 |
+
MAX_SEED = np.iinfo(np.int32).max
|
40 |
+
MAX_IMAGE_SIZE = 1024
|
41 |
+
|
42 |
+
valid_api = ""
|
43 |
+
|
44 |
+
css="""
|
45 |
+
#col-container {
|
46 |
+
margin: 0 auto;
|
47 |
+
max-width: 520px;
|
48 |
+
}
|
49 |
+
|
50 |
+
#gen-container {
|
51 |
+
margin: 0 auto;
|
52 |
+
max-width: 640px;
|
53 |
+
}
|
54 |
+
|
55 |
+
#title-container {
|
56 |
+
margin: 0 auto;
|
57 |
+
max-width: 1340px;
|
58 |
+
}
|
59 |
+
|
60 |
+
#main-container {
|
61 |
+
margin: 0 auto;
|
62 |
+
max-width: 1340px;
|
63 |
+
}
|
64 |
+
"""
|
65 |
+
|
66 |
+
|
67 |
+
if torch.cuda.is_available():
|
68 |
+
power_device = "GPU"
|
69 |
+
else:
|
70 |
+
power_device = "CPU"
|
71 |
+
|
72 |
+
from PIL import Image
|
73 |
+
|
74 |
+
comment_images = [
|
75 |
+
"test.png",
|
76 |
+
"comment_images/0.png",
|
77 |
+
"comment_images/1.png",
|
78 |
+
"comment_images/2.png",
|
79 |
+
"comment_images/3.png",
|
80 |
+
"comment_images/4.png",
|
81 |
+
"comment_images/5.png",
|
82 |
+
"comment_images/6.png",
|
83 |
+
"comment_images/7.png",
|
84 |
+
"comment_images/8.png",
|
85 |
+
"comment_images/9.png",
|
86 |
+
"comment_images/10.png",
|
87 |
+
"comment_images/11.png",
|
88 |
+
"comment_images/12.png",
|
89 |
+
"comment_images/13.png",
|
90 |
+
"comment_images/14.png",
|
91 |
+
"comment_images/15.png",
|
92 |
+
"comment_images/16.png",
|
93 |
+
"comment_images/17.png",
|
94 |
+
"comment_images/18.png",
|
95 |
+
"comment_images/19.png",
|
96 |
+
"comment_images/20.png",
|
97 |
+
"comment_images/21.png",
|
98 |
+
"comment_images/22.png",
|
99 |
+
"comment_images/23.png",
|
100 |
+
"comment_images/24.png",
|
101 |
+
"comment_images/25.png",
|
102 |
+
"comment_images/26.png",
|
103 |
+
"comment_images/27.png",
|
104 |
+
"comment_images/28.png",
|
105 |
+
"comment_images/29.png",
|
106 |
+
"comment_images/30.png",
|
107 |
+
"comment_images/31.png",
|
108 |
+
"comment_images/32.png",
|
109 |
+
"comment_images/33.png",
|
110 |
+
"comment_images/34.png",
|
111 |
+
"comment_images/35.png",
|
112 |
+
"comment_images/36.png",
|
113 |
+
"comment_images/37.png",
|
114 |
+
"comment_images/38.png",
|
115 |
+
"comment_images/39.png",
|
116 |
+
"comment_images/40.png",
|
117 |
+
"comment_images/41.png",
|
118 |
+
"comment_images/42.jpg",
|
119 |
+
"comment_images/43.png",
|
120 |
+
"comment_images/44.png",
|
121 |
+
"comment_images/45.png",
|
122 |
+
"comment_images/46.png",
|
123 |
+
"comment_images/47.png",
|
124 |
+
"comment_images/48.png",
|
125 |
+
"comment_images/49.png",
|
126 |
+
"comment_images/50.png",
|
127 |
+
"comment_images/51.png",
|
128 |
+
"comment_images/52.png",
|
129 |
+
"comment_images/53.png",
|
130 |
+
"comment_images/54.png",
|
131 |
+
"comment_images/55.png"
|
132 |
+
]
|
133 |
+
|
134 |
+
comments = {'test.png': "Not sure about the concept, it's too straightforward. Though the boy looks kinda creepy which makes it exciting. the art style is pretty to look at. I like that the colors are muted, but wish they were a bit darker to make it more eerie and add depth.", 'comment_images/0.png': "Hate this with a passion. The colors are too vibrant and don't match at all. I hate these colors in general. The patterns are too abstract and contemporary. a 5-year-old could draw this. pass.", 'comment_images/1.png': "Woah I love the art style. The texture feels like old paper which is oh so beautiful. There are so many details to focus on. I love the expressive lines and how busy the composition is. Even though orange isn't my favorite, the greenish blue color of the water is so gorgeous.", 'comment_images/2.png': "I don't like how monochromatic and muted this one is. but the paperish texture is nice and the details are so intricate.", 'comment_images/3.png': "Oh super pretty! Looks so smooth and wet. Love the details and loose lines too. Feels mystical and magical and eerie. Also dark purples and blues? deep indigo? My fav ever. I'm here for it.", 'comment_images/4.png': "Love the art style. The uncanny vibe and nightmarish horror is so cool. Like its horror but if you squint you can't tell? Love the strange. wish it had more colors though. not a fan of greyscale.", 'comment_images/5.png': 'omg I hate this haha. what the hell. everything about it disgusts me so boring and childish ew.', 'comment_images/6.png': 'yessss. give it to the texture give it to the brushstrokes give it to the style. perfect. just wish the colors were less beige and more bold. I want an active nightmare. but kisses to the surrealism.'}
|
135 |
+
comments = dict()
|
136 |
+
|
137 |
+
image_index = 0
|
138 |
+
|
139 |
+
def submit_comment(comment):
|
140 |
+
global comment_images, image_index
|
141 |
+
if comment != "":
|
142 |
+
comments[comment_images[0]] = comment
|
143 |
+
comment_images.append(comment_images[0])
|
144 |
+
comment_images = comment_images[1:]
|
145 |
+
|
146 |
+
image_index = (image_index + 1) % len(comment_images)
|
147 |
+
|
148 |
+
elif comment_images[0] in comments:
|
149 |
+
comments.pop(comment_images[0], None)
|
150 |
+
|
151 |
+
print(comments)
|
152 |
+
next_comment = ""
|
153 |
+
if comment_images[0] in comments:
|
154 |
+
next_comment = comments[comment_images[0]]
|
155 |
+
|
156 |
+
clear_botton = gr.Button("Clear comments", interactive=len(comments) != 0)
|
157 |
+
|
158 |
+
return (gr.Image(value=comment_images[0], label=f"image {image_index+1}/{len(comment_images)}", show_label=True),
|
159 |
+
gr.Text(label="Comment", show_label=False, lines=2, max_lines=3, placeholder="Enter your comment", value=next_comment, container=False),
|
160 |
+
gr.Button(f"Extract visual preference from {len(comments)} comments", interactive=len(comments) != 0),
|
161 |
+
clear_botton
|
162 |
+
)
|
163 |
+
|
164 |
+
def next_image():
|
165 |
+
global comment_images, image_index
|
166 |
+
comment_images.append(comment_images[0])
|
167 |
+
comment_images = comment_images[1:]
|
168 |
+
|
169 |
+
print(comments)
|
170 |
+
next_comment = ""
|
171 |
+
if comment_images[0] in comments:
|
172 |
+
next_comment = comments[comment_images[0]]
|
173 |
+
|
174 |
+
image_index = (image_index + 1) % len(comment_images)
|
175 |
+
|
176 |
+
return gr.Image(value=comment_images[0], label=f"image {image_index+1}/{len(comment_images)}", show_label=True), gr.Text(label="Comment", show_label=False, lines=2, max_lines=3, placeholder="Enter your comment", value=next_comment, container=False)
|
177 |
+
|
178 |
+
def previous_image():
|
179 |
+
global comment_images, image_index
|
180 |
+
comment_images = comment_images[::-1]
|
181 |
+
comment_images.append(comment_images[0])
|
182 |
+
comment_images = comment_images[1:]
|
183 |
+
comment_images = comment_images[::-1]
|
184 |
+
|
185 |
+
print(comments)
|
186 |
+
next_comment = ""
|
187 |
+
if comment_images[0] in comments:
|
188 |
+
next_comment = comments[comment_images[0]]
|
189 |
+
|
190 |
+
image_index = (image_index - 1) % len(comment_images)
|
191 |
+
|
192 |
+
return gr.Image(value=comment_images[0], label=f"image {image_index+1}/{len(comment_images)}", show_label=True), gr.Text(label="Comment", show_label=False, lines=2, max_lines=3, placeholder="Enter your comment", value=next_comment, container=False)
|
193 |
+
|
194 |
+
def clear_comments():
|
195 |
+
comments.clear()
|
196 |
+
extract_vp_botton = gr.Button(f"Extract visual preference from {len(comments)} comments", interactive=len(comments) != 0)
|
197 |
+
clear_botton = gr.Button("Clear comments", interactive=len(comments) != 0)
|
198 |
+
return extract_vp_botton, clear_botton
|
199 |
+
|
200 |
+
|
201 |
+
def extract_vp():
|
202 |
+
if valid_api == "":
|
203 |
+
vpe_model = AutoModelForVision2Seq.from_pretrained(
|
204 |
+
"HuggingFaceM4/idefics2-8b",
|
205 |
+
torch_dtype=torch.float16,
|
206 |
+
quantization_config=bnb_config,
|
207 |
+
)
|
208 |
+
|
209 |
+
vpe_model = PeftModel.from_pretrained(vpe_model, "VPE2").to("cuda")
|
210 |
+
|
211 |
+
global comments
|
212 |
+
|
213 |
+
prompt = """I will provide a set of artworks along with accompanying comments from a person. Analyze these artworks and the comments on them and identify artistic features such as present or mentioned colors, style, composition, mood, medium, texture, brushwork, lighting, shadow effects, perspective, and other noteworthy elements.
|
214 |
+
|
215 |
+
Your task is to extract the artistic features the person likes and dislikes based on both the artworks' features and the person's comments. Focus solely on artistic aspects and refrain from considering subject matter.
|
216 |
+
|
217 |
+
If the person expresses a preference for a specific aspect without clearly stating its category (e.g., appreciating the colors without specifying which colors), identify these specific features from the images directly to make the person's preference understandable without needing to see the artwork.
|
218 |
+
|
219 |
+
Your output should consist of two concise lists of keywords: one listing the specific art features the person likes and another listing the specific features they dislike (specified in keyword format without using sentences).
|
220 |
+
|
221 |
+
Here are the images and their corresponding comments:
|
222 |
+
"""
|
223 |
+
messages = [
|
224 |
+
{
|
225 |
+
"role": "user",
|
226 |
+
"content": [
|
227 |
+
{"type": "text",
|
228 |
+
"text": prompt},
|
229 |
+
]
|
230 |
+
}
|
231 |
+
]
|
232 |
+
images = []
|
233 |
+
comment_number = 1
|
234 |
+
for image in comments:
|
235 |
+
comment = comments[image]
|
236 |
+
image = Image.open(image)
|
237 |
+
images.append(image)
|
238 |
+
|
239 |
+
messages[0]["content"].append(
|
240 |
+
{"type": "image"}
|
241 |
+
)
|
242 |
+
|
243 |
+
messages[0]["content"].append(
|
244 |
+
{"type": "text",
|
245 |
+
"text": f"Comment {comment_number}: {comment}"}
|
246 |
+
)
|
247 |
+
comment_number = comment_number + 1
|
248 |
+
|
249 |
+
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
|
250 |
+
inputs = processor(text=prompt, images=images, return_tensors="pt")
|
251 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
252 |
+
|
253 |
+
generated_ids = vpe_model.generate(**inputs, max_new_tokens=2000, repetition_penalty=0.99, do_sample=False)
|
254 |
+
del vpe_model
|
255 |
+
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
256 |
+
positive_vp, negative_vp = re.search('.* \nAssistant: Liked Art Features: (.*)\nDisliked Art Features: (.*)', generated_texts).groups()
|
257 |
+
|
258 |
+
else:
|
259 |
+
PRINT(valid_api)
|
260 |
+
|
261 |
+
gr.Info("Visual preference successfully extracted.")
|
262 |
+
|
263 |
+
return gr.Textbox(label="Liked visual attributes", lines=3, value=positive_vp, interactive=True), gr.Textbox(label="Disliked visual attributes", lines=1, value=negative_vp, interactive=True), gr.Button("Run", scale=0, interactive=True)
|
264 |
+
|
265 |
+
def api_fn(api):
|
266 |
+
global valid_api
|
267 |
+
if api != "correct":
|
268 |
+
gr.Warning("Invalid API!")
|
269 |
+
valid_api = ""
|
270 |
+
else:
|
271 |
+
gr.Info("Valid API")
|
272 |
+
valid_api = api
|
273 |
+
|
274 |
+
def generate(prompt, vp_pos, vp_neg, slider):
|
275 |
+
print(f"prompt: {prompt}")
|
276 |
+
image = pipe(prompt=prompt,
|
277 |
+
num_inference_steps=40,
|
278 |
+
vp_pos=vp_pos,
|
279 |
+
vp_neg=vp_neg,
|
280 |
+
vp_degree_pos=slider,
|
281 |
+
vp_degree_neg=slider
|
282 |
+
).images[0]
|
283 |
+
return image
|
284 |
+
|
285 |
+
def change_vp(extract_vp):
|
286 |
+
return
|
287 |
+
|
288 |
+
def upload_file(files):
|
289 |
+
global comment_images, image_index
|
290 |
+
file_path = [file.name for file in files][0]
|
291 |
+
comment_images = [file_path] + comment_images
|
292 |
+
|
293 |
+
next_comment = ""
|
294 |
+
return gr.Image(value=comment_images[0], label=f"image {image_index+1}/{len(comment_images)}", show_label=True), gr.Text(label="Comment", show_label=False, lines=2, max_lines=3, placeholder="Enter your comment", value=next_comment, container=False)
|
295 |
+
|
296 |
+
with gr.Blocks(css=css, title="ViPer Demo", theme=gr.themes.Base()) as demo:
|
297 |
+
with gr.Row(elem_id="title-container"):
|
298 |
+
gr.Markdown(f"""
|
299 |
+
# **ViPer: Visual Personalization of Generative Models via Individual Preference Learning**
|
300 |
+
\n
|
301 |
+
\n
|
302 |
+
\n
|
303 |
+
|
304 |
+
""")
|
305 |
+
with gr.Row(elem_id="main-container"):
|
306 |
+
with gr.Column(elem_id="col-container"):
|
307 |
+
|
308 |
+
gr.Markdown(f"""
|
309 |
+
## Step 1: Extracting visual preference from comments on images
|
310 |
+
"""
|
311 |
+
)
|
312 |
+
|
313 |
+
gr.Markdown("Please write your comments on the images below, explaining why you like or dislike each one from an artistic perspective. Focus on images that evoke **strong reactions**, whether positive or negative, and skip those that don't affect you much.\nMore **detailed** comments will help us provide more personalized results. We recommend commenting on **at least 8** images.")
|
314 |
+
|
315 |
+
gr.Markdown("Note that our method works best with an OpenAI API. The free method might result in minor hallucinations in the extracted visual preferences.")
|
316 |
+
|
317 |
+
with gr.Accordion("Examples of Effective Comments", open=False):
|
318 |
+
example_comment_1 = gr.Textbox(
|
319 |
+
label="Example 1",
|
320 |
+
lines=4,
|
321 |
+
value="Gotta say I love this one. The idea of collage painting really appeals to me. I can pick up on the subtle shadows. The combination of soft, creamy yellow and warm green looks really nice too. The paper texture itself is really interesting.",
|
322 |
+
)
|
323 |
+
|
324 |
+
example_comment_2 = gr.Textbox(
|
325 |
+
label="Example 2",
|
326 |
+
lines=4,
|
327 |
+
value="I adore the blue and greenish-blue palette, blue Dianne, and dark colors of this image. I also appreciate the Hergé inspiration in this artwork. However, I would have preferred a more complex and adventurous concept rather than a simple landscape. I wish it was more surreal and creepy.",
|
328 |
+
)
|
329 |
+
|
330 |
+
comment_image = gr.Image(value=comment_images[0], label=f"image {image_index+1}/{len(comment_images)}", show_label=True)
|
331 |
+
|
332 |
+
comment = gr.Text(
|
333 |
+
label="Comment",
|
334 |
+
show_label=False,
|
335 |
+
lines=2,
|
336 |
+
max_lines=3,
|
337 |
+
placeholder="Enter your comment",
|
338 |
+
container=False,
|
339 |
+
)
|
340 |
+
|
341 |
+
with gr.Row():
|
342 |
+
submit_comment_button = gr.Button("Submit comment", scale=0)
|
343 |
+
previous_image_botton = gr.Button("Previous Image", scale=0)
|
344 |
+
next_image_botton = gr.Button("Next Image", scale=0)
|
345 |
+
|
346 |
+
file_output = gr.File(visible=False)
|
347 |
+
upload_button = gr.UploadButton("Click to upload images", file_types=["image"], file_count="multiple")
|
348 |
+
|
349 |
+
clear_botton = gr.Button("Clear comments", interactive=len(comments) != 0)
|
350 |
+
|
351 |
+
with gr.Accordion("Enter GPT API for Better Results (optional)", open=False):
|
352 |
+
with gr.Row():
|
353 |
+
api = gr.Text(
|
354 |
+
max_lines=1,
|
355 |
+
placeholder="Enter your API",
|
356 |
+
container=False,
|
357 |
+
)
|
358 |
+
|
359 |
+
api_button = gr.Button("Enter", scale=0)
|
360 |
+
|
361 |
+
extract_vp_botton = gr.Button(f"Extract visual preference from {len(comments)} comments", interactive=len(comments) != 0)
|
362 |
+
|
363 |
+
with gr.Column(elem_id="gen-container"):
|
364 |
+
gr.Markdown(f"""
|
365 |
+
You can edit your visual preference in case of hallucinations.
|
366 |
+
"""
|
367 |
+
)
|
368 |
+
|
369 |
+
positive_extracted_vp = gr.Textbox(
|
370 |
+
label="Liked visual attributes",
|
371 |
+
lines=3,
|
372 |
+
value="",
|
373 |
+
)
|
374 |
+
|
375 |
+
negative_extracted_vp = gr.Textbox(
|
376 |
+
label="Disliked visual attributes",
|
377 |
+
lines=1,
|
378 |
+
value="",
|
379 |
+
)
|
380 |
+
|
381 |
+
gr.Markdown(f"""
|
382 |
+
## Step 2: Personalized image generation (using Stable Diffusion XL)
|
383 |
+
Write down the prompt to generate your preferred images once your visual preference has been extracted from your comments.
|
384 |
+
""")
|
385 |
+
|
386 |
+
slider = gr.Slider(value=0.85, minimum=0, maximum=1.5, label="Personalization degree", interactive=True)
|
387 |
+
|
388 |
+
with gr.Row():
|
389 |
+
|
390 |
+
prompt = gr.Text(
|
391 |
+
label="Prompt",
|
392 |
+
show_label=False,
|
393 |
+
max_lines=1,
|
394 |
+
placeholder="Enter your prompt",
|
395 |
+
container=False,
|
396 |
+
)
|
397 |
+
|
398 |
+
run_button = gr.Button("Run", scale=0, interactive=False)
|
399 |
+
|
400 |
+
result = gr.Image(label="Result", show_label=False, interactive=False, value="Train running through the coun (6).png")
|
401 |
+
|
402 |
+
with gr.Row(elem_id="main-container"):
|
403 |
+
with gr.Accordion("images generated from the same prompt but different extracted preferences (prompt: Picture of a lady)", open=False):
|
404 |
+
examples = [
|
405 |
+
"examples/(0).png",
|
406 |
+
"examples/(16).png",
|
407 |
+
"examples/(2).png",
|
408 |
+
"examples/(12).png",
|
409 |
+
"examples/(13).png",
|
410 |
+
"examples/(14).png",
|
411 |
+
"examples/(15).png",
|
412 |
+
"examples/(17).png",
|
413 |
+
"examples/(11).png",
|
414 |
+
"examples/(18).png",
|
415 |
+
]
|
416 |
+
gallery = gr.Gallery(
|
417 |
+
value=examples,
|
418 |
+
label="",
|
419 |
+
show_label=False,
|
420 |
+
columns=[5],
|
421 |
+
rows=[2],
|
422 |
+
object_fit="contain",
|
423 |
+
height=500)
|
424 |
+
|
425 |
+
|
426 |
+
submit_comment_button.click(
|
427 |
+
fn = submit_comment,
|
428 |
+
inputs = [comment],
|
429 |
+
outputs = [comment_image, comment, extract_vp_botton, clear_botton]
|
430 |
+
)
|
431 |
+
|
432 |
+
previous_image_botton.click(
|
433 |
+
fn = previous_image,
|
434 |
+
inputs = [],
|
435 |
+
outputs = [comment_image, comment]
|
436 |
+
)
|
437 |
+
|
438 |
+
next_image_botton.click(
|
439 |
+
fn = next_image,
|
440 |
+
inputs = [],
|
441 |
+
outputs = [comment_image, comment]
|
442 |
+
)
|
443 |
+
|
444 |
+
extract_vp_botton.click(
|
445 |
+
fn = extract_vp,
|
446 |
+
inputs = [],
|
447 |
+
outputs = [positive_extracted_vp, negative_extracted_vp, run_button]
|
448 |
+
)
|
449 |
+
|
450 |
+
api_button.click(
|
451 |
+
fn = api_fn,
|
452 |
+
inputs = [api],
|
453 |
+
outputs = [],
|
454 |
+
)
|
455 |
+
|
456 |
+
run_button.click(
|
457 |
+
fn = generate,
|
458 |
+
inputs = [prompt, positive_extracted_vp, negative_extracted_vp, slider],
|
459 |
+
outputs = [result],
|
460 |
+
)
|
461 |
+
|
462 |
+
positive_extracted_vp.change(
|
463 |
+
fn = change_vp,
|
464 |
+
inputs = [positive_extracted_vp],
|
465 |
+
outputs = [],
|
466 |
+
)
|
467 |
+
|
468 |
+
negative_extracted_vp.change(
|
469 |
+
fn = change_vp,
|
470 |
+
inputs = [negative_extracted_vp],
|
471 |
+
outputs = [],
|
472 |
+
)
|
473 |
+
|
474 |
+
clear_botton.click(
|
475 |
+
fn = clear_comments,
|
476 |
+
inputs = [],
|
477 |
+
outputs = [extract_vp_botton, clear_botton]
|
478 |
+
)
|
479 |
+
|
480 |
+
upload_button.upload(
|
481 |
+
upload_file,
|
482 |
+
upload_button,
|
483 |
+
[comment_image, comment]
|
484 |
+
)
|
485 |
+
|
486 |
+
demo.launch(share=True)
|
487 |
+
|
488 |
+
|
489 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
diffusers
|
3 |
+
invisible_watermark
|
4 |
+
torch
|
5 |
+
transformers
|
6 |
+
xformers
|
7 |
+
accelerate
|
8 |
+
bitsandbytes
|
9 |
+
peft
|
test.png
ADDED
Git LFS Details
|