-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
258 lines (231 loc) · 9.1 KB
/
generate.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import argparse
import json
import os
import re
from collections import OrderedDict
from time import perf_counter
import numpy as np
import torch
from iso639 import languages
from tqdm import tqdm
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from dataloading import CrossSumAggregated
from models.aux_models import NLLB, SONARTextEncoder
from models.summarizers import (
CrossLingualSum,
CrossLingualSumLLM,
CrossLingualSumReranker,
CrossLingualSumTrans,
)
WHITESPACE_HANDLER = lambda k: re.sub("\s+", " ", re.sub("\n+", " ", k.strip()))
def get_sentence_encoder_lang_name(language: str, language_list: list[str]) -> str: # type: ignore
if language.endswith("_latin"):
lang = languages.get(name=language[: -len("_latin")].capitalize()).part3
suffix = "_Latn"
return lang + suffix
elif language.endswith("_cyrillic"):
lang = languages.get(name=language[: -len("_cyrillic")].capitalize()).part3
suffix = "_Cyrl"
return lang + suffix
else:
try:
lang = languages.get(name=language.capitalize()).part3
for l in language_list: # type: ignore
if l.startswith(lang):
return l
except:
# temp fix for a few languages
if language == "persian":
return "pes_Arab"
if language == "chinese_simplified":
return "zho_Hans"
if language == "chinese_traditional":
return "zho_Hant"
if language == "kirundi":
return "run_Latn"
raise Exception(f"Invalid language '{language}'.")
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--source_lang", type=str, required=True)
parser.add_argument("--output", type=str, required=True)
parser.add_argument("--target_langs", type=str, nargs="+", default=None)
parser.add_argument(
"--data",
type=str,
default="./data",
)
parser.add_argument("--split", type=str, default="test")
parser.add_argument("--method", type=str, default="rerank")
parser.add_argument(
"--sentence_encoder", type=str, default="text_sonar_basic_encoder"
)
parser.add_argument("--mt_model", type=str, default="facebook/nllb-200-1.3B")
parser.add_argument("--llm", type=str, default="mistralai/Mistral-7B-Instruct-v0.2")
parser.add_argument(
"--llm_url",
type=str,
default="http://gpusrv04.interno.priberam.pt:1080/v1/chat/completions",
)
parser.add_argument("--llm_api_key_env", type=str, default=None)
parser.add_argument("--devices", type=str, nargs="+", default=["cuda:0", "cuda:1"])
parser.add_argument("--pivot_lang", type=str, default=None)
parser.add_argument("--num_candidates", type=int, default=8)
parser.add_argument("--num_beams", type=int, default=8)
parser.add_argument("--num_sampling_beams", type=int, default=4)
parser.add_argument("--temperature", type=float, default=1.0)
parser.add_argument("--top_k", type=int, default=50)
parser.add_argument("--pivot_gen_mode", type=str, default="beam_search")
parser.add_argument("--search_mode", type=str, default="dijkstra")
parser.add_argument("--num_permutations", type=int, default=6)
parser.add_argument("--num_examples", type=int, default=None)
parser.add_argument("--cluster_size", type=int, default=None)
args = parser.parse_args()
devices = [torch.device(d) for d in args.devices]
source_language_name = languages.get(alpha2=args.source_lang).name.lower()
if "chinese" in source_language_name:
source_language_name = "chinese_simplified"
target_language_names = (
[languages.get(alpha2=t).name.lower() for t in args.target_langs]
if args.target_langs is not None
else None
)
if target_language_names is not None:
target_language_names = [
"chinese_simplified" if "chinese" in l else l for l in target_language_names
]
pivot_language_name = (
languages.get(alpha2=args.pivot_lang).name.lower()
if args.pivot_lang is not None
else None
)
if pivot_language_name is not None and "chinese" in pivot_language_name:
pivot_language_name = "chinese_simplified"
dataset = CrossSumAggregated(
os.path.join(args.data, args.split),
source_language=source_language_name,
target_languages=target_language_names,
)
if args.method == "rerank":
summarization_model = (
AutoModelForSeq2SeqLM.from_pretrained(
"csebuetnlp/mT5_m2m_crossSum_enhanced"
)
.to(devices[0])
.eval()
)
summarization_tokenizer = AutoTokenizer.from_pretrained(
"csebuetnlp/mT5_m2m_crossSum_enhanced", use_fast=False
)
encoder = SONARTextEncoder(
encoder=args.sentence_encoder,
tokenizer=args.sentence_encoder,
device=devices[1],
).eval()
pipeline = CrossLingualSumReranker(
summarization_model, summarization_tokenizer, encoder, devices
)
summ_kwargs = {
"temperature": args.temperature,
"top_k": args.top_k,
"num_sampling_beams": args.num_sampling_beams,
"num_candidates": args.num_candidates,
}
if pivot_language_name is not None:
summ_kwargs["pivot_lang"] = pivot_language_name
summ_kwargs["pivot_gen_mode"] = args.pivot_gen_mode
if args.pivot_gen_mode == "beam_search":
summ_kwargs["num_beams"] = args.num_beams
else:
summ_kwargs["search_mode"] = args.search_mode
summ_kwargs["num_permutations"] = args.num_permutations
elif args.method == "translate":
summarization_model = (
AutoModelForSeq2SeqLM.from_pretrained(
"csebuetnlp/mT5_m2m_crossSum_enhanced"
)
.to(devices[0])
.eval()
)
summarization_tokenizer = AutoTokenizer.from_pretrained(
"csebuetnlp/mT5_m2m_crossSum_enhanced", use_fast=False
)
mt_model = NLLB(args.mt_model, device=devices[1])
pipeline = CrossLingualSumTrans(
summarization_model, summarization_tokenizer, mt_model, devices
)
summ_kwargs = {
"pivot_lang": pivot_language_name,
"num_beams": args.num_beams,
}
elif args.method == "llm":
api_key = (
os.getenv(args.llm_api_key_env)
if args.llm_api_key_env is not None
else None
)
pipeline = CrossLingualSumLLM(args.llm, url=args.llm_url, api_key=api_key)
summ_kwargs = {
"temperature": args.temperature,
}
else: # beam search
summarization_model = (
AutoModelForSeq2SeqLM.from_pretrained(
"csebuetnlp/mT5_m2m_crossSum_enhanced"
)
.to(devices[0])
.eval()
)
summarization_tokenizer = AutoTokenizer.from_pretrained(
"csebuetnlp/mT5_m2m_crossSum_enhanced", use_fast=False
)
pipeline = CrossLingualSum(
summarization_model, summarization_tokenizer, devices[0]
)
summ_kwargs = {
"num_beams": args.num_beams,
}
# get number of lines from args.output to resume from that point
try:
with open(args.output, "r") as fd:
num_lines = sum(1 for _ in fd)
except:
num_lines = 0
with open(args.output, "a") as fd:
for i, cluster in enumerate(tqdm(dataset)): # type: ignore
if i < num_lines:
continue
if args.num_examples is not None and i >= args.num_examples:
break
source_idx = cluster["source_index"]
source_text = cluster[f"text{source_idx}"]
if target_language_names is None:
target_language_names_i = [source_language_name] + [
cluster[key]
for key in cluster # type: ignore
if key.startswith("lang")
if cluster[key] != source_language_name
]
else:
target_language_names_i = target_language_names
if (
args.cluster_size is not None
and len(target_language_names_i) != args.cluster_size
):
continue
start_time = perf_counter()
summaries = pipeline.summarize(
text=source_text,
source_lang=source_language_name,
target_langs=target_language_names_i,
**summ_kwargs,
)
torch.cuda.synchronize(device=devices[0])
torch.cuda.synchronize(device=devices[1])
end_time = perf_counter()
r = {f"summary_{l}": summaries[l] for l in summaries}
r["source_url"] = cluster[f"url{source_idx}"]
r["time_per_summary"] = str((end_time - start_time) / len(summaries))
fd.write(json.dumps(r))
fd.write("\n")
if __name__ == "__main__":
main()