forked from CeeZh/LLoVi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompts.py
186 lines (160 loc) · 12.3 KB
/
prompts.py
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
from string import Template
def first_char_as_answer(res):
mapping = {'A':0, 'B':1, 'C':2, 'D':3, 'E':4}
if res[0] in mapping:
return mapping[res[0]]
return -1
def identity(res):
return res
def first_char_after_anchor(anchor):
def f(res):
mapping = {'A':0, 'B':1, 'C':2, 'D':3, 'E':4}
anchor_index = res.find(anchor)
pred = -1 # if decoding failed, return -1
if anchor_index >= 0:
pred_letter = res[anchor_index+len(anchor)]
if pred_letter in mapping:
pred = mapping[pred_letter]
return pred
return f
def get_intervals_as_list(text):
text = text.split('.')[0]
text = text.strip()
if text[-1] != ']':
index = text.rfind(']')
assert index > 0
text = text[:index+1]
interval_list_text = text.split('and')
intervals = []
for interval_text in interval_list_text:
if ',' not in interval_text:
intervals.append([0, 0])
continue
start_text, end_text = interval_text.split(',')
start_text, end_text = start_text.strip(' []'), end_text.strip(' []')
if start_text == 'None':
start_text = '0'
if end_text == 'None':
end_text = '1'
start, end = int(start_text), int(end_text)
intervals.append([start, end])
return intervals
class PromptTemplate(object):
def __init__(self, head, template, post_process_fn):
self.head = head
self.prompt_template = template
self.post_process_fn = post_process_fn
def get_num_stages(self):
return len(self.template)
def get_template_str(self):
template = []
for temp in self.prompt_template:
template.append(temp.safe_substitute())
return template
def fill(self, **kwargs):
# match variable names: duration, narration, question, optionA, optionB, optionC, optionD, optionE, num_words
prompt_filled = []
for temp in self.prompt_template:
prompt_filled.append(temp.substitute(kwargs))
return prompt_filled
class PromptFactory(object):
def __init__(self):
self.prompt_templates = self.build()
def build(self):
prompt_templates = {}
# egoschema QA (raw captions as input)
prompt_templates['qa_standard'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template("Please provide a single-letter answer (A, B, C, D, E) to the following multiple-choice question, and your answer must be one of the letters (A, B, C, D, or E). You must not provide any other response or explanation. You are given some language descriptions of a first person view video. The video is $duration seconds long. Each sentence describes a ${clip_length}s clip. The descriptions are sequential and non-overlapping which cover the whole video exactly. Here are the descriptions: $narration.\n You are going to answer a multiple choice question based on the descriptions, and your answer should be a single letter chosen from the choices.\n Here is the question: $question.\n Here are the choices.\n A: $optionA\n B: $optionB\n C: $optionC\n D: $optionD\n E: $optionE\n"),
],
post_process_fn = first_char_as_answer
)
# egoschema QA (raw captions as input) few-shot
prompt_templates['qa_standard_fewshot'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template("You are given some language descriptions of a first person view video. The video is $duration seconds long. You are also given a question and five potential choices. Your task is to answer with a correct choice based on the video descriptions. \nHere are a few examples. \n${examplars}\n\n Now answer this question.\nDescriptions: ${narration}.\n Question: ${question}\n A: ${optionA}.\n B: ${optionB}.\n C: ${optionC}.\n D: ${optionD}.\n E: ${optionE}.\n Answer: "),
],
post_process_fn = first_char_as_answer
)
# egoschema QA (summary as input)
prompt_templates['qa_sum'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template("Please provide a single-letter answer (A, B, C, D, E) to the following multiple-choice question, and your answer must be one of the letters (A, B, C, D, or E). You must not provide any other response or explanation. You are given some language descriptions of a first person view video. The video is $duration seconds long. Here are the descriptions: $narration.\n You are going to answer a multiple choice question based on the descriptions, and your answer should be a single letter chosen from the choices.\n Here is the question: $question.\n Here are the choices.\n A: $optionA\n B: $optionB\n C: $optionC\n D: $optionD\n E: $optionE\n"),
],
post_process_fn = first_char_as_answer
)
# egoschema sum (standard)
prompt_templates['sum_standard'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template('You are given some language descriptions of a first person view video. The video is $duration seconds long. Each sentence describes a ${clip_length}s clip. Here are the descriptions: $narration.\n Please give me a $num_words words summary.')
],
post_process_fn = identity
)
# egoschema sum (q)
prompt_templates['sum_q'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template('You are given some language descriptions of a first person view video. The video is $duration seconds long. Each sentence describes a ${clip_length}s clip. The descriptions are sequential and non-overlapping which cover the whole video exactly. Here are the descriptions: $narration.\n Please give me a $num_words words summary. When doing summarization, remember that your summary will be used to answer this multiple choice question: $question'),
],
post_process_fn = identity
)
# egoschema sum (qa)
prompt_templates['sum_qa'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template('You are given some language descriptions of a first person view video. The video is $duration seconds long. Each sentence describes a ${clip_length}s clip. Here are the descriptions: $narration.\n Please give me a $num_words words summary. When doing summarization, remember that your summary will be used to answer this multiple choice question: $question\n Here are the choices.\n A: $optionA\n B: $optionB\n C: $optionC\n D: $optionD\n E: $optionE\n Do not answer this question directly. Instead, use the question and choices to guide your summary.')
],
post_process_fn = identity
)
# egoschema QA zero-shot-CoT
prompt_templates['qa_zs-cot'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template("You are given some language descriptions of a first person view video. The video is $duration seconds long. Each sentence describes a ${clip_length}s clip. Here are the descriptions: $narration.\n You are going to answer a multiple choice question based on the descriptions, and your answer should be a single letter chosen from the choices.\n Here is the question: $question.\n Here are the choices.\n A: $optionA\n B: $optionB\n C: $optionC\n D: $optionD\n E: $optionE\n Before answering this question, let's think step by step."),
Template("Please provide a single-letter answer (A, B, C, D, E) to the multiple-choice question, and your answer must be one of the letters (A, B, C, D, or E). You must not provide any other response or explanation. Your response should only contain one letter.")
],
post_process_fn = first_char_as_answer
)
# egoschema QA plan-and-solve
prompt_templates['qa_plansolve'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template("You are given some language descriptions of a first person view video. The video is $duration seconds long. Each sentence describes a ${clip_length}s clip. Here are the descriptions: $narration.\n You are going to answer a multiple choice question based on the descriptions, and your answer should be a single letter chosen from the choices.\n Here is the question: $question.\n Here are the choices.\n A: $optionA\n B: $optionB\n C: $optionC\n D: $optionD\n E: $optionE\n To answer this question, let's first prepare relevant information and decompose it into 3 sub-questions. Then, let's answer the sub-questions one by one. Finally, let's answer the multiple choice question."),
Template("Please provide a single-letter answer (A, B, C, D, E) to the multiple-choice question, and your answer must be one of the letters (A, B, C, D, or E). You must not provide any other response or explanation. Your response should only contain one letter.")
],
post_process_fn = first_char_as_answer
)
# next-qa QA, intentQA QA
prompt_templates['qa_next'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template("Please provide a single-letter answer (A, B, C, D, E) to the following multiple-choice question, and your answer must be one of the letters (A, B, C, D, or E). You must not provide any other response or explanation. If you are not sure, answer with the most likely answer. You are given some language descriptions of a first person view video. The video is 1 FPS and the descriptions are the captions every 2 frames. Each caption starts with the frame number.\nHere are the descriptions:\n$narration\n Here is the question: $question?\n Here are the choices:\n (A): $optionA\n (B): $optionB\n (C): $optionC\n (D): $optionD\n (E): $optionE\n"),
],
post_process_fn = first_char_as_answer
)
# next-gqa GQA
prompt_templates['gqa'] = PromptTemplate(
head = "You are a helpful expert in first person view video analysis.",
template = [
Template("I will provide video descriptions and one question about the video. The video is 1 FPS and the descriptions are the captions every 2 frames. Each caption starts with the frame number.\n To answer this question, what is the minimun frame interval to check?\n Follow this format: [frame_start_index, frame_end_index]. Do not provide any explanation.\n Here are the descriptions:\n$narration\n Here is the question: $question?\n Please follow the output format as follows:\n #Example1: [5, 19]\n #Example2: [30, 60]\n #Example3: [1, 10] and [50, 60]"),
],
post_process_fn = get_intervals_as_list
)
# egoschema QA llama
B_INST, E_INST = "[INST]", "[/INST]"
B_SYS, E_SYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
anchor = 'The most correct answer is ('
prompt_templates['qa_standard_llama'] = PromptTemplate(
head = "",
template = [
Template(B_INST + B_SYS + "Please provide a single-letter answer (A, B, C, D, E) to the following multiple-choice question, and your answer must be one of the letters (A, B, C, D, or E). You must not provide any other response or explanation. You are given some language descriptions of a first person view video. The video is $duration seconds long. Each sentence describes a ${clip_length}s clip. The descriptions are sequential and non-overlapping which cover the whole video exactly." + E_SYS + 'Here are the descriptions:\n$narration\n Here is the question: $question.\n Here are the choices:\n (A): $optionA\n (B): $optionB\n (C): $optionC\n (D): $optionD\n (E): $optionE\n' + E_INST + anchor),
],
post_process_fn = first_char_after_anchor(anchor)
)
return prompt_templates
def get(self, prompt_type):
return self.prompt_templates[prompt_type]