LuisV commited on
Commit
a883cc0
1 Parent(s): 20cd584

adding utilities for generating prompts

Browse files
Files changed (1) hide show
  1. prompting/promptingutils.py +190 -0
prompting/promptingutils.py ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, sys
2
+ import pandas as pd
3
+
4
+
5
+ TOP_COMMENTARIES_DIR = os.path.join(os.curdir, "top_commentaries")
6
+
7
+
8
+ ARTEMIS_EMOTIONS = [
9
+ "amusement",
10
+ "anger",
11
+ "awe",
12
+ "contentment",
13
+ "disgust",
14
+ "excitement",
15
+ "fear",
16
+ "sadness",
17
+ "something else",
18
+ ]
19
+
20
+ DEFAULT_N_SAMPLES = 4
21
+ DEFAULT_RANDOM_STATE = 42
22
+ DEFAULT_OBJECT_THRESHOLD = 0.1
23
+
24
+
25
+
26
+
27
+
28
+ def fill_extracted_items(
29
+ colors_list = None,
30
+ objects_list = None,
31
+ emotion = None,
32
+ commentary = None,
33
+ ):
34
+ result = ""
35
+ if colors_list:
36
+ result += f"colors: {', '.join(colors_list)}"
37
+ result += "\n"
38
+ if objects_list:
39
+ result += f"objects: {', '.join(objects_list)}"
40
+ result += "\n"
41
+ if emotion:
42
+ result += f"emotion: {emotion}"
43
+ result += "\n"
44
+ if commentary:
45
+ result += f"commentary: {commentary}"
46
+ result += "\n"
47
+ else:
48
+ result += "commentary: "
49
+
50
+ return result
51
+
52
+
53
+ def load_dataframe(
54
+ csv_filepath,
55
+ ):
56
+ df = pd.read_csv(csv_filepath, index_col = 0)
57
+ for stringified_col in [
58
+ "maskrcnn_objects",
59
+ "colors",
60
+ "clip_recognized_objects",
61
+ ]:
62
+ df[stringified_col] = df[stringified_col].apply(eval)
63
+
64
+ return df
65
+
66
+ TOP_COMMENTARIES_DFS = {
67
+ emotion : load_dataframe(os.path.join(TOP_COMMENTARIES_DIR, f"top_{emotion.replace(' ', '_')}.csv"))
68
+ for emotion in ARTEMIS_EMOTIONS
69
+ }
70
+
71
+
72
+ def filter_items(
73
+ items_and_probs_list,
74
+ items_threshold = DEFAULT_OBJECT_THRESHOLD
75
+ ):
76
+ return [item for item, prob in items_and_probs_list if prob > items_threshold]
77
+
78
+ def get_random_samples_for_emotion(
79
+ emotion,
80
+ n_samples = DEFAULT_N_SAMPLES,
81
+ random_state = DEFAULT_RANDOM_STATE,
82
+ object_threshold = DEFAULT_OBJECT_THRESHOLD
83
+ ):
84
+ emotion_df = TOP_COMMENTARIES_DFS[emotion]
85
+ samples = emotion_df.sample(n_samples, random_state = random_state)
86
+
87
+
88
+
89
+ result = []
90
+ for _, sample in samples.iterrows():
91
+ colors_list = sample["colors"]
92
+ commentary = sample["utterance"]
93
+ emotion = sample["emotion"]
94
+
95
+ objects_and_probs_list = sample["clip_recognized_objects"]
96
+ objects_list = filter_items(objects_and_probs_list, object_threshold)
97
+
98
+ entry = {
99
+ "colors_list" : colors_list,
100
+ "objects_list" : objects_list,
101
+ "emotion" : emotion,
102
+ "commentary" : commentary,
103
+ }
104
+ result.append(entry)
105
+
106
+ return result
107
+
108
+ def get_subprompt_for_emotion(
109
+ emotion,
110
+ n_samples = DEFAULT_N_SAMPLES,
111
+ random_state = DEFAULT_RANDOM_STATE,
112
+ object_threshold = DEFAULT_OBJECT_THRESHOLD,
113
+ ):
114
+ random_samples = get_random_samples_for_emotion(
115
+ emotion = emotion,
116
+ n_samples = n_samples,
117
+ random_state = random_state,
118
+ object_threshold=object_threshold,
119
+ )
120
+ subprompt = [
121
+ fill_extracted_items(**entry) for entry in random_samples
122
+ ]
123
+ subprompt = "\n".join(subprompt)
124
+
125
+ return subprompt
126
+
127
+
128
+ def get_subprompt_with_examples(
129
+ n_samples_per_emotion = DEFAULT_N_SAMPLES,
130
+ random_state = DEFAULT_RANDOM_STATE,
131
+ object_threshold = DEFAULT_OBJECT_THRESHOLD,
132
+ ):
133
+
134
+ examples = [
135
+ get_subprompt_for_emotion(
136
+ emotion=emotion,
137
+ n_samples = n_samples_per_emotion,
138
+ random_state = random_state,
139
+ object_threshold = object_threshold
140
+ )
141
+ for emotion in ARTEMIS_EMOTIONS
142
+ ]
143
+
144
+ examples = "\n".join(examples)
145
+
146
+ return examples
147
+
148
+ def get_user_prompt(
149
+ colors_list,
150
+ objects_list,
151
+ emotion,
152
+ n_samples_per_emotion = DEFAULT_N_SAMPLES,
153
+ random_state = DEFAULT_RANDOM_STATE,
154
+ object_threshold = DEFAULT_OBJECT_THRESHOLD,
155
+ ):
156
+ user_prompt= (
157
+ "You have to write a commentary for an artwork.\n"
158
+ "To write the commentary, you are given the objects present in the picture, "
159
+ "the colors present in the picture, and the emotion the picture evokes.\n"
160
+ "You are first shown several examples, and then have to give your commentary.\n"
161
+ "First come the examples, and then the objects, colors, and emotion you will have to use for your commentary.\n"
162
+ "Avoid explicitly mentioning the objects, or colors, or emotion, if it sounds more natural.\n"
163
+ "Only write the commentary.\n"
164
+ "\n"
165
+ "EXAMPLES:"
166
+ "\n\n"
167
+ "{examples}"
168
+ "\n"
169
+ "Now, write your personal opinion about the picture."
170
+ "\n"
171
+ "{image_subprompt}"
172
+ )
173
+
174
+ examples = get_subprompt_with_examples(
175
+ n_samples_per_emotion = n_samples_per_emotion,
176
+ random_state = random_state,
177
+ object_threshold=object_threshold,
178
+ )
179
+
180
+ image_subprompt = fill_extracted_items(
181
+ colors_list = colors_list,
182
+ objects_list = objects_list,
183
+ emotion = emotion,
184
+ commentary = None,
185
+ )
186
+
187
+
188
+ result = user_prompt.format(examples = examples, image_subprompt = image_subprompt)
189
+
190
+ return result