Spaces:
Sleeping
Sleeping
File size: 18,524 Bytes
3a6a9b6 9af2839 a862f54 9af2839 e0d127e 3a6a9b6 6525dcf 3a6a9b6 9af2839 3a6a9b6 9af2839 6525dcf 3a6a9b6 6525dcf 3a6a9b6 6525dcf 9af2839 6525dcf 9af2839 6525dcf 3a6a9b6 6525dcf 31e89b7 3a6a9b6 9af2839 3a6a9b6 6525dcf 3a6a9b6 e136236 3a6a9b6 9af2839 57539e8 9af2839 3a6a9b6 b308128 6525dcf b308128 6525dcf b308128 6525dcf b308128 6525dcf b308128 6525dcf b308128 6525dcf b308128 6525dcf |
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
import os
import sys
import json
import time
import openai
import pickle
import argparse
import requests
from tqdm import tqdm
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaForCausalLM, LlamaTokenizer
from fastchat.model import load_model, get_conversation_template, add_model_args
from nltk.tag.mapping import _UNIVERSAL_TAGS
import gradio as gr
uni_tags = list(_UNIVERSAL_TAGS)
uni_tags[-1] = 'PUNC'
bio_tags = ['B', 'I', 'O']
chunk_tags = ['ADJP', 'ADVP', 'CONJP', 'INTJ', 'LST', 'NP', 'O', 'PP', 'PRT', 'SBAR', 'UCP', 'VP']
syntags = ['NP', 'S', 'VP', 'ADJP', 'ADVP', 'SBAR', 'TOP', 'PP', 'POS', 'NAC', "''", 'SINV', 'PRN', 'QP', 'WHNP', 'RB', 'FRAG',
'WHADVP', 'NX', 'PRT', 'VBZ', 'VBP', 'MD', 'NN', 'WHPP', 'SQ', 'SBARQ', 'LST', 'INTJ', 'X', 'UCP', 'CONJP', 'NNP', 'CD', 'JJ',
'VBD', 'WHADJP', 'PRP', 'RRC', 'NNS', 'SYM', 'CC']
openai.api_key = "OPENAI_API_KEY"
# determinant vs. determiner
# https://wikidiff.com/determiner/determinant
ents_prompt = ['Noun','Verb','Adjective','Adverb','Preposition/Subord','Coordinating Conjunction',# 'Cardinal Number',
'Determiner',
'Noun Phrase','Verb Phrase','Adjective Phrase','Adverb Phrase','Preposition Phrase','Conjunction Phrase','Coordinate Phrase','Quantitave Phrase','Complex Nominal',
'Clause','Dependent Clause','Fragment Clause','T-unit','Complex T-unit',# 'Fragment T-unit',
][7:]
ents = ['NN', 'VB', 'JJ', 'RB', 'IN', 'CC', 'DT', 'NP', 'VP', 'ADJP', 'ADVP', 'PP', 'CONJP', 'CP', 'QP', 'CN', 'C', 'DC', 'FC', 'T', 'CT'][7:]
ents_prompt_uni_tags = ['Verb', 'Noun', 'Pronoun', 'Adjective', 'Adverb', 'Preposition and Postposition', 'Coordinating Conjunction',
'Determiner', 'Cardinal Number', 'Particles or other function words',
'Words that cannot be assigned a POS tag', 'Punctuation']
ents = uni_tags + ents
ents_prompt = ents_prompt_uni_tags + ents_prompt
for i, j in zip(ents, ents_prompt):
print(i, j)
# raise
model_mapping = {
# 'gpt3': 'gpt-3',
'gpt3.5': 'gpt-3.5-turbo-0613',
'vicuna-7b': 'lmsys/vicuna-7b-v1.3',
'vicuna-13b': 'lmsys/vicuna-13b-v1.3',
'vicuna-33b': 'lmsys/vicuna-33b-v1.3',
'fastchat-t5': 'lmsys/fastchat-t5-3b-v1.0',
# 'llama2-7b': 'meta-llama/Llama-2-7b-hf',
# 'llama2-13b': 'meta-llama/Llama-2-13b-hf',
# 'llama2-70b': 'meta-llama/Llama-2-70b-hf',
'llama-7b': './llama/hf/7B',
'llama-13b': './llama/hf/13B',
'llama-30b': './llama/hf/30B',
# 'llama-65b': './llama/hf/65B',
'alpaca': './alpaca-7B',
# 'koala-7b': 'koala-7b',
# 'koala-13b': 'koala-13b',
}
for m in model_mapping.keys():
for eid, ent in enumerate(ents):
os.makedirs(f'result/prompt1_qa/{m}/ptb/per_ent/{ent}', exist_ok=True)
os.makedirs(f'result/prompt2_instruction/pos_tagging/{m}/ptb', exist_ok=True)
os.makedirs(f'result/prompt2_instruction/chunking/{m}/ptb', exist_ok=True)
os.makedirs(f'result/prompt2_instruction/parsing/{m}/ptb', exist_ok=True)
os.makedirs(f'result/prompt3_structured_prompt/pos_tagging/{m}/ptb', exist_ok=True)
os.makedirs(f'result/prompt3_structured_prompt/chunking/{m}/ptb', exist_ok=True)
os.makedirs(f'result/prompt3_structured_prompt/parsing/{m}/ptb', exist_ok=True)
#s = int(sys.argv[1])
#e = int(sys.argv[2])
#s = 0
#e = 1000
with open('sample_uniform_1k_2.txt', 'r') as f:
selected_idx = f.readlines()
selected_idx = [int(i.strip()) for i in selected_idx]#[s:e]
ptb = []
with open('ptb.jsonl', 'r') as f:
for l in f:
ptb.append(json.loads(l))
## Prompt 1
template_all = '''Please output the <Noun, Verb, Adjective, Adverb, Preposition/Subord, Coordinating Conjunction, Cardinal Number, Determiner, Noun Phrase, Verb Phrase, Adjective Phrase, Adverb Phrase, Preposition Phrase, Conjunction Phrase, Coordinate Phrase, Quantitave Phrase, Complex Nominal, Clause, Dependent Clause, Fragment Clause, T-unit, Complex T-unit, Fragment T-unit> in the following sentence without any additional text in json format: "{}"'''
template_single = '''Please output any <{}> in the following sentence one per line without any additional text: "{}"'''
## Prompt 2
prompt2_pos = '''Please pos tag the following sentence using Universal POS tag set without generating any additional text: {}'''
prompt2_chunk = '''Please do sentence chunking for the following sentence as in CoNLL 2000 shared task without generating any addtional text: {}'''
prompt2_parse = '''Generate textual representation of the constituency parse tree of the following sentence using Penn TreeBank tag set without outputing any additional text: {}'''
prompt2_chunk = '''Please chunk the following sentence in CoNLL 2000 format with BIO tags without outputing any additional text: {}'''
## Prompt 3
with open('demonstration_3_42_pos.txt', 'r') as f:
demon_pos = f.read()
with open('demonstration_3_42_chunk.txt', 'r') as f:
demon_chunk = f.read()
with open('demonstration_3_42_parse.txt', 'r') as f:
demon_parse = f.read()
def para(m):
c = 0
for n, p in m.named_parameters():
c += p.numel()
return c
def main(args=None):
gid_list = selected_idx[args.start:args.end]
if 'gpt3' in args.model_path:
pass
else:
path = model_mapping[args.model_path]
model, tokenizer = load_model(
path,
args.device,
args.num_gpus,
args.max_gpu_memory,
args.load_8bit,
args.cpu_offloading,
revision=args.revision,
debug=args.debug,
)
whitelist_ids_pos = [tokenizer.encode(word)[1] for word in uni_tags]
bad_words_ids_pos = [[ids] for ids in range(tokenizer.vocab_size) if ids not in whitelist_ids_pos]
whitelist_ids_bio = [tokenizer.encode(word)[1] for word in bio_tags]
bad_words_ids_bio = [[ids] for ids in range(tokenizer.vocab_size) if ids not in whitelist_ids_bio]
whitelist_ids_chunk = [tokenizer.encode(word)[1] for word in chunk_tags]
bad_words_ids_chunk = [[ids] for ids in range(tokenizer.vocab_size) if ids not in whitelist_ids_chunk]
whitelist_ids_parse = [tokenizer.encode(word)[1] for word in syntags]
bad_words_ids_parse = [[ids] for ids in range(tokenizer.vocab_size) if ids not in whitelist_ids_parse]
if args.prompt == 1:
for gid in tqdm(gid_list, desc='Query'):
text = ptb[gid]['text']
for eid, ent in enumerate(ents):
os.makedirs(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}', exist_ok=True)
if ent == 'NOUN' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/NOUN'):
os.system(f'ln -sT ./NN result/prompt1_qa/{args.model_path}/ptb/per_ent/NOUN')
if ent == 'VERB' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/VERB'):
os.system(f'ln -sT ./VB result/prompt1_qa/{args.model_path}/ptb/per_ent/VERB')
if ent == 'ADJ' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/ADJ'):
os.system(f'ln -sT ./JJ result/prompt1_qa/{args.model_path}/ptb/per_ent/ADJ')
if ent == 'ADV' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/ADV'):
os.system(f'ln -sT ./RB result/prompt1_qa/{args.model_path}/ptb/per_ent/ADV')
if ent == 'CONJ' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/CONJ'):
os.system(f'ln -sT ./CC result/prompt1_qa/{args.model_path}/ptb/per_ent/CONJ')
if ent == 'DET' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/DET'):
os.system(f'ln -sT ./DT result/prompt1_qa/{args.model_path}/ptb/per_ent/DET')
if ent == 'ADP' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/ADP'):
os.system(f'ln -sT ./DT result/prompt1_qa/{args.model_path}/ptb/per_ent/IN')
if os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}/{gid}.txt'):
print(gid, ent, 'skip')
continue
## Get prompt
msg = template_single.format(ents_prompt[eid], text)
## Run
if 'gpt3' in args.model_path:
if os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}/{gid}.pkl'):
print('Found cache')
with open(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}/{gid}.pkl', 'rb') as f:
outputs = pickle.load(f)
outputs = outputs['choices'][0]['message']['content']
else:
outputs = gpt3(msg)
if outputs is None:
continue
time.sleep(0.2)
else:
conv = get_conversation_template(args.model_path)
conv.append_message(conv.roles[0], msg)
conv.append_message(conv.roles[1], None)
conv.system = ''
prompt = conv.get_prompt().strip()
outputs = fastchat(prompt, model, tokenizer)
with open(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}/{gid}.txt', 'w') as f:
f.write(outputs)
if args.prompt == 2:
for gid in tqdm(gid_list, desc='Query'):
text = ptb[gid]['text']
## POS tagging
if os.path.exists(f'result/prompt2_instruction/pos_tagging/{args.model_path}/ptb/{gid}.txt'):
print(gid, 'skip')
else:
msg = prompt2_pos.format(text)
if 'gpt3' in args.model_path:
outputs = gpt3(msg)
if outputs is None:
continue
time.sleep(0.2)
else:
conv = get_conversation_template(args.model_path)
conv.append_message(conv.roles[0], msg)
conv.append_message(conv.roles[1], None)
conv.system = ''
prompt = conv.get_prompt()
outputs = fastchat(prompt, model, tokenizer)
with open(f'result/prompt2_instruction/pos_tagging/{args.model_path}/ptb/{gid}.txt', 'w') as f:
f.write(outputs)
## Sentence chunking
if os.path.exists(f'result/prompt2_instruction/chunking/{args.model_path}/ptb/{gid}.txt'):
print(gid, 'skip')
if False:
pass
else:
msg = prompt2_chunk.format(text)
if 'gpt3' in args.model_path:
outputs = gpt3(msg)
if outputs is None:
continue
time.sleep(0.2)
else:
conv = get_conversation_template(args.model_path)
conv.append_message(conv.roles[0], msg)
conv.append_message(conv.roles[1], None)
conv.system = ''
prompt = conv.get_prompt()
outputs = fastchat(prompt, model, tokenizer)
print(args.model_path, gid, outputs)
with open(f'result/prompt2_instruction/chunking/{args.model_path}/ptb/{gid}.txt', 'w') as f:
f.write(outputs)
## Parsing
if os.path.exists(f'result/prompt2_instruction/parsing/{args.model_path}/ptb/{gid}.txt'):
print(gid, 'skip')
else:
msg = prompt2_parse.format(text)
if 'gpt3' in args.model_path:
outputs = gpt3(msg)
if outputs is None:
continue
time.sleep(0.2)
else:
conv = get_conversation_template(args.model_path)
conv.append_message(conv.roles[0], msg)
conv.append_message(conv.roles[1], None)
conv.system = ''
prompt = conv.get_prompt()
outputs = fastchat(prompt, model, tokenizer)
with open(f'result/prompt2_instruction/parsing/{args.model_path}/ptb/{gid}.txt', 'w') as f:
f.write(outputs)
if args.prompt == 3:
for gid in tqdm(gid_list, desc='Query'):
text = ptb[gid]['text']
tokens = ptb[gid]['tokens']
poss = ptb[gid]['uni_poss']
## POS tagging
if os.path.exists(f'result/prompt3_structured_prompt/pos_tagging/{args.model_path}/ptb/{gid}.txt'):
print(gid, 'skip')
continue
prompt = demon_pos + '\n' + 'C: ' + text + '\n' + 'T: '
if 'gpt3' in args.model_path:
outputs = gpt3(prompt)
if outputs is None:
continue
time.sleep(0.2)
else:
pred_poss = []
for _tok, _pos in zip(tokens, poss):
prompt = prompt + ' ' + _tok + '_'
outputs = structured_prompt(prompt, model, tokenizer, bad_words_ids_pos)
prompt = prompt + outputs
pred_poss.append(outputs)
outputs = ' '.join(pred_poss)
with open(f'result/prompt3_structured_prompt/pos_tagging/{args.model_path}/ptb/{gid}.txt', 'w') as f:
f.write(outputs)
## Chunking
if os.path.exists(f'result/prompt3_structured_prompt/chunking/{args.model_path}/ptb/{gid}.txt'):
print(gid, 'skip')
continue
prompt = demon_chunk + '\n' + 'C: ' + text + '\n' + 'T: '
if 'gpt3' in args.model_path:
outputs = gpt3(prompt)
print(outputs)
if outputs is None:
continue
time.sleep(0.2)
else:
pred_chunk = []
for _tok, _pos in zip(tokens, poss):
prompt = prompt + ' ' + _tok + '_'
# Generate BIO
outputs_bio = structured_prompt(prompt, model, tokenizer, bad_words_ids_bio)
prompt = prompt + outputs_bio + '-'
# Generate tag
outputs_chunk = structured_prompt(prompt, model, tokenizer, bad_words_ids_chunk)
prompt = prompt + outputs_chunk
pred_chunk.append((outputs_bio + '-' + outputs_chunk))
outputs = ' '.join(pred_chunk)
with open(f'result/prompt3_structured_prompt/chunking/{args.model_path}/ptb/{gid}.txt', 'w') as f:
f.write(outputs)
## Parsing
if os.path.exists(f'result/prompt3_structured_prompt/parsing/{args.model_path}/ptb/{gid}.txt'):
print(gid, 'skip')
continue
prompt = demon_parse + '\n' + 'C: ' + text + '\n' + 'T: '
if 'gpt3' in args.model_path:
outputs = gpt3(prompt)
if outputs is None:
continue
time.sleep(0.2)
else:
pred_syn = []
for _tok, _pos in zip(tokens, poss):
prompt = prompt + _tok + '_'
outputs = structured_prompt(prompt, model, tokenizer, bad_words_ids_parse)
pred_syn.append(outputs)
with open(f'result/prompt3_structured_prompt/parsing/{args.model_path}/ptb/{gid}.txt', 'w') as f:
f.write(' '.join(pred_syn))
def structured_prompt(prompt, model, tokenizer, bad_words_ids):
input_ids = tokenizer([prompt]).input_ids
output_ids = model.generate(
torch.as_tensor(input_ids).cuda(),
max_new_tokens=1,
bad_words_ids=bad_words_ids,
)
if model.config.is_encoder_decoder:
output_ids = output_ids[0]
else:
output_ids = output_ids[0][len(input_ids[0]) :]
outputs = tokenizer.decode(
output_ids, skip_special_tokens=True, spaces_between_special_tokens=False
)
return outputs
def fastchat(prompt, model, tokenizer):
input_ids = tokenizer([prompt]).input_ids
output_ids = model.generate(
torch.as_tensor(input_ids).cuda(),
do_sample=True,
temperature=args.temperature,
repetition_penalty=args.repetition_penalty,
max_new_tokens=args.max_new_tokens,
)
if model.config.is_encoder_decoder:
output_ids = output_ids[0]
else:
output_ids = output_ids[0][len(input_ids[0]) :]
outputs = tokenizer.decode(
output_ids, skip_special_tokens=True, spaces_between_special_tokens=False
)
#print('Empty system message')
#print(f"{conv.roles[0]}: {msg}")
#print(f"{conv.roles[1]}: {outputs}")
return outputs
def gpt3(prompt):
try:
response = openai.ChatCompletion.create(
model=model_mapping[args.model_path], messages=[{"role": "user", "content": prompt}])
return response['choices'][0]['message']['content']
except Exception as err:
print('Error')
print(err)
return None
if __name__ == "__main__":
parser = argparse.ArgumentParser()
add_model_args(parser)
parser.add_argument("--temperature", type=float, default=0.7)
parser.add_argument("--repetition_penalty", type=float, default=1.0)
parser.add_argument("--max-new-tokens", type=int, default=512)
parser.add_argument("--debug", action="store_true")
parser.add_argument("--message", type=str, default="Hello! Who are you?")
parser.add_argument("--start", type=int, default=0)
parser.add_argument("--end", type=int, default=1000)
parser.add_argument("--prompt", required=True, type=int, default=None)
# parser.add_argument("--system_msg", required=True, type=str, default='default_system_msg')
args = parser.parse_args()
# Reset default repetition penalty for T5 models.
if "t5" in args.model_path and args.repetition_penalty == 1.0:
args.repetition_penalty = 1.2
main(args) |