Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,9 +16,9 @@ from diffusers import StableDiffusionXLPipeline, DiffusionPipeline
|
|
16 |
import anthropic
|
17 |
import base64
|
18 |
from datasets import load_dataset
|
|
|
19 |
|
20 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
-
print(device)
|
22 |
|
23 |
word_list_dataset = load_dataset("EPFL-VILAB/4m-wordlist", data_files="list.txt", use_auth_token=True)
|
24 |
word_list = word_list_dataset["train"]['text']
|
@@ -50,7 +50,6 @@ else:
|
|
50 |
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", use_safetensors=True)
|
51 |
pipe = pipe.to(device)
|
52 |
|
53 |
-
|
54 |
MAX_SEED = np.iinfo(np.int32).max
|
55 |
MAX_IMAGE_SIZE = 1024
|
56 |
|
@@ -75,13 +74,10 @@ css="""
|
|
75 |
}
|
76 |
"""
|
77 |
|
78 |
-
|
79 |
if torch.cuda.is_available():
|
80 |
power_device = "GPU"
|
81 |
else:
|
82 |
power_device = "CPU"
|
83 |
-
|
84 |
-
from PIL import Image
|
85 |
|
86 |
comment_images = [
|
87 |
"comment_images/15.png",
|
@@ -158,7 +154,6 @@ examples = {
|
|
158 |
"examples/A person reaching fo_0.png",
|
159 |
"examples/A person reaching fo_1.png",
|
160 |
"examples/A person reaching fo_2.png",
|
161 |
-
#"examples/A person reaching fo_3.png",
|
162 |
"examples/A person reaching fo_4.png",
|
163 |
"examples/A person reaching fo_5.png",
|
164 |
"examples/A person reaching fo_6.png",
|
@@ -170,7 +165,6 @@ examples = {
|
|
170 |
"examples/Abandoned robot at t_0.png",
|
171 |
"examples/Abandoned robot at t_1.png",
|
172 |
"examples/Abandoned robot at t_2.png",
|
173 |
-
#"examples/Abandoned robot at t_3.png",
|
174 |
"examples/Abandoned robot at t_4.png",
|
175 |
"examples/Abandoned robot at t_5.png",
|
176 |
"examples/Abandoned robot at t_6.png",
|
@@ -182,7 +176,6 @@ examples = {
|
|
182 |
"examples/Cityscape during a t_0.png",
|
183 |
"examples/Cityscape during a t_1.png",
|
184 |
"examples/Cityscape during a t_2.png",
|
185 |
-
#"examples/Cityscape during a t_3.png",
|
186 |
"examples/Cityscape during a t_4.png",
|
187 |
"examples/Cityscape during a t_5.png",
|
188 |
"examples/Cityscape during a t_6.png",
|
@@ -194,7 +187,6 @@ examples = {
|
|
194 |
"examples/Human in a frame_0.png",
|
195 |
"examples/Human in a frame_1.png",
|
196 |
"examples/Human in a frame_2.png",
|
197 |
-
#"examples/Human in a frame_3.png",
|
198 |
"examples/Human in a frame_4.png",
|
199 |
"examples/Human in a frame_5.png",
|
200 |
"examples/Human in a frame_6.png",
|
@@ -206,7 +198,6 @@ examples = {
|
|
206 |
"examples/Inside an abondoned _0.png",
|
207 |
"examples/Inside an abondoned _1.png",
|
208 |
"examples/Inside an abondoned _2.png",
|
209 |
-
#"examples/Inside an abondoned _3.png",
|
210 |
"examples/Inside an abondoned _4.png",
|
211 |
"examples/Inside an abondoned _5.png",
|
212 |
"examples/Inside an abondoned _6.png",
|
@@ -218,7 +209,6 @@ examples = {
|
|
218 |
"examples/Lonely astronaut in _0.png",
|
219 |
"examples/Lonely astronaut in _1.png",
|
220 |
"examples/Lonely astronaut in _2.png",
|
221 |
-
#"examples/Lonely astronaut in _3.png",
|
222 |
"examples/Lonely astronaut in _4.png",
|
223 |
"examples/Lonely astronaut in _5.png",
|
224 |
"examples/Lonely astronaut in _6.png",
|
@@ -230,7 +220,6 @@ examples = {
|
|
230 |
"examples/Painting of a lady_0.png",
|
231 |
"examples/Painting of a lady_1.png",
|
232 |
"examples/Painting of a lady_2.png",
|
233 |
-
#"examples/Painting of a lady_3.png",
|
234 |
"examples/Painting of a lady_4.png",
|
235 |
"examples/Painting of a lady_5.png",
|
236 |
"examples/Painting of a lady_6.png",
|
@@ -251,7 +240,6 @@ def submit_comment(comment):
|
|
251 |
comments[comment_images[0]] = comment
|
252 |
comment_images.append(comment_images[0])
|
253 |
comment_images = comment_images[1:]
|
254 |
-
|
255 |
image_index = (image_index + 1) % len(comment_images)
|
256 |
|
257 |
elif comment_images[0] in comments:
|
@@ -310,7 +298,7 @@ def clear_comments():
|
|
310 |
def extract_vp_from_vpe():
|
311 |
global comments
|
312 |
if len(comments) < 8:
|
313 |
-
gr.Warning("
|
314 |
|
315 |
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.
|
316 |
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.
|
@@ -351,8 +339,13 @@ Here are the images and their corresponding comments:
|
|
351 |
generated_ids = vpe_model.generate(**inputs, max_new_tokens=2000, repetition_penalty=0.99, do_sample=False)
|
352 |
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
353 |
|
354 |
-
|
355 |
-
|
|
|
|
|
|
|
|
|
|
|
356 |
|
357 |
return positive_vp, negative_vp
|
358 |
|
@@ -361,7 +354,6 @@ def extract_vp():
|
|
361 |
if valid_api == "":
|
362 |
positive_vp, negative_vp = extract_vp_from_vpe()
|
363 |
|
364 |
-
|
365 |
else:
|
366 |
client = anthropic.Anthropic(
|
367 |
api_key=valid_api,
|
@@ -369,31 +361,23 @@ def extract_vp():
|
|
369 |
|
370 |
prompt = """**Objective:**
|
371 |
Analyze a set of artworks and accompanying comments from a person to identify artistic features they like and dislike.
|
372 |
-
|
373 |
**Steps:**
|
374 |
-
|
375 |
1. **Analyze Artworks and Comments:**
|
376 |
- Examine each artwork for artistic features such as colors, style, composition, mood, medium, texture, brushwork, lighting, shadow effects, perspective, and other noteworthy elements.
|
377 |
- Review the accompanying comments to understand the person's preferences and opinions on these features.
|
378 |
-
|
379 |
2. **Identify Preferences:**
|
380 |
- Extract artistic features that the person likes and dislikes based on the artworks' features and the comments.
|
381 |
- Focus solely on artistic aspects and ignore the subject matter.
|
382 |
- Convert the art features mentioned in the comments to well-known synonyms if needed.
|
383 |
-
|
384 |
3. **Resolve Ambiguous Preferences:**
|
385 |
- If the person expresses a preference without clearly stating its category (e.g., "I like the style" without specifying which style), identify these specific features from the images directly.
|
386 |
- Make the person's preference understandable and independednt of the artworks.
|
387 |
-
|
388 |
4. **Output Format:**
|
389 |
- Create two concise lists of keywords: one for features the person likes and another for features they dislike.
|
390 |
- Ensure the lists are in keyword format, divided by commas, without using sentences.
|
391 |
- Maintain detail and accuracy for all comments and images.
|
392 |
-
|
393 |
**Your Task:**
|
394 |
-
|
395 |
Follow the example format and ensure that your output consists of two lists of keywords summarizing the person's preferences based on the artworks and comments provided. Consider all comments and images comprehensively.
|
396 |
-
|
397 |
**Example**: example START:
|
398 |
"""
|
399 |
messages = [
|
@@ -448,8 +432,13 @@ Follow the example format and ensure that your output consists of two lists of k
|
|
448 |
)
|
449 |
|
450 |
generated_text = message.content[0].text
|
451 |
-
|
452 |
-
|
|
|
|
|
|
|
|
|
|
|
453 |
|
454 |
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 personalized generation", scale=0, interactive=True, variant="primary")
|
455 |
|
@@ -539,8 +528,6 @@ def generate(prompt, vp_pos, vp_neg, slider, example_prompt, gallery, num_infere
|
|
539 |
object_fit="contain",
|
540 |
height=500)
|
541 |
|
542 |
-
|
543 |
-
|
544 |
return image, example_prompt, gallery
|
545 |
|
546 |
def change_vp(extract_vp):
|
@@ -690,9 +677,6 @@ with gr.Blocks(css=css, title="ViPer Demo", theme=gr.themes.Base()) as demo:
|
|
690 |
slider = gr.Slider(value=0.85, minimum=0, maximum=1.5, label="Personalization degree", interactive=True)
|
691 |
|
692 |
with gr.Row():
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
prompt = gr.Dropdown(
|
697 |
example_prompts, label="Prompt", info="Enter your prompt or choose an example prompts from the dropdown.", allow_custom_value=True, multiselect=False, show_label=False
|
698 |
)
|
@@ -723,9 +707,7 @@ with gr.Blocks(css=css, title="ViPer Demo", theme=gr.themes.Base()) as demo:
|
|
723 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
724 |
|
725 |
with gr.Row(elem_id="main-container"):
|
726 |
-
with gr.Accordion("Images generated from the example prompts, but with different extracted preferences. The first image shows the non-personalized baseline generation.", open=False):
|
727 |
-
|
728 |
-
|
729 |
example_prompt = gr.Markdown(f"Prompt: {example_prompts[0]}")
|
730 |
gallery = gr.Gallery(
|
731 |
value=examples[example_prompts[0]],
|
@@ -740,7 +722,6 @@ with gr.Blocks(css=css, title="ViPer Demo", theme=gr.themes.Base()) as demo:
|
|
740 |
pre_prompt_button = gr.Button("⬅ Previous prompt", scale=1, interactive=True)
|
741 |
next_prompt_button = gr.Button("Next prompt ➡", scale=1, interactive=True)
|
742 |
|
743 |
-
|
744 |
submit_comment_button.click(
|
745 |
fn = submit_comment,
|
746 |
inputs = [comment],
|
|
|
16 |
import anthropic
|
17 |
import base64
|
18 |
from datasets import load_dataset
|
19 |
+
from PIL import Image
|
20 |
|
21 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
22 |
|
23 |
word_list_dataset = load_dataset("EPFL-VILAB/4m-wordlist", data_files="list.txt", use_auth_token=True)
|
24 |
word_list = word_list_dataset["train"]['text']
|
|
|
50 |
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", use_safetensors=True)
|
51 |
pipe = pipe.to(device)
|
52 |
|
|
|
53 |
MAX_SEED = np.iinfo(np.int32).max
|
54 |
MAX_IMAGE_SIZE = 1024
|
55 |
|
|
|
74 |
}
|
75 |
"""
|
76 |
|
|
|
77 |
if torch.cuda.is_available():
|
78 |
power_device = "GPU"
|
79 |
else:
|
80 |
power_device = "CPU"
|
|
|
|
|
81 |
|
82 |
comment_images = [
|
83 |
"comment_images/15.png",
|
|
|
154 |
"examples/A person reaching fo_0.png",
|
155 |
"examples/A person reaching fo_1.png",
|
156 |
"examples/A person reaching fo_2.png",
|
|
|
157 |
"examples/A person reaching fo_4.png",
|
158 |
"examples/A person reaching fo_5.png",
|
159 |
"examples/A person reaching fo_6.png",
|
|
|
165 |
"examples/Abandoned robot at t_0.png",
|
166 |
"examples/Abandoned robot at t_1.png",
|
167 |
"examples/Abandoned robot at t_2.png",
|
|
|
168 |
"examples/Abandoned robot at t_4.png",
|
169 |
"examples/Abandoned robot at t_5.png",
|
170 |
"examples/Abandoned robot at t_6.png",
|
|
|
176 |
"examples/Cityscape during a t_0.png",
|
177 |
"examples/Cityscape during a t_1.png",
|
178 |
"examples/Cityscape during a t_2.png",
|
|
|
179 |
"examples/Cityscape during a t_4.png",
|
180 |
"examples/Cityscape during a t_5.png",
|
181 |
"examples/Cityscape during a t_6.png",
|
|
|
187 |
"examples/Human in a frame_0.png",
|
188 |
"examples/Human in a frame_1.png",
|
189 |
"examples/Human in a frame_2.png",
|
|
|
190 |
"examples/Human in a frame_4.png",
|
191 |
"examples/Human in a frame_5.png",
|
192 |
"examples/Human in a frame_6.png",
|
|
|
198 |
"examples/Inside an abondoned _0.png",
|
199 |
"examples/Inside an abondoned _1.png",
|
200 |
"examples/Inside an abondoned _2.png",
|
|
|
201 |
"examples/Inside an abondoned _4.png",
|
202 |
"examples/Inside an abondoned _5.png",
|
203 |
"examples/Inside an abondoned _6.png",
|
|
|
209 |
"examples/Lonely astronaut in _0.png",
|
210 |
"examples/Lonely astronaut in _1.png",
|
211 |
"examples/Lonely astronaut in _2.png",
|
|
|
212 |
"examples/Lonely astronaut in _4.png",
|
213 |
"examples/Lonely astronaut in _5.png",
|
214 |
"examples/Lonely astronaut in _6.png",
|
|
|
220 |
"examples/Painting of a lady_0.png",
|
221 |
"examples/Painting of a lady_1.png",
|
222 |
"examples/Painting of a lady_2.png",
|
|
|
223 |
"examples/Painting of a lady_4.png",
|
224 |
"examples/Painting of a lady_5.png",
|
225 |
"examples/Painting of a lady_6.png",
|
|
|
240 |
comments[comment_images[0]] = comment
|
241 |
comment_images.append(comment_images[0])
|
242 |
comment_images = comment_images[1:]
|
|
|
243 |
image_index = (image_index + 1) % len(comment_images)
|
244 |
|
245 |
elif comment_images[0] in comments:
|
|
|
298 |
def extract_vp_from_vpe():
|
299 |
global comments
|
300 |
if len(comments) < 8:
|
301 |
+
gr.Warning("Fewer than 8 comments may lead to errors.")
|
302 |
|
303 |
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.
|
304 |
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.
|
|
|
339 |
generated_ids = vpe_model.generate(**inputs, max_new_tokens=2000, repetition_penalty=0.99, do_sample=False)
|
340 |
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
341 |
|
342 |
+
if re.match(r"(.|\n)*Assistant: Liked Art Features: (.|\n)*Disliked Art Features: (.|\n)*", generated_texts):
|
343 |
+
positive_vp, negative_vp = re.search('.* \nAssistant: Liked Art Features: (.*)\nDisliked Art Features: (.*)', generated_texts).groups()
|
344 |
+
gr.Info("Visual preference successfully extracted.")
|
345 |
+
else:
|
346 |
+
positive_vp = ""
|
347 |
+
negative_vp = ""
|
348 |
+
gr.Warning("VP extraction failed: Please comment on more images.")
|
349 |
|
350 |
return positive_vp, negative_vp
|
351 |
|
|
|
354 |
if valid_api == "":
|
355 |
positive_vp, negative_vp = extract_vp_from_vpe()
|
356 |
|
|
|
357 |
else:
|
358 |
client = anthropic.Anthropic(
|
359 |
api_key=valid_api,
|
|
|
361 |
|
362 |
prompt = """**Objective:**
|
363 |
Analyze a set of artworks and accompanying comments from a person to identify artistic features they like and dislike.
|
|
|
364 |
**Steps:**
|
|
|
365 |
1. **Analyze Artworks and Comments:**
|
366 |
- Examine each artwork for artistic features such as colors, style, composition, mood, medium, texture, brushwork, lighting, shadow effects, perspective, and other noteworthy elements.
|
367 |
- Review the accompanying comments to understand the person's preferences and opinions on these features.
|
|
|
368 |
2. **Identify Preferences:**
|
369 |
- Extract artistic features that the person likes and dislikes based on the artworks' features and the comments.
|
370 |
- Focus solely on artistic aspects and ignore the subject matter.
|
371 |
- Convert the art features mentioned in the comments to well-known synonyms if needed.
|
|
|
372 |
3. **Resolve Ambiguous Preferences:**
|
373 |
- If the person expresses a preference without clearly stating its category (e.g., "I like the style" without specifying which style), identify these specific features from the images directly.
|
374 |
- Make the person's preference understandable and independednt of the artworks.
|
|
|
375 |
4. **Output Format:**
|
376 |
- Create two concise lists of keywords: one for features the person likes and another for features they dislike.
|
377 |
- Ensure the lists are in keyword format, divided by commas, without using sentences.
|
378 |
- Maintain detail and accuracy for all comments and images.
|
|
|
379 |
**Your Task:**
|
|
|
380 |
Follow the example format and ensure that your output consists of two lists of keywords summarizing the person's preferences based on the artworks and comments provided. Consider all comments and images comprehensively.
|
|
|
381 |
**Example**: example START:
|
382 |
"""
|
383 |
messages = [
|
|
|
432 |
)
|
433 |
|
434 |
generated_text = message.content[0].text
|
435 |
+
if re.match(r"(.|\n)*Like.*:(.|\n)*Dislike.(.|\n)*", generated_texts):
|
436 |
+
positive_vp, negative_vp = re.search('.*Like.*:\n(.*)\n*Dislike.*:\n(.*)', generated_text).groups()
|
437 |
+
gr.Info("Visual preference successfully extracted.")
|
438 |
+
else:
|
439 |
+
positive_vp = ""
|
440 |
+
negative_vp = ""
|
441 |
+
gr.Warning("VP extraction failed: Please comment on more images.")
|
442 |
|
443 |
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 personalized generation", scale=0, interactive=True, variant="primary")
|
444 |
|
|
|
528 |
object_fit="contain",
|
529 |
height=500)
|
530 |
|
|
|
|
|
531 |
return image, example_prompt, gallery
|
532 |
|
533 |
def change_vp(extract_vp):
|
|
|
677 |
slider = gr.Slider(value=0.85, minimum=0, maximum=1.5, label="Personalization degree", interactive=True)
|
678 |
|
679 |
with gr.Row():
|
|
|
|
|
|
|
680 |
prompt = gr.Dropdown(
|
681 |
example_prompts, label="Prompt", info="Enter your prompt or choose an example prompts from the dropdown.", allow_custom_value=True, multiselect=False, show_label=False
|
682 |
)
|
|
|
707 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
708 |
|
709 |
with gr.Row(elem_id="main-container"):
|
710 |
+
with gr.Accordion("Images generated from the example prompts, but with different extracted preferences. The first image shows the non-personalized baseline generation.", open=False):
|
|
|
|
|
711 |
example_prompt = gr.Markdown(f"Prompt: {example_prompts[0]}")
|
712 |
gallery = gr.Gallery(
|
713 |
value=examples[example_prompts[0]],
|
|
|
722 |
pre_prompt_button = gr.Button("⬅ Previous prompt", scale=1, interactive=True)
|
723 |
next_prompt_button = gr.Button("Next prompt ➡", scale=1, interactive=True)
|
724 |
|
|
|
725 |
submit_comment_button.click(
|
726 |
fn = submit_comment,
|
727 |
inputs = [comment],
|