Spaces:
Runtime error
Runtime error
File size: 9,124 Bytes
b308128 5e8be56 b308128 7438e40 b308128 5e8be56 b308128 0274a84 04fc021 bfc086f 04fc021 b308128 9f1cf26 c0ac2c5 7438e40 b308128 04fc021 323ed1c 04fc021 bfc086f 04fc021 b308128 04fc021 5e8be56 cc5a0c8 b308128 7ddcdfa 04fc021 99d8a50 04fc021 b3044ef 323ed1c 99d8a50 04fc021 b3044ef 323ed1c 99d8a50 04fc021 b3044ef 323ed1c 99d8a50 bfc086f 04fc021 6f1af31 5e8be56 a862f54 5e8be56 b308128 a862f54 4ebd536 a862f54 04fc021 abba6e8 5e8be56 04fc021 |
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 |
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
from transformers import pipeline
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 = "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"
# 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)
model_mapping = {
'gpt3.5': 'gpt2',
#'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',
#'llama-7b': './llama/hf/7B',
#'llama-13b': './llama/hf/13B',
#'llama-30b': './llama/hf/30B',
#'alpaca': './alpaca-7B',
}
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()
# Your existing code
theme = gr.themes.Soft()
gpt_pipeline = pipeline(task="text-generation", model="gpt2")
#vicuna7b_pipeline = pipeline(task="text2text-generation", model="lmsys/vicuna-7b-v1.3")
#vicuna13b_pipeline = pipeline(task="text2text-generation", model="lmsys/vicuna-13b-v1.3")
#vicuna33b_pipeline = pipeline(task="text2text-generation", model="lmsys/vicuna-33b-v1.3")
fastchatT5_pipeline = pipeline(task="text2text-generation", model="lmsys/fastchat-t5-3b-v1.0")
#llama7b_pipeline = pipeline(task="text2text-generation", model="./llama/hf/7B")
#llama13b_pipeline = pipeline(task="text2text-generation", model="./llama/hf/13B")
#llama30b_pipeline = pipeline(task="text2text-generation", model="./llama/hf/30B")
#alpaca_pipeline = pipeline(task="text2text-generation", model="./alpaca-7B")
# Dropdown options for model and task
model_options = list(model_mapping.keys())
task_options = ['POS', 'Chunking', 'Parsing']
# Function to process text based on model and task
def process_text(model_name, task, text):
gid_list = selected_idx[0:20]
for gid in tqdm(gid_list, desc='Query'):
text = ptb[gid]['text']
if model_name == 'gpt3.5':
if task == 'POS':
strategy1 = template_all.format(text)
strategy2 = prompt2_pos.format(text)
strategy3 = demon_pos
response1 = gpt_pipeline(strategy1)[0]['generated_text']
response2 = gpt_pipeline(strategy2)[0]['generated_text']
response3 = gpt_pipeline(strategy3)[0]['generated_text']
return (response1, response2, response3)
elif task == 'Chunking':
strategy1 = template_all.format(text)
strategy2 = prompt2_chunk.format(text)
strategy3 = demon_chunk
response1 = gpt_pipeline(strategy1)[0]['generated_text']
response2 = gpt_pipeline(strategy2)[0]['generated_text']
response3 = gpt_pipeline(strategy3)[0]['generated_text']
return (response1, response2, response3)
elif task == 'Parsing':
strategy1 = template_all.format(text)
strategy2 = prompt2_parse.format(text)
strategy3 = demon_parse
response1 = gpt_pipeline(strategy1)[0]['generated_text']
response2 = gpt_pipeline(strategy2)[0]['generated_text']
response3 = gpt_pipeline(strategy3)[0]['generated_text']
return (response1, response2, response3)
elif model_name == 'fastchat-t5':
if task == 'POS':
strategy1 = template_all.format(text)
strategy2 = prompt2_pos.format(text)
strategy3 = demon_pos
response1 = fastchatT5_pipeline(strategy1)[0]['generated_text']
response2 = fastchatT5_pipeline(strategy2)[0]['generated_text']
response3 = fastchatT5_pipeline(strategy3)[0]['generated_text']
return (response1, response2, response3)
elif task == 'Chunking':
strategy1 = template_all.format(text)
strategy2 = prompt2_chunk.format(text)
strategy3 = demon_chunk
response1 = fastchatT5_pipeline(strategy1)[0]['generated_text']
response2 = fastchatT5_pipeline(strategy2)[0]['generated_text']
response3 = fastchatT5_pipeline(strategy3)[0]['generated_text']
return (response1, response2, response3)
elif task == 'Parsing':
strategy1 = template_all.format(text)
strategy2 = prompt2_parse.format(text)
strategy3 = demon_parse
response1 = fastchatT5_pipeline(strategy1)[0]['generated_text']
response2 = fastchatT5_pipeline(strategy2)[0]['generated_text']
response3 = fastchatT5_pipeline(strategy3)[0]['generated_text']
return (response1, response2, response3)
# Define prompts for each strategy based on the task
#strategy_prompts = {
# 'Strategy 1': template_all.format(text),
# 'Strategy 2': {
# 'POS': prompt2_pos.format(text),
# 'Chunking': prompt2_chunk.format(text),
# 'Parsing': prompt2_parse.format(text),
# }.get(task, "Invalid Task Selection for Strategy 2"),
# 'Strategy 3': {
# 'POS': demon_pos,
# 'Chunking': demon_chunk,
# 'Parsing': demon_parse,
# }.get(task, "Invalid Task Selection for Strategy 3"),
#}
# Gradio interface
iface = gr.Interface(
fn=process_text,
inputs=[
gr.Dropdown(model_options, label="Select Model"),
gr.Dropdown(task_options, label="Select Task"),
gr.Textbox(label="Input Text", placeholder="Enter the text to process..."),
],
outputs=[
gr.Textbox(label="Strategy 1 QA Result"),
gr.Textbox(label="Strategy 2 Instruction Result"),
gr.Textbox(label="Strategy 3 Structured Prompting Result"),
],
title = "LLM Evaluator For Linguistic Scrutiny",
theme = theme,
live=False,
)
iface.launch()
|