Spaces:
Runtime error
Runtime error
Commit
·
ac4f141
1
Parent(s):
1d66b8b
test new interface
Browse files- app.py +71 -43
- run_llm2.py +58 -379
app.py
CHANGED
|
@@ -17,6 +17,8 @@ from nltk.tag.mapping import _UNIVERSAL_TAGS
|
|
| 17 |
import gradio as gr
|
| 18 |
from transformers import pipeline
|
| 19 |
|
|
|
|
|
|
|
| 20 |
uni_tags = list(_UNIVERSAL_TAGS)
|
| 21 |
uni_tags[-1] = 'PUNC'
|
| 22 |
|
|
@@ -98,50 +100,76 @@ task_options = ['POS', 'Chunking'] # remove parsing
|
|
| 98 |
|
| 99 |
|
| 100 |
# Function to process text based on model and task
|
| 101 |
-
def process_text(
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
result1 = gpt_pipeline(strategy1_format)[0]['generated_text']
|
| 123 |
-
result2 = gpt_pipeline(strategy2_format)[0]['generated_text']
|
| 124 |
-
result3 = gpt_pipeline(strategy3_format)[0]['generated_text']
|
| 125 |
-
return (result1, result2, result3)
|
| 126 |
|
| 127 |
# Gradio interface
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
gr.
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
)
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
|
|
|
| 147 |
|
|
|
|
| 17 |
import gradio as gr
|
| 18 |
from transformers import pipeline
|
| 19 |
|
| 20 |
+
demo = gr.Blocks()
|
| 21 |
+
|
| 22 |
uni_tags = list(_UNIVERSAL_TAGS)
|
| 23 |
uni_tags[-1] = 'PUNC'
|
| 24 |
|
|
|
|
| 100 |
|
| 101 |
|
| 102 |
# Function to process text based on model and task
|
| 103 |
+
def process_text(tab, text):
|
| 104 |
+
if tab == 'POS Tab':
|
| 105 |
+
strategy1_format = template_all.format(text)
|
| 106 |
+
strategy2_format = prompt2_pos.format(text)
|
| 107 |
+
strategy3_format = demon_pos
|
| 108 |
+
|
| 109 |
+
vicuna_result1 = gpt_pipeline(strategy1_format)[0]['generated_text']
|
| 110 |
+
vicuna_result2 = gpt_pipeline(strategy2_format)[0]['generated_text']
|
| 111 |
+
vicuna_result3 = gpt_pipeline(strategy3_format)[0]['generated_text']
|
| 112 |
+
|
| 113 |
+
return (vicuna_result1, vicuna_result2, vicuna_result3)
|
| 114 |
+
elif tab == 'Chunk Tab':
|
| 115 |
+
strategy1_format = template_all.format(text)
|
| 116 |
+
strategy2_format = prompt2_chunk.format(text)
|
| 117 |
+
strategy3_format = demon_chunk
|
| 118 |
+
|
| 119 |
+
result1 = gpt_pipeline(strategy1_format)[0]['generated_text']
|
| 120 |
+
result2 = gpt_pipeline(strategy2_format)[0]['generated_text']
|
| 121 |
+
result3 = gpt_pipeline(strategy3_format)[0]['generated_text']
|
| 122 |
+
return (result1, result2, result3)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
# Gradio interface
|
| 125 |
+
with demo:
|
| 126 |
+
gr.Markdown("# LLM Evaluator With Linguistic Scrutiny")
|
| 127 |
+
|
| 128 |
+
with gr.Tabs():
|
| 129 |
+
with gr.TabItem("POS", id="POS Tab"):
|
| 130 |
+
with gr.Row():
|
| 131 |
+
gr.Markdown("<center>Vicuna 7b</center>")
|
| 132 |
+
gr.Markdown("<center> LLaMA-7b </center>")
|
| 133 |
+
gr.Markdown("<center> GPT 3.5 </center>")
|
| 134 |
+
with gr.Row():
|
| 135 |
+
model1_S1_output = gr.Textbox(label="Strategy 1 QA")
|
| 136 |
+
model2_S1_output = gr.Textbox(label=".")
|
| 137 |
+
model3_S1_output = gr.Textbox(label=".")
|
| 138 |
+
with gr.Row():
|
| 139 |
+
model1_S2_output = gr.Textbox(label="Strategy 2 Instruction")
|
| 140 |
+
model2_S2_output = gr.Textbox(label=".")
|
| 141 |
+
model3_S2_output = gr.Textbox(label=".")
|
| 142 |
+
with gr.Row():
|
| 143 |
+
model1_S3_output = gr.Textbox(label="Strategy 3 Structured Prompting")
|
| 144 |
+
model2_S3_output = gr.Textbox(label=".")
|
| 145 |
+
model3_S3_output = gr.Textbox(label=".")
|
| 146 |
+
with gr.Row():
|
| 147 |
+
prompt = gr.Textbox(show_label=False, placeholder="Enter prompt")
|
| 148 |
+
send_button_POS = gr.Button("Send", scale=0)
|
| 149 |
+
|
| 150 |
+
with gr.TabItem("Chunking", id="Chunk Tab"):
|
| 151 |
+
with gr.Row():
|
| 152 |
+
gr.Markdown("<center>Vicuna 7b</center>")
|
| 153 |
+
gr.Markdown("<center> LLaMA-7b </center>")
|
| 154 |
+
gr.Markdown("<center> GPT 3.5 </center>")
|
| 155 |
+
with gr.Row():
|
| 156 |
+
model1_S1_output = gr.Textbox(label="Strategy 1 QA")
|
| 157 |
+
model2_S1_output = gr.Textbox(label=".")
|
| 158 |
+
model3_S1_output = gr.Textbox(label=".")
|
| 159 |
+
with gr.Row():
|
| 160 |
+
model1_S2_output = gr.Textbox(label="Strategy 2 Instruction")
|
| 161 |
+
model2_S2_output = gr.Textbox(label=".")
|
| 162 |
+
model3_S2_output = gr.Textbox(label=".")
|
| 163 |
+
with gr.Row():
|
| 164 |
+
model1_S3_output = gr.Textbox(label="Strategy 3 Structured Prompting")
|
| 165 |
+
model2_S3_output = gr.Textbox(label=".")
|
| 166 |
+
model3_S3_output = gr.Textbox(label=".")
|
| 167 |
+
with gr.Row():
|
| 168 |
+
prompt = gr.Textbox(show_label=False, placeholder="Enter prompt")
|
| 169 |
+
send_button_Chunk = gr.Button("Send", scale=0)
|
| 170 |
+
|
| 171 |
+
send_button_POS.click(process_text, inputs=["POS Tab", prompt], outputs=[model1_S1_output, model1_S1_output, model1_S1_output])
|
| 172 |
+
send_button_Chunk.click(process_text, inputs=["Chunk Tab", prompt], outputs=[model1_S1_output, model1_S1_output, model1_S1_output])
|
| 173 |
|
| 174 |
+
demo.launch()
|
| 175 |
|
run_llm2.py
CHANGED
|
@@ -15,6 +15,7 @@ from fastchat.model import load_model, get_conversation_template, add_model_args
|
|
| 15 |
from nltk.tag.mapping import _UNIVERSAL_TAGS
|
| 16 |
|
| 17 |
import gradio as gr
|
|
|
|
| 18 |
|
| 19 |
uni_tags = list(_UNIVERSAL_TAGS)
|
| 20 |
uni_tags[-1] = 'PUNC'
|
|
@@ -26,8 +27,7 @@ syntags = ['NP', 'S', 'VP', 'ADJP', 'ADVP', 'SBAR', 'TOP', 'PP', 'POS', 'NAC', "
|
|
| 26 |
'WHADVP', 'NX', 'PRT', 'VBZ', 'VBP', 'MD', 'NN', 'WHPP', 'SQ', 'SBARQ', 'LST', 'INTJ', 'X', 'UCP', 'CONJP', 'NNP', 'CD', 'JJ',
|
| 27 |
'VBD', 'WHADJP', 'PRP', 'RRC', 'NNS', 'SYM', 'CC']
|
| 28 |
|
| 29 |
-
openai.api_key = "
|
| 30 |
-
|
| 31 |
|
| 32 |
# determinant vs. determiner
|
| 33 |
# https://wikidiff.com/determiner/determinant
|
|
@@ -48,51 +48,17 @@ ents_prompt = ents_prompt_uni_tags + ents_prompt
|
|
| 48 |
|
| 49 |
for i, j in zip(ents, ents_prompt):
|
| 50 |
print(i, j)
|
| 51 |
-
# raise
|
| 52 |
-
|
| 53 |
|
| 54 |
model_mapping = {
|
| 55 |
-
|
| 56 |
-
'
|
| 57 |
-
'
|
| 58 |
-
'vicuna-13b': 'lmsys/vicuna-13b-v1.3',
|
| 59 |
-
'vicuna-33b': 'lmsys/vicuna-33b-v1.3',
|
| 60 |
-
'fastchat-t5': 'lmsys/fastchat-t5-3b-v1.0',
|
| 61 |
-
# 'llama2-7b': 'meta-llama/Llama-2-7b-hf',
|
| 62 |
-
# 'llama2-13b': 'meta-llama/Llama-2-13b-hf',
|
| 63 |
-
# 'llama2-70b': 'meta-llama/Llama-2-70b-hf',
|
| 64 |
-
'llama-7b': './llama/hf/7B',
|
| 65 |
-
'llama-13b': './llama/hf/13B',
|
| 66 |
-
'llama-30b': './llama/hf/30B',
|
| 67 |
-
# 'llama-65b': './llama/hf/65B',
|
| 68 |
-
'alpaca': './alpaca-7B',
|
| 69 |
-
# 'koala-7b': 'koala-7b',
|
| 70 |
-
# 'koala-13b': 'koala-13b',
|
| 71 |
}
|
| 72 |
|
| 73 |
-
for m in model_mapping.keys():
|
| 74 |
-
for eid, ent in enumerate(ents):
|
| 75 |
-
os.makedirs(f'result/prompt1_qa/{m}/ptb/per_ent/{ent}', exist_ok=True)
|
| 76 |
-
|
| 77 |
-
os.makedirs(f'result/prompt2_instruction/pos_tagging/{m}/ptb', exist_ok=True)
|
| 78 |
-
os.makedirs(f'result/prompt2_instruction/chunking/{m}/ptb', exist_ok=True)
|
| 79 |
-
os.makedirs(f'result/prompt2_instruction/parsing/{m}/ptb', exist_ok=True)
|
| 80 |
-
|
| 81 |
-
os.makedirs(f'result/prompt3_structured_prompt/pos_tagging/{m}/ptb', exist_ok=True)
|
| 82 |
-
os.makedirs(f'result/prompt3_structured_prompt/chunking/{m}/ptb', exist_ok=True)
|
| 83 |
-
os.makedirs(f'result/prompt3_structured_prompt/parsing/{m}/ptb', exist_ok=True)
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
#s = int(sys.argv[1])
|
| 87 |
-
#e = int(sys.argv[2])
|
| 88 |
-
|
| 89 |
-
#s = 0
|
| 90 |
-
#e = 1000
|
| 91 |
with open('sample_uniform_1k_2.txt', 'r') as f:
|
| 92 |
selected_idx = f.readlines()
|
| 93 |
selected_idx = [int(i.strip()) for i in selected_idx]#[s:e]
|
| 94 |
|
| 95 |
-
|
| 96 |
ptb = []
|
| 97 |
with open('ptb.jsonl', 'r') as f:
|
| 98 |
for l in f:
|
|
@@ -118,351 +84,64 @@ with open('demonstration_3_42_chunk.txt', 'r') as f:
|
|
| 118 |
with open('demonstration_3_42_parse.txt', 'r') as f:
|
| 119 |
demon_parse = f.read()
|
| 120 |
|
|
|
|
|
|
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
return c
|
| 127 |
-
|
| 128 |
-
def main(args=None):
|
| 129 |
|
| 130 |
-
|
|
|
|
|
|
|
| 131 |
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
-
|
| 137 |
-
path = model_mapping[args.model_path]
|
| 138 |
-
model, tokenizer = load_model(
|
| 139 |
-
path,
|
| 140 |
-
args.device,
|
| 141 |
-
args.num_gpus,
|
| 142 |
-
args.max_gpu_memory,
|
| 143 |
-
args.load_8bit,
|
| 144 |
-
args.cpu_offloading,
|
| 145 |
-
revision=args.revision,
|
| 146 |
-
debug=args.debug,
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
whitelist_ids_pos = [tokenizer.encode(word)[1] for word in uni_tags]
|
| 150 |
-
bad_words_ids_pos = [[ids] for ids in range(tokenizer.vocab_size) if ids not in whitelist_ids_pos]
|
| 151 |
-
|
| 152 |
-
whitelist_ids_bio = [tokenizer.encode(word)[1] for word in bio_tags]
|
| 153 |
-
bad_words_ids_bio = [[ids] for ids in range(tokenizer.vocab_size) if ids not in whitelist_ids_bio]
|
| 154 |
-
|
| 155 |
-
whitelist_ids_chunk = [tokenizer.encode(word)[1] for word in chunk_tags]
|
| 156 |
-
bad_words_ids_chunk = [[ids] for ids in range(tokenizer.vocab_size) if ids not in whitelist_ids_chunk]
|
| 157 |
-
|
| 158 |
-
whitelist_ids_parse = [tokenizer.encode(word)[1] for word in syntags]
|
| 159 |
-
bad_words_ids_parse = [[ids] for ids in range(tokenizer.vocab_size) if ids not in whitelist_ids_parse]
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
if args.prompt == 1:
|
| 163 |
-
for gid in tqdm(gid_list, desc='Query'):
|
| 164 |
text = ptb[gid]['text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
for eid, ent in enumerate(ents):
|
| 167 |
-
os.makedirs(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}', exist_ok=True)
|
| 168 |
-
|
| 169 |
-
if ent == 'NOUN' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/NOUN'):
|
| 170 |
-
os.system(f'ln -sT ./NN result/prompt1_qa/{args.model_path}/ptb/per_ent/NOUN')
|
| 171 |
-
if ent == 'VERB' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/VERB'):
|
| 172 |
-
os.system(f'ln -sT ./VB result/prompt1_qa/{args.model_path}/ptb/per_ent/VERB')
|
| 173 |
-
if ent == 'ADJ' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/ADJ'):
|
| 174 |
-
os.system(f'ln -sT ./JJ result/prompt1_qa/{args.model_path}/ptb/per_ent/ADJ')
|
| 175 |
-
if ent == 'ADV' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/ADV'):
|
| 176 |
-
os.system(f'ln -sT ./RB result/prompt1_qa/{args.model_path}/ptb/per_ent/ADV')
|
| 177 |
-
if ent == 'CONJ' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/CONJ'):
|
| 178 |
-
os.system(f'ln -sT ./CC result/prompt1_qa/{args.model_path}/ptb/per_ent/CONJ')
|
| 179 |
-
if ent == 'DET' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/DET'):
|
| 180 |
-
os.system(f'ln -sT ./DT result/prompt1_qa/{args.model_path}/ptb/per_ent/DET')
|
| 181 |
-
if ent == 'ADP' and not os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/ADP'):
|
| 182 |
-
os.system(f'ln -sT ./DT result/prompt1_qa/{args.model_path}/ptb/per_ent/IN')
|
| 183 |
-
|
| 184 |
-
if os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}/{gid}.txt'):
|
| 185 |
-
print(gid, ent, 'skip')
|
| 186 |
-
continue
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
## Get prompt
|
| 190 |
-
msg = template_single.format(ents_prompt[eid], text)
|
| 191 |
-
|
| 192 |
-
## Run
|
| 193 |
-
if 'gpt3' in args.model_path:
|
| 194 |
-
if os.path.exists(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}/{gid}.pkl'):
|
| 195 |
-
print('Found cache')
|
| 196 |
-
with open(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}/{gid}.pkl', 'rb') as f:
|
| 197 |
-
outputs = pickle.load(f)
|
| 198 |
-
outputs = outputs['choices'][0]['message']['content']
|
| 199 |
-
else:
|
| 200 |
-
outputs = gpt3(msg)
|
| 201 |
-
if outputs is None:
|
| 202 |
-
continue
|
| 203 |
-
time.sleep(0.2)
|
| 204 |
-
|
| 205 |
-
else:
|
| 206 |
-
conv = get_conversation_template(args.model_path)
|
| 207 |
-
conv.append_message(conv.roles[0], msg)
|
| 208 |
-
conv.append_message(conv.roles[1], None)
|
| 209 |
-
conv.system = ''
|
| 210 |
-
prompt = conv.get_prompt().strip()
|
| 211 |
-
outputs = fastchat(prompt, model, tokenizer)
|
| 212 |
-
|
| 213 |
-
with open(f'result/prompt1_qa/{args.model_path}/ptb/per_ent/{ent}/{gid}.txt', 'w') as f:
|
| 214 |
-
f.write(outputs)
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
if args.prompt == 2:
|
| 218 |
-
for gid in tqdm(gid_list, desc='Query'):
|
| 219 |
-
text = ptb[gid]['text']
|
| 220 |
|
| 221 |
-
## POS tagging
|
| 222 |
-
if os.path.exists(f'result/prompt2_instruction/pos_tagging/{args.model_path}/ptb/{gid}.txt'):
|
| 223 |
-
print(gid, 'skip')
|
| 224 |
-
|
| 225 |
-
else:
|
| 226 |
-
msg = prompt2_pos.format(text)
|
| 227 |
-
|
| 228 |
-
if 'gpt3' in args.model_path:
|
| 229 |
-
outputs = gpt3(msg)
|
| 230 |
-
if outputs is None:
|
| 231 |
-
continue
|
| 232 |
-
time.sleep(0.2)
|
| 233 |
-
|
| 234 |
-
else:
|
| 235 |
-
conv = get_conversation_template(args.model_path)
|
| 236 |
-
conv.append_message(conv.roles[0], msg)
|
| 237 |
-
conv.append_message(conv.roles[1], None)
|
| 238 |
-
conv.system = ''
|
| 239 |
-
prompt = conv.get_prompt()
|
| 240 |
-
|
| 241 |
-
outputs = fastchat(prompt, model, tokenizer)
|
| 242 |
-
|
| 243 |
-
with open(f'result/prompt2_instruction/pos_tagging/{args.model_path}/ptb/{gid}.txt', 'w') as f:
|
| 244 |
-
f.write(outputs)
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
## Sentence chunking
|
| 248 |
-
if os.path.exists(f'result/prompt2_instruction/chunking/{args.model_path}/ptb/{gid}.txt'):
|
| 249 |
-
print(gid, 'skip')
|
| 250 |
-
if False:
|
| 251 |
-
pass
|
| 252 |
-
else:
|
| 253 |
-
msg = prompt2_chunk.format(text)
|
| 254 |
-
|
| 255 |
-
if 'gpt3' in args.model_path:
|
| 256 |
-
outputs = gpt3(msg)
|
| 257 |
-
if outputs is None:
|
| 258 |
-
continue
|
| 259 |
-
time.sleep(0.2)
|
| 260 |
-
|
| 261 |
-
else:
|
| 262 |
-
conv = get_conversation_template(args.model_path)
|
| 263 |
-
conv.append_message(conv.roles[0], msg)
|
| 264 |
-
conv.append_message(conv.roles[1], None)
|
| 265 |
-
conv.system = ''
|
| 266 |
-
prompt = conv.get_prompt()
|
| 267 |
-
|
| 268 |
-
outputs = fastchat(prompt, model, tokenizer)
|
| 269 |
-
|
| 270 |
-
print(args.model_path, gid, outputs)
|
| 271 |
-
with open(f'result/prompt2_instruction/chunking/{args.model_path}/ptb/{gid}.txt', 'w') as f:
|
| 272 |
-
f.write(outputs)
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
## Parsing
|
| 276 |
-
if os.path.exists(f'result/prompt2_instruction/parsing/{args.model_path}/ptb/{gid}.txt'):
|
| 277 |
-
print(gid, 'skip')
|
| 278 |
-
|
| 279 |
-
else:
|
| 280 |
-
msg = prompt2_parse.format(text)
|
| 281 |
-
|
| 282 |
-
if 'gpt3' in args.model_path:
|
| 283 |
-
outputs = gpt3(msg)
|
| 284 |
-
if outputs is None:
|
| 285 |
-
continue
|
| 286 |
-
time.sleep(0.2)
|
| 287 |
-
|
| 288 |
-
else:
|
| 289 |
-
conv = get_conversation_template(args.model_path)
|
| 290 |
-
conv.append_message(conv.roles[0], msg)
|
| 291 |
-
conv.append_message(conv.roles[1], None)
|
| 292 |
-
conv.system = ''
|
| 293 |
-
prompt = conv.get_prompt()
|
| 294 |
-
|
| 295 |
-
outputs = fastchat(prompt, model, tokenizer)
|
| 296 |
-
|
| 297 |
-
with open(f'result/prompt2_instruction/parsing/{args.model_path}/ptb/{gid}.txt', 'w') as f:
|
| 298 |
-
f.write(outputs)
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
if args.prompt == 3:
|
| 303 |
-
for gid in tqdm(gid_list, desc='Query'):
|
| 304 |
-
text = ptb[gid]['text']
|
| 305 |
-
tokens = ptb[gid]['tokens']
|
| 306 |
-
poss = ptb[gid]['uni_poss']
|
| 307 |
-
|
| 308 |
-
## POS tagging
|
| 309 |
-
if os.path.exists(f'result/prompt3_structured_prompt/pos_tagging/{args.model_path}/ptb/{gid}.txt'):
|
| 310 |
-
print(gid, 'skip')
|
| 311 |
-
continue
|
| 312 |
-
|
| 313 |
-
prompt = demon_pos + '\n' + 'C: ' + text + '\n' + 'T: '
|
| 314 |
-
|
| 315 |
-
if 'gpt3' in args.model_path:
|
| 316 |
-
outputs = gpt3(prompt)
|
| 317 |
-
if outputs is None:
|
| 318 |
-
continue
|
| 319 |
-
time.sleep(0.2)
|
| 320 |
-
|
| 321 |
-
else:
|
| 322 |
-
pred_poss = []
|
| 323 |
-
for _tok, _pos in zip(tokens, poss):
|
| 324 |
-
prompt = prompt + ' ' + _tok + '_'
|
| 325 |
-
outputs = structured_prompt(prompt, model, tokenizer, bad_words_ids_pos)
|
| 326 |
-
prompt = prompt + outputs
|
| 327 |
-
pred_poss.append(outputs)
|
| 328 |
-
|
| 329 |
-
outputs = ' '.join(pred_poss)
|
| 330 |
-
with open(f'result/prompt3_structured_prompt/pos_tagging/{args.model_path}/ptb/{gid}.txt', 'w') as f:
|
| 331 |
-
f.write(outputs)
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
## Chunking
|
| 335 |
-
if os.path.exists(f'result/prompt3_structured_prompt/chunking/{args.model_path}/ptb/{gid}.txt'):
|
| 336 |
-
print(gid, 'skip')
|
| 337 |
-
continue
|
| 338 |
-
|
| 339 |
-
prompt = demon_chunk + '\n' + 'C: ' + text + '\n' + 'T: '
|
| 340 |
-
|
| 341 |
-
if 'gpt3' in args.model_path:
|
| 342 |
-
outputs = gpt3(prompt)
|
| 343 |
-
print(outputs)
|
| 344 |
-
if outputs is None:
|
| 345 |
-
continue
|
| 346 |
-
time.sleep(0.2)
|
| 347 |
-
|
| 348 |
-
else:
|
| 349 |
-
pred_chunk = []
|
| 350 |
-
for _tok, _pos in zip(tokens, poss):
|
| 351 |
-
prompt = prompt + ' ' + _tok + '_'
|
| 352 |
-
|
| 353 |
-
# Generate BIO
|
| 354 |
-
outputs_bio = structured_prompt(prompt, model, tokenizer, bad_words_ids_bio)
|
| 355 |
-
prompt = prompt + outputs_bio + '-'
|
| 356 |
-
|
| 357 |
-
# Generate tag
|
| 358 |
-
outputs_chunk = structured_prompt(prompt, model, tokenizer, bad_words_ids_chunk)
|
| 359 |
-
prompt = prompt + outputs_chunk
|
| 360 |
-
|
| 361 |
-
pred_chunk.append((outputs_bio + '-' + outputs_chunk))
|
| 362 |
-
|
| 363 |
-
outputs = ' '.join(pred_chunk)
|
| 364 |
-
|
| 365 |
-
with open(f'result/prompt3_structured_prompt/chunking/{args.model_path}/ptb/{gid}.txt', 'w') as f:
|
| 366 |
-
f.write(outputs)
|
| 367 |
-
|
| 368 |
-
## Parsing
|
| 369 |
-
if os.path.exists(f'result/prompt3_structured_prompt/parsing/{args.model_path}/ptb/{gid}.txt'):
|
| 370 |
-
print(gid, 'skip')
|
| 371 |
-
continue
|
| 372 |
-
|
| 373 |
-
prompt = demon_parse + '\n' + 'C: ' + text + '\n' + 'T: '
|
| 374 |
-
|
| 375 |
-
if 'gpt3' in args.model_path:
|
| 376 |
-
outputs = gpt3(prompt)
|
| 377 |
-
if outputs is None:
|
| 378 |
-
continue
|
| 379 |
-
time.sleep(0.2)
|
| 380 |
-
|
| 381 |
-
else:
|
| 382 |
-
pred_syn = []
|
| 383 |
-
for _tok, _pos in zip(tokens, poss):
|
| 384 |
-
prompt = prompt + _tok + '_'
|
| 385 |
-
outputs = structured_prompt(prompt, model, tokenizer, bad_words_ids_parse)
|
| 386 |
-
pred_syn.append(outputs)
|
| 387 |
-
|
| 388 |
-
with open(f'result/prompt3_structured_prompt/parsing/{args.model_path}/ptb/{gid}.txt', 'w') as f:
|
| 389 |
-
f.write(' '.join(pred_syn))
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
def structured_prompt(prompt, model, tokenizer, bad_words_ids):
|
| 393 |
-
input_ids = tokenizer([prompt]).input_ids
|
| 394 |
-
output_ids = model.generate(
|
| 395 |
-
torch.as_tensor(input_ids).cuda(),
|
| 396 |
-
max_new_tokens=1,
|
| 397 |
-
bad_words_ids=bad_words_ids,
|
| 398 |
-
)
|
| 399 |
-
|
| 400 |
-
if model.config.is_encoder_decoder:
|
| 401 |
-
output_ids = output_ids[0]
|
| 402 |
-
else:
|
| 403 |
-
output_ids = output_ids[0][len(input_ids[0]) :]
|
| 404 |
-
outputs = tokenizer.decode(
|
| 405 |
-
output_ids, skip_special_tokens=True, spaces_between_special_tokens=False
|
| 406 |
-
)
|
| 407 |
-
|
| 408 |
-
return outputs
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
def fastchat(prompt, model, tokenizer):
|
| 412 |
-
input_ids = tokenizer([prompt]).input_ids
|
| 413 |
-
output_ids = model.generate(
|
| 414 |
-
torch.as_tensor(input_ids).cuda(),
|
| 415 |
-
do_sample=True,
|
| 416 |
-
temperature=args.temperature,
|
| 417 |
-
repetition_penalty=args.repetition_penalty,
|
| 418 |
-
max_new_tokens=args.max_new_tokens,
|
| 419 |
-
)
|
| 420 |
-
|
| 421 |
-
if model.config.is_encoder_decoder:
|
| 422 |
-
output_ids = output_ids[0]
|
| 423 |
-
else:
|
| 424 |
-
output_ids = output_ids[0][len(input_ids[0]) :]
|
| 425 |
-
outputs = tokenizer.decode(
|
| 426 |
-
output_ids, skip_special_tokens=True, spaces_between_special_tokens=False
|
| 427 |
-
)
|
| 428 |
-
|
| 429 |
-
#print('Empty system message')
|
| 430 |
-
#print(f"{conv.roles[0]}: {msg}")
|
| 431 |
-
#print(f"{conv.roles[1]}: {outputs}")
|
| 432 |
-
|
| 433 |
-
return outputs
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
def gpt3(prompt):
|
| 437 |
-
try:
|
| 438 |
-
response = openai.ChatCompletion.create(
|
| 439 |
-
model=model_mapping[args.model_path], messages=[{"role": "user", "content": prompt}])
|
| 440 |
-
|
| 441 |
-
return response['choices'][0]['message']['content']
|
| 442 |
-
|
| 443 |
-
except Exception as err:
|
| 444 |
-
print('Error')
|
| 445 |
-
print(err)
|
| 446 |
-
|
| 447 |
-
return None
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
if __name__ == "__main__":
|
| 451 |
-
parser = argparse.ArgumentParser()
|
| 452 |
-
add_model_args(parser)
|
| 453 |
-
parser.add_argument("--temperature", type=float, default=0.7)
|
| 454 |
-
parser.add_argument("--repetition_penalty", type=float, default=1.0)
|
| 455 |
-
parser.add_argument("--max-new-tokens", type=int, default=512)
|
| 456 |
-
parser.add_argument("--debug", action="store_true")
|
| 457 |
-
parser.add_argument("--message", type=str, default="Hello! Who are you?")
|
| 458 |
-
parser.add_argument("--start", type=int, default=0)
|
| 459 |
-
parser.add_argument("--end", type=int, default=1000)
|
| 460 |
-
parser.add_argument("--prompt", required=True, type=int, default=None)
|
| 461 |
-
# parser.add_argument("--system_msg", required=True, type=str, default='default_system_msg')
|
| 462 |
-
args = parser.parse_args()
|
| 463 |
-
|
| 464 |
-
# Reset default repetition penalty for T5 models.
|
| 465 |
-
if "t5" in args.model_path and args.repetition_penalty == 1.0:
|
| 466 |
-
args.repetition_penalty = 1.2
|
| 467 |
-
|
| 468 |
-
main(args)
|
|
|
|
| 15 |
from nltk.tag.mapping import _UNIVERSAL_TAGS
|
| 16 |
|
| 17 |
import gradio as gr
|
| 18 |
+
from transformers import pipeline
|
| 19 |
|
| 20 |
uni_tags = list(_UNIVERSAL_TAGS)
|
| 21 |
uni_tags[-1] = 'PUNC'
|
|
|
|
| 27 |
'WHADVP', 'NX', 'PRT', 'VBZ', 'VBP', 'MD', 'NN', 'WHPP', 'SQ', 'SBARQ', 'LST', 'INTJ', 'X', 'UCP', 'CONJP', 'NNP', 'CD', 'JJ',
|
| 28 |
'VBD', 'WHADJP', 'PRP', 'RRC', 'NNS', 'SYM', 'CC']
|
| 29 |
|
| 30 |
+
openai.api_key = " "
|
|
|
|
| 31 |
|
| 32 |
# determinant vs. determiner
|
| 33 |
# https://wikidiff.com/determiner/determinant
|
|
|
|
| 48 |
|
| 49 |
for i, j in zip(ents, ents_prompt):
|
| 50 |
print(i, j)
|
|
|
|
|
|
|
| 51 |
|
| 52 |
model_mapping = {
|
| 53 |
+
'gpt3.5': 'gpt2',
|
| 54 |
+
#'vicuna-7b': 'lmsys/vicuna-7b-v1.3',
|
| 55 |
+
#'llama-7b': './llama/hf/7B',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
with open('sample_uniform_1k_2.txt', 'r') as f:
|
| 59 |
selected_idx = f.readlines()
|
| 60 |
selected_idx = [int(i.strip()) for i in selected_idx]#[s:e]
|
| 61 |
|
|
|
|
| 62 |
ptb = []
|
| 63 |
with open('ptb.jsonl', 'r') as f:
|
| 64 |
for l in f:
|
|
|
|
| 84 |
with open('demonstration_3_42_parse.txt', 'r') as f:
|
| 85 |
demon_parse = f.read()
|
| 86 |
|
| 87 |
+
# Your existing code
|
| 88 |
+
theme = gr.themes.Soft()
|
| 89 |
|
| 90 |
+
# issue get request for gpt 3.5
|
| 91 |
+
gpt_pipeline = pipeline(task="text2text-generation", model="gpt2")
|
| 92 |
+
#vicuna7b_pipeline = pipeline(task="text2text-generation", model="lmsys/vicuna-7b-v1.3")
|
| 93 |
+
#llama7b_pipeline = pipeline(task="text2text-generation", model="./llama/hf/7B")
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# Dropdown options for model and task
|
| 96 |
+
model_options = list(model_mapping.keys())
|
| 97 |
+
task_options = ['POS', 'Chunking'] # remove parsing
|
| 98 |
|
| 99 |
|
| 100 |
+
# Function to process text based on model and task
|
| 101 |
+
def process_text(model_name, task, text):
|
| 102 |
+
gid_list = selected_idx[0:20]
|
| 103 |
|
| 104 |
+
for gid in tqdm(gid_list, desc='Query'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
text = ptb[gid]['text']
|
| 106 |
+
|
| 107 |
+
if model_name == 'vicuna-7b':
|
| 108 |
+
if task == 'POS':
|
| 109 |
+
strategy1_format = template_all.format(text)
|
| 110 |
+
strategy2_format = prompt2_pos.format(text)
|
| 111 |
+
strategy3_format = demon_pos
|
| 112 |
+
|
| 113 |
+
result1 = gpt_pipeline(strategy1_format)[0]['generated_text']
|
| 114 |
+
result2 = gpt_pipeline(strategy2_format)[0]['generated_text']
|
| 115 |
+
result3 = gpt_pipeline(strategy3_format)[0]['generated_text']
|
| 116 |
+
return (result1, result2, result3)
|
| 117 |
+
elif task == 'Chunking':
|
| 118 |
+
strategy1_format = template_all.format(text)
|
| 119 |
+
strategy2_format = prompt2_chunk.format(text)
|
| 120 |
+
strategy3_format = demon_chunk
|
| 121 |
+
|
| 122 |
+
result1 = gpt_pipeline(strategy1_format)[0]['generated_text']
|
| 123 |
+
result2 = gpt_pipeline(strategy2_format)[0]['generated_text']
|
| 124 |
+
result3 = gpt_pipeline(strategy3_format)[0]['generated_text']
|
| 125 |
+
return (result1, result2, result3)
|
| 126 |
+
|
| 127 |
+
# Gradio interface
|
| 128 |
+
iface = gr.Interface(
|
| 129 |
+
fn=process_text,
|
| 130 |
+
inputs=[
|
| 131 |
+
gr.Dropdown(model_options, label="Select Model"),
|
| 132 |
+
gr.Dropdown(task_options, label="Select Task"),
|
| 133 |
+
gr.Textbox(label="Input Text", placeholder="Enter the text to process..."),
|
| 134 |
+
],
|
| 135 |
+
outputs=[
|
| 136 |
+
gr.Textbox(label="Strategy 1 QA Result"),
|
| 137 |
+
gr.Textbox(label="Strategy 2 Instruction Result"),
|
| 138 |
+
gr.Textbox(label="Strategy 3 Structured Prompting Result"),
|
| 139 |
+
],
|
| 140 |
+
title = "LLM Evaluator For Linguistic Scrutiny",
|
| 141 |
+
theme = theme,
|
| 142 |
+
live=False,
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
iface.launch()
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|