Spaces:
Runtime error
Runtime error
<ADD> update app
Browse files- .gitignore +6 -0
- app.py +55 -12
- example_sets/sst2/sample.pkl +14 -10
.gitignore
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.vscode
|
2 |
+
|
3 |
+
# Byte-compiled / optimized / DLL files
|
4 |
+
__pycache__/
|
5 |
+
*.py[cod]
|
6 |
+
*$py.class
|
app.py
CHANGED
@@ -15,7 +15,6 @@ from tasks.loader import TokenizedForMCRightPad
|
|
15 |
|
16 |
DISPLAY_MAPPING = {
|
17 |
"sst2": {"positive": "Pos", "negative": "Neg"},
|
18 |
-
"trec": {},
|
19 |
}
|
20 |
|
21 |
|
@@ -78,13 +77,21 @@ def process_once(dataset_name, exemplar_str, forward_steps, raw_data):
|
|
78 |
generated_info.extend(zipped_logprobs)
|
79 |
|
80 |
all_predicted = []
|
|
|
81 |
for idx, (data, choice_info) in enumerate(zip(processed_data, generated_info)):
|
82 |
merged_choice_info = task_agent.merge_choice_info(choice_info)
|
83 |
merged_predictions_idx = task_agent.choice_info_to_predictions(merged_choice_info)["lm_log_p"]
|
84 |
predicted = task_agent.CHOICES[merged_predictions_idx]
|
85 |
ground_truth = task_agent.CHOICES[data["answer_idx"]]
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
all_predicted.append(res)
|
|
|
88 |
return all_predicted
|
89 |
|
90 |
|
@@ -102,7 +109,10 @@ def button_pressed(prev_state):
|
|
102 |
current_output = process_once(dataset_name, exemplar_str, forward_steps, raw_data)
|
103 |
|
104 |
t_prev = transpose(prev_table_data)
|
105 |
-
|
|
|
|
|
|
|
106 |
updated_table_data = transpose(t_prev)
|
107 |
|
108 |
ret = [
|
@@ -113,7 +123,7 @@ def button_pressed(prev_state):
|
|
113 |
"step": forward_steps,
|
114 |
"table_data": updated_table_data,
|
115 |
},
|
116 |
-
f"
|
117 |
updated_table_data,
|
118 |
]
|
119 |
return ret
|
@@ -138,37 +148,70 @@ if __name__ == "__main__":
|
|
138 |
with task_root.joinpath("demos.txt").open("r") as f:
|
139 |
demos = f.read()
|
140 |
with task_root.joinpath("sample.pkl").open("r") as f:
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
|
146 |
title = "π€ Iterative Forward Tuning Boosts In-context Learning in Language Models"
|
147 |
demo = gr.Blocks(css=css, title="π€Deep-Thinking")
|
148 |
with demo:
|
149 |
gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{title}</h1>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
with gr.Tab("SST-2"):
|
151 |
mapping = ["negative", "positive"]
|
152 |
|
153 |
-
init_columns = [[e["sentence"]
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
state = gr.State(
|
155 |
{
|
156 |
"dataset_name": "sst2",
|
157 |
"exemplar_str": demos,
|
158 |
"raw_data": raw_data,
|
159 |
-
"step":
|
160 |
-
"table_data":
|
161 |
}
|
162 |
)
|
163 |
|
164 |
prompt = gr.Textbox(label="Demonstrations (Prompt template formatted)", value=demos)
|
|
|
|
|
165 |
big_table = gr.DataFrame(
|
166 |
-
value=
|
167 |
elem_id="the-table",
|
168 |
datatype=["markdown"] * 50,
|
169 |
headers=None,
|
170 |
)
|
171 |
-
step_button = gr.Button("Step + 2, Now: 0")
|
172 |
step_button.click(button_pressed, inputs=[state], outputs=[state, step_button, big_table])
|
173 |
|
174 |
demo.launch(server_name="0.0.0.0")
|
|
|
15 |
|
16 |
DISPLAY_MAPPING = {
|
17 |
"sst2": {"positive": "Pos", "negative": "Neg"},
|
|
|
18 |
}
|
19 |
|
20 |
|
|
|
77 |
generated_info.extend(zipped_logprobs)
|
78 |
|
79 |
all_predicted = []
|
80 |
+
num_correct = 0
|
81 |
for idx, (data, choice_info) in enumerate(zip(processed_data, generated_info)):
|
82 |
merged_choice_info = task_agent.merge_choice_info(choice_info)
|
83 |
merged_predictions_idx = task_agent.choice_info_to_predictions(merged_choice_info)["lm_log_p"]
|
84 |
predicted = task_agent.CHOICES[merged_predictions_idx]
|
85 |
ground_truth = task_agent.CHOICES[data["answer_idx"]]
|
86 |
+
|
87 |
+
res = f"{DISPLAY_MAPPING[dataset_name][predicted]}"
|
88 |
+
if predicted == ground_truth:
|
89 |
+
res += " β
"
|
90 |
+
num_correct += 1
|
91 |
+
else:
|
92 |
+
res += " β"
|
93 |
all_predicted.append(res)
|
94 |
+
all_predicted.append(f"{100*num_correct / len(all_predicted):.2f}%")
|
95 |
return all_predicted
|
96 |
|
97 |
|
|
|
109 |
current_output = process_once(dataset_name, exemplar_str, forward_steps, raw_data)
|
110 |
|
111 |
t_prev = transpose(prev_table_data)
|
112 |
+
if forward_steps == 1:
|
113 |
+
t_prev.append(["**ICL**"] + current_output)
|
114 |
+
else:
|
115 |
+
t_prev.append([f"**Step={forward_steps}**"] + current_output)
|
116 |
updated_table_data = transpose(t_prev)
|
117 |
|
118 |
ret = [
|
|
|
123 |
"step": forward_steps,
|
124 |
"table_data": updated_table_data,
|
125 |
},
|
126 |
+
f"Click here to train LLM ! Now Step: {forward_steps}",
|
127 |
updated_table_data,
|
128 |
]
|
129 |
return ret
|
|
|
148 |
with task_root.joinpath("demos.txt").open("r") as f:
|
149 |
demos = f.read()
|
150 |
with task_root.joinpath("sample.pkl").open("r") as f:
|
151 |
+
raw_data = json.load(f)
|
152 |
+
|
153 |
+
icl_result = process_once(dataset_name, demos, 1, raw_data)
|
154 |
+
|
155 |
+
text = """We utilize a Large Language Model (LLM) to perform in-context learning (ICL) for sentiment classification of movie reviews.
|
156 |
+
|
157 |
+
Taking the following two labeled examples as demonstrations, we predict the sentiment of the subsequent test input.
|
158 |
|
159 |
+
Directly employing ICL results in lower prediction accuracy. However, in our proposed approach, **Deep-Thinking**, we repeatedly apply **Forward Tuning**, leading to improved accuracy of the model."""
|
160 |
+
|
161 |
+
css = """
|
162 |
+
#the-table { overflow: auto; }
|
163 |
+
#the-table > div:nth-child(2) { margin: auto; width: fit-content; }
|
164 |
+
#the-table > div > div > div > table { width: auto; margin: 0; white-space: normal; }
|
165 |
+
#the-table > div > div > div > table > thead {display: none}
|
166 |
+
#the-table > div > div > div > table > tbody > tr:last-child {background-color: beige}
|
167 |
+
#the-table > div > div > div > table > tbody > tr:first-child {background-color: lightgray}
|
168 |
+
#the-table > div > div > div > table > tbody > tr > td:first-child {min-width: 300px;}
|
169 |
+
#the-table > div > div > div > table > tbody > tr > td:not(:first-child) {white-space: nowrap; padding: 0 2px; }
|
170 |
+
#the-text { font-size: large; }
|
171 |
+
"""
|
172 |
|
173 |
title = "π€ Iterative Forward Tuning Boosts In-context Learning in Language Models"
|
174 |
demo = gr.Blocks(css=css, title="π€Deep-Thinking")
|
175 |
with demo:
|
176 |
gr.Markdown(f"<h1 style='text-align: center; margin-bottom: 1rem'>{title}</h1>")
|
177 |
+
gr.Markdown(
|
178 |
+
"""
|
179 |
+
<h2 style='text-align: center; margin-bottom: 1rem'>
|
180 |
+
<a href='https://arxiv.org/abs/2305.13016' target="_blank" style='text-decoration: none'>[Paper]</a>
|
181 |
+
<a href='https://arxiv.org/abs/2305.13016' target="_blank" style='text-decoration: none'>[Code]</a>
|
182 |
+
</h2>"""
|
183 |
+
)
|
184 |
+
|
185 |
+
gr.Markdown(text, elem_id="the-text")
|
186 |
with gr.Tab("SST-2"):
|
187 |
mapping = ["negative", "positive"]
|
188 |
|
189 |
+
init_columns = [[e["sentence"]] for e in raw_data]
|
190 |
+
|
191 |
+
init_table_result = [["**Test Input**"], *init_columns, ["**Accuracy**"]]
|
192 |
+
init_table_result = transpose(init_table_result)
|
193 |
+
init_table_result.append(["**ICL**"] + icl_result)
|
194 |
+
init_table_result = transpose(init_table_result)
|
195 |
+
|
196 |
state = gr.State(
|
197 |
{
|
198 |
"dataset_name": "sst2",
|
199 |
"exemplar_str": demos,
|
200 |
"raw_data": raw_data,
|
201 |
+
"step": 1,
|
202 |
+
"table_data": init_table_result,
|
203 |
}
|
204 |
)
|
205 |
|
206 |
prompt = gr.Textbox(label="Demonstrations (Prompt template formatted)", value=demos)
|
207 |
+
gr.Markdown("<h2 style='text-align: center; margin-bottom: 1rem'>π Run forward tuning once !</h2>")
|
208 |
+
step_button = gr.Button("Click here to train LLM ! Now Step: 1")
|
209 |
big_table = gr.DataFrame(
|
210 |
+
value=init_table_result,
|
211 |
elem_id="the-table",
|
212 |
datatype=["markdown"] * 50,
|
213 |
headers=None,
|
214 |
)
|
|
|
215 |
step_button.click(button_pressed, inputs=[state], outputs=[state, step_button, big_table])
|
216 |
|
217 |
demo.launch(server_name="0.0.0.0")
|
example_sets/sst2/sample.pkl
CHANGED
@@ -1,10 +1,14 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
}
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{"sentence":"... the movie is just a plain old monster . ","label":0,"idx":18},
|
3 |
+
{"sentence": "overall very good for what it 's trying to do . ", "label": 1, "idx": 150},
|
4 |
+
{"sentence": "it has all the excitement of eating oatmeal . ", "label": 0, "idx": 527},
|
5 |
+
{"sentence": "and when you 're talking about a slapstick comedy , that 's a pretty big problem . ", "label": 0, "idx": 748},
|
6 |
+
{"sentence": "and that 's a big part of why we go to the movies . ", "label": 1, "idx": 505},
|
7 |
+
{"sentence": "a good piece of work more often than not . ", "label": 1, "idx": 424},
|
8 |
+
{"sentence": "the cold turkey would 've been a far better title . ", "label": 0, "idx": 57},
|
9 |
+
{"sentence": "it 's slow -- very , very slow . ", "label": 0, "idx": 4},
|
10 |
+
{"sentence": "it 's a cookie-cutter movie , a cut-and-paste job . ", "label": 0, "idx": 28},
|
11 |
+
{"sentence": "i am sorry that i was unable to get the full brunt of the comedy . ", "label": 0, "idx": 423},
|
12 |
+
{"sentence": "filmmakers who can deftly change moods are treasures and even marvels . ", "label": 1, "idx": 679},
|
13 |
+
{"sentence": "a solid film ... but more conscientious than it is truly stirring . ", "label": 1, "idx": 143}
|
14 |
+
]
|