File size: 5,125 Bytes
a883cc0
 
 
3b02084
 
 
f17c211
 
 
 
 
a883cc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60fb1ce
 
 
 
 
 
 
a883cc0
60fb1ce
a883cc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60fb1ce
 
 
 
 
 
 
 
 
 
a883cc0
60fb1ce
a883cc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60fb1ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os, sys
import pandas as pd

print("current directory:")
print(os.curdir)
print(os.getcwd())
TOP_COMMENTARIES_DIR = os.path.join(
   os.getcwd(),
   "prompting",
   "top_commentaries"
   )


ARTEMIS_EMOTIONS = [
    "amusement",         
    "anger",              
    "awe",               
    "contentment",       
    "disgust",           
    "excitement",        
    "fear",              
    "sadness",           
    "something else",    
]

DEFAULT_N_SAMPLES = 4
DEFAULT_RANDOM_STATE = 42
DEFAULT_OBJECT_THRESHOLD = 0.1





def fill_extracted_items(
    colors_list = None,
    objects_list = None,
    emotion = None,
    commentary = None,
):
  result = ""
  if colors_list:
    result += f"colors: {', '.join(colors_list)}"
    result += "\n"
  if objects_list:
    result += f"objects: {', '.join(objects_list)}"
    result += "\n"
  if emotion:
    result += f"emotion: {emotion}"
    result += "\n"
  if commentary:
    result += f"commentary: {commentary}"
    result += "\n"
  else:
    result += "commentary: "

  return result


def load_dataframe(
    csv_filepath,
):
    df = pd.read_csv(csv_filepath, index_col = 0)
    for stringified_col in  [
                                "maskrcnn_objects",
                                "colors",
                                "clip_recognized_objects", 
                            ]:
        df[stringified_col] = df[stringified_col].apply(eval)

    return df

TOP_COMMENTARIES_DFS = {
    emotion : load_dataframe(os.path.join(TOP_COMMENTARIES_DIR, f"top_{emotion.replace(' ', '_')}.csv"))
    for emotion in ARTEMIS_EMOTIONS
    }
  

def filter_items(
    items_and_probs_list,
    items_threshold = DEFAULT_OBJECT_THRESHOLD
    ):
    return [item for item, prob in items_and_probs_list if prob > items_threshold]

def get_random_samples_for_emotion(
    emotion,
    n_samples = DEFAULT_N_SAMPLES,
    random_state = DEFAULT_RANDOM_STATE,
    object_threshold = DEFAULT_OBJECT_THRESHOLD
):
    emotion_df = TOP_COMMENTARIES_DFS[emotion]
    samples = emotion_df.sample(n_samples, random_state = random_state)



    result = []
    for _, sample in samples.iterrows():
      colors_list = sample["colors"]
      commentary = sample["utterance"]
      emotion = sample["emotion"]

      objects_and_probs_list = sample["clip_recognized_objects"]
      objects_list = filter_items(objects_and_probs_list, object_threshold)

      entry = {
        "colors_list" : colors_list,
        "objects_list" : objects_list,
        "emotion" : emotion,
        "commentary" : commentary,
      }
      result.append(entry)

    return result

def get_subprompt_for_emotion(
    emotion,
    n_samples = DEFAULT_N_SAMPLES,
    random_state = DEFAULT_RANDOM_STATE,
    object_threshold = DEFAULT_OBJECT_THRESHOLD,
):
    random_samples = get_random_samples_for_emotion(
        emotion = emotion,
        n_samples = n_samples,
        random_state = random_state,
        object_threshold=object_threshold,
    )
    subprompt = [
            fill_extracted_items(**entry) for entry in random_samples    
    ]
    subprompt = "\n".join(subprompt)

    return subprompt


def get_subprompt_with_examples(
    n_samples_per_emotion = DEFAULT_N_SAMPLES,
    random_state = DEFAULT_RANDOM_STATE,
    object_threshold = DEFAULT_OBJECT_THRESHOLD,
):

    examples = [
        get_subprompt_for_emotion(
            emotion=emotion,
            n_samples = n_samples_per_emotion,
            random_state = random_state,
            object_threshold = object_threshold
        )   
        for emotion in ARTEMIS_EMOTIONS
    ]

    examples = "\n".join(examples)

    return examples

def get_user_prompt(
    colors_list,
    objects_list,
    emotion,
    n_samples_per_emotion = DEFAULT_N_SAMPLES,
    random_state = DEFAULT_RANDOM_STATE,
    object_threshold = DEFAULT_OBJECT_THRESHOLD,
):
    user_prompt= (
        "You have to write a commentary for an artwork.\n"
        "To write the commentary, you are given the objects present in the picture, "
        "the colors present in the picture, and the emotion the picture evokes.\n"
        "You are first shown several examples, and then have to give your commentary.\n"
        "First come the examples, and then the objects, colors, and emotion you will have to use for your commentary.\n"
        "Avoid explicitly mentioning the objects, or colors, or emotion, if it sounds more natural.\n"
        "Only write the commentary.\n"
        "\n"
        "EXAMPLES:"
        "\n\n"
        "{examples}"
        "\n"
        "Now, write your personal opinion about the picture."
        "\n"
        "{image_subprompt}"
    )

    examples = get_subprompt_with_examples(
        n_samples_per_emotion = n_samples_per_emotion,  
        random_state = random_state,
        object_threshold=object_threshold,
    )

    image_subprompt = fill_extracted_items(
        colors_list = colors_list,
        objects_list = objects_list,
        emotion = emotion,
        commentary = None,
    )


    result = user_prompt.format(examples = examples, image_subprompt = image_subprompt)

    return result