research14 commited on
Commit
b308128
1 Parent(s): c0ac2c5
Files changed (2) hide show
  1. app.py +133 -29
  2. run_llm.py +80 -11
app.py CHANGED
@@ -1,52 +1,152 @@
1
- import gradio as gr
 
2
  import json
3
- from run_llm import template_all, prompt2_pos, prompt2_chunk, prompt2_parse, demon_pos, demon_chunk, demon_parse, model_mapping
 
 
 
 
4
  from tqdm import tqdm
 
 
5
 
6
- # Your existing code
7
- theme = gr.themes.Soft()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  with open('sample_uniform_1k_2.txt', 'r') as f:
10
  selected_idx = f.readlines()
11
  selected_idx = [int(i.strip()) for i in selected_idx]#[s:e]
12
 
13
- gid_list = selected_idx[0]
14
-
15
  ptb = []
16
  with open('ptb.jsonl', 'r') as f:
17
  for l in f:
18
  ptb.append(json.loads(l))
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  # Function to process text based on model and task
21
  def process_text(model_name, task, text):
 
 
22
  for gid in tqdm(gid_list, desc='Query'):
23
  text = ptb[gid]['text']
 
 
 
 
 
 
 
 
 
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  # Define prompts for each strategy based on the task
26
- strategy_prompts = {
27
- 'Strategy 1': template_all.format(text),
28
- 'Strategy 2': {
29
- 'POS': prompt2_pos.format(text),
30
- 'Chunking': prompt2_chunk.format(text),
31
- 'Parsing': prompt2_parse.format(text),
32
- }.get(task, "Invalid Task Selection for Strategy 2"),
33
- 'Strategy 3': {
34
- 'POS': demon_pos,
35
- 'Chunking': demon_chunk,
36
- 'Parsing': demon_parse,
37
- }.get(task, "Invalid Task Selection for Strategy 3"),
38
- }
39
-
40
- # Get the selected prompt based on the strategy
41
- prompt = strategy_prompts.get(model_name, "Invalid Model Selection")
42
-
43
- # Add your logic to feed the prompt to the selected model and get the result
44
- result = "Processed Result" # Replace this with your actual result
45
- return result
46
 
47
- # Dropdown options for model and task
48
- model_options = list(model_mapping.keys())
49
- task_options = ['POS', 'Chunking', 'Parsing']
50
 
51
  # Gradio interface
52
  iface = gr.Interface(
@@ -54,14 +154,18 @@ iface = gr.Interface(
54
  inputs=[
55
  gr.Dropdown(model_options, label="Select Model"),
56
  gr.Dropdown(task_options, label="Select Task"),
 
57
  ],
58
  outputs=[
59
  gr.Textbox(label="Strategy 1 QA Result"),
60
  gr.Textbox(label="Strategy 2 Instruction Result"),
61
  gr.Textbox(label="Strategy 3 Structured Prompting Result"),
62
  ],
 
63
  theme = theme,
64
  live=False,
65
  )
66
 
67
  iface.launch()
 
 
 
1
+ import os
2
+ import sys
3
  import json
4
+ import time
5
+ import openai
6
+ import pickle
7
+ import argparse
8
+ import requests
9
  from tqdm import tqdm
10
+ import torch
11
+ from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaForCausalLM, LlamaTokenizer
12
 
13
+ from fastchat.model import load_model, get_conversation_template, add_model_args
14
+
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'
22
+
23
+ bio_tags = ['B', 'I', 'O']
24
+ chunk_tags = ['ADJP', 'ADVP', 'CONJP', 'INTJ', 'LST', 'NP', 'O', 'PP', 'PRT', 'SBAR', 'UCP', 'VP']
25
+
26
+ syntags = ['NP', 'S', 'VP', 'ADJP', 'ADVP', 'SBAR', 'TOP', 'PP', 'POS', 'NAC', "''", 'SINV', 'PRN', 'QP', 'WHNP', 'RB', 'FRAG',
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 = "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"
31
+
32
+ # determinant vs. determiner
33
+ # https://wikidiff.com/determiner/determinant
34
+ ents_prompt = ['Noun','Verb','Adjective','Adverb','Preposition/Subord','Coordinating Conjunction',# 'Cardinal Number',
35
+ 'Determiner',
36
+ 'Noun Phrase','Verb Phrase','Adjective Phrase','Adverb Phrase','Preposition Phrase','Conjunction Phrase','Coordinate Phrase','Quantitave Phrase','Complex Nominal',
37
+ 'Clause','Dependent Clause','Fragment Clause','T-unit','Complex T-unit',# 'Fragment T-unit',
38
+ ][7:]
39
+ ents = ['NN', 'VB', 'JJ', 'RB', 'IN', 'CC', 'DT', 'NP', 'VP', 'ADJP', 'ADVP', 'PP', 'CONJP', 'CP', 'QP', 'CN', 'C', 'DC', 'FC', 'T', 'CT'][7:]
40
+
41
+
42
+ ents_prompt_uni_tags = ['Verb', 'Noun', 'Pronoun', 'Adjective', 'Adverb', 'Preposition and Postposition', 'Coordinating Conjunction',
43
+ 'Determiner', 'Cardinal Number', 'Particles or other function words',
44
+ 'Words that cannot be assigned a POS tag', 'Punctuation']
45
+
46
+ ents = uni_tags + ents
47
+ ents_prompt = ents_prompt_uni_tags + ents_prompt
48
+
49
+ for i, j in zip(ents, ents_prompt):
50
+ print(i, j)
51
+
52
+ model_mapping = {
53
+ 'gpt3.5': 'gpt-3.5-turbo-0613',
54
+ 'vicuna-7b': 'lmsys/vicuna-7b-v1.3',
55
+ 'vicuna-13b': 'lmsys/vicuna-13b-v1.3',
56
+ 'vicuna-33b': 'lmsys/vicuna-33b-v1.3',
57
+ 'fastchat-t5': 'lmsys/fastchat-t5-3b-v1.0',
58
+ 'llama-7b': './llama/hf/7B',
59
+ 'llama-13b': './llama/hf/13B',
60
+ 'llama-30b': './llama/hf/30B',
61
+ 'alpaca': './alpaca-7B',
62
+ }
63
 
64
  with open('sample_uniform_1k_2.txt', 'r') as f:
65
  selected_idx = f.readlines()
66
  selected_idx = [int(i.strip()) for i in selected_idx]#[s:e]
67
 
 
 
68
  ptb = []
69
  with open('ptb.jsonl', 'r') as f:
70
  for l in f:
71
  ptb.append(json.loads(l))
72
 
73
+
74
+ ## Prompt 1
75
+ 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: "{}"'''
76
+ template_single = '''Please output any <{}> in the following sentence one per line without any additional text: "{}"'''
77
+
78
+ ## Prompt 2
79
+ prompt2_pos = '''Please pos tag the following sentence using Universal POS tag set without generating any additional text: {}'''
80
+ prompt2_chunk = '''Please do sentence chunking for the following sentence as in CoNLL 2000 shared task without generating any addtional text: {}'''
81
+ prompt2_parse = '''Generate textual representation of the constituency parse tree of the following sentence using Penn TreeBank tag set without outputing any additional text: {}'''
82
+
83
+ prompt2_chunk = '''Please chunk the following sentence in CoNLL 2000 format with BIO tags without outputing any additional text: {}'''
84
+
85
+ ## Prompt 3
86
+ with open('demonstration_3_42_pos.txt', 'r') as f:
87
+ demon_pos = f.read()
88
+ with open('demonstration_3_42_chunk.txt', 'r') as f:
89
+ demon_chunk = f.read()
90
+ with open('demonstration_3_42_parse.txt', 'r') as f:
91
+ demon_parse = f.read()
92
+
93
+ # Your existing code
94
+ theme = gr.themes.Soft()
95
+
96
+ pipeline = pipeline(task="text-generation", model="lmsys/vicuna-7b-v1.3")
97
+
98
+ # Dropdown options for model and task
99
+ model_options = list(model_mapping.keys())
100
+ task_options = ['POS', 'Chunking', 'Parsing']
101
+
102
  # Function to process text based on model and task
103
  def process_text(model_name, task, text):
104
+ gid_list = selected_idx[0:20]
105
+
106
  for gid in tqdm(gid_list, desc='Query'):
107
  text = ptb[gid]['text']
108
+
109
+ #if model_name is 'gpt3.5': 'gpt-3.5-turbo-0613',
110
+ #elif model_name is 'vicuna-7b': 'lmsys/vicuna-7b-v1.3',
111
+ #elif model_name is 'vicuna-13b': 'lmsys/vicuna-13b-v1.3',
112
+ #elif model_name is 'vicuna-33b': 'lmsys/vicuna-33b-v1.3',
113
+ #elif model_name is 'fastchat-t5': 'lmsys/fastchat-t5-3b-v1.0',
114
+ #elif model_name is 'llama-7b': './llama/hf/7B',
115
+ #elif model_name is 'llama-13b': './llama/hf/13B',
116
+ #elif model_name is 'llama-30b': './llama/hf/30B',
117
+ #elif model_name is 'alpaca': './alpaca-7B',
118
 
119
+ if task == 'POS':
120
+ strategy1 = pipeline(template_all.format(text))
121
+ strategy2 = pipeline(prompt2_pos.format(text))
122
+ strategy3 = pipeline(demon_pos)
123
+ return (strategy1, strategy2, strategy3)
124
+ elif task == 'Chunking':
125
+ strategy1 = pipeline(template_all.format(text))
126
+ strategy2 = pipeline(prompt2_chunk.format(text))
127
+ strategy3 = pipeline(demon_chunk)
128
+ return (strategy1, strategy2, strategy3)
129
+ elif task == 'Parsing':
130
+ strategy1 = pipeline(template_all.format(text))
131
+ strategy2 = pipeline(prompt2_parse.format(text))
132
+ strategy3 = pipeline(demon_parse)
133
+ return (strategy1, strategy2, strategy3)
134
+
135
  # Define prompts for each strategy based on the task
136
+ #strategy_prompts = {
137
+ # 'Strategy 1': template_all.format(text),
138
+ # 'Strategy 2': {
139
+ # 'POS': prompt2_pos.format(text),
140
+ # 'Chunking': prompt2_chunk.format(text),
141
+ # 'Parsing': prompt2_parse.format(text),
142
+ # }.get(task, "Invalid Task Selection for Strategy 2"),
143
+ # 'Strategy 3': {
144
+ # 'POS': demon_pos,
145
+ # 'Chunking': demon_chunk,
146
+ # 'Parsing': demon_parse,
147
+ # }.get(task, "Invalid Task Selection for Strategy 3"),
148
+ #}
 
 
 
 
 
 
 
149
 
 
 
 
150
 
151
  # Gradio interface
152
  iface = gr.Interface(
 
154
  inputs=[
155
  gr.Dropdown(model_options, label="Select Model"),
156
  gr.Dropdown(task_options, label="Select Task"),
157
+ gr.Textbox(label="Input Text", placeholder="Enter the text to process..."),
158
  ],
159
  outputs=[
160
  gr.Textbox(label="Strategy 1 QA Result"),
161
  gr.Textbox(label="Strategy 2 Instruction Result"),
162
  gr.Textbox(label="Strategy 3 Structured Prompting Result"),
163
  ],
164
+ title = "LLM Evaluator For Linguistic Scrutiny",
165
  theme = theme,
166
  live=False,
167
  )
168
 
169
  iface.launch()
170
+
171
+
run_llm.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'
@@ -28,7 +29,6 @@ syntags = ['NP', 'S', 'VP', 'ADJP', 'ADVP', 'SBAR', 'TOP', 'PP', 'POS', 'NAC', "
28
 
29
  openai.api_key = "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"
30
 
31
-
32
  # determinant vs. determiner
33
  # https://wikidiff.com/determiner/determinant
34
  ents_prompt = ['Noun','Verb','Adjective','Adverb','Preposition/Subord','Coordinating Conjunction',# 'Cardinal Number',
@@ -48,33 +48,23 @@ 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
- # 'gpt3': 'gpt-3',
56
  'gpt3.5': 'gpt-3.5-turbo-0613',
57
  'vicuna-7b': 'lmsys/vicuna-7b-v1.3',
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
  with open('sample_uniform_1k_2.txt', 'r') as f:
74
  selected_idx = f.readlines()
75
  selected_idx = [int(i.strip()) for i in selected_idx]#[s:e]
76
 
77
-
78
  ptb = []
79
  with open('ptb.jsonl', 'r') as f:
80
  for l in f:
@@ -100,3 +90,82 @@ with open('demonstration_3_42_chunk.txt', 'r') as f:
100
  with open('demonstration_3_42_parse.txt', 'r') as f:
101
  demon_parse = f.read()
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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'
 
29
 
30
  openai.api_key = "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"
31
 
 
32
  # determinant vs. determiner
33
  # https://wikidiff.com/determiner/determinant
34
  ents_prompt = ['Noun','Verb','Adjective','Adverb','Preposition/Subord','Coordinating Conjunction',# 'Cardinal Number',
 
48
 
49
  for i, j in zip(ents, ents_prompt):
50
  print(i, j)
 
 
51
 
52
  model_mapping = {
 
53
  'gpt3.5': 'gpt-3.5-turbo-0613',
54
  'vicuna-7b': 'lmsys/vicuna-7b-v1.3',
55
  'vicuna-13b': 'lmsys/vicuna-13b-v1.3',
56
  'vicuna-33b': 'lmsys/vicuna-33b-v1.3',
57
  'fastchat-t5': 'lmsys/fastchat-t5-3b-v1.0',
 
 
 
58
  'llama-7b': './llama/hf/7B',
59
  'llama-13b': './llama/hf/13B',
60
  'llama-30b': './llama/hf/30B',
 
61
  'alpaca': './alpaca-7B',
 
 
62
  }
63
 
64
  with open('sample_uniform_1k_2.txt', 'r') as f:
65
  selected_idx = f.readlines()
66
  selected_idx = [int(i.strip()) for i in selected_idx]#[s:e]
67
 
 
68
  ptb = []
69
  with open('ptb.jsonl', 'r') as f:
70
  for l in f:
 
90
  with open('demonstration_3_42_parse.txt', 'r') as f:
91
  demon_parse = f.read()
92
 
93
+ # Your existing code
94
+ theme = gr.themes.Soft()
95
+
96
+ pipeline = pipeline(task="text-generation", model="lmsys/vicuna-7b-v1.3")
97
+
98
+ # Dropdown options for model and task
99
+ model_options = list(model_mapping.keys())
100
+ task_options = ['POS', 'Chunking', 'Parsing']
101
+
102
+ # Function to process text based on model and task
103
+ def process_text(model_name, task, text):
104
+ gid_list = selected_idx[0:20]
105
+
106
+ for gid in tqdm(gid_list, desc='Query'):
107
+ text = ptb[gid]['text']
108
+
109
+ #if model_name is 'gpt3.5': 'gpt-3.5-turbo-0613',
110
+ #elif model_name is 'vicuna-7b': 'lmsys/vicuna-7b-v1.3',
111
+ #elif model_name is 'vicuna-13b': 'lmsys/vicuna-13b-v1.3',
112
+ #elif model_name is 'vicuna-33b': 'lmsys/vicuna-33b-v1.3',
113
+ #elif model_name is 'fastchat-t5': 'lmsys/fastchat-t5-3b-v1.0',
114
+ #elif model_name is 'llama-7b': './llama/hf/7B',
115
+ #elif model_name is 'llama-13b': './llama/hf/13B',
116
+ #elif model_name is 'llama-30b': './llama/hf/30B',
117
+ #elif model_name is 'alpaca': './alpaca-7B',
118
+
119
+ if task == 'POS':
120
+ strategy1 = pipeline(template_all.format(text))
121
+ strategy2 = pipeline(prompt2_pos.format(text))
122
+ strategy3 = pipeline(demon_pos)
123
+ return (strategy1, strategy2, strategy3)
124
+ elif task == 'Chunking':
125
+ strategy1 = pipeline(template_all.format(text))
126
+ strategy2 = pipeline(prompt2_chunk.format(text))
127
+ strategy3 = pipeline(demon_chunk)
128
+ return (strategy1, strategy2, strategy3)
129
+ elif task == 'Parsing':
130
+ strategy1 = pipeline(template_all.format(text))
131
+ strategy2 = pipeline(prompt2_parse.format(text))
132
+ strategy3 = pipeline(demon_parse)
133
+ return (strategy1, strategy2, strategy3)
134
+
135
+ # Define prompts for each strategy based on the task
136
+ #strategy_prompts = {
137
+ # 'Strategy 1': template_all.format(text),
138
+ # 'Strategy 2': {
139
+ # 'POS': prompt2_pos.format(text),
140
+ # 'Chunking': prompt2_chunk.format(text),
141
+ # 'Parsing': prompt2_parse.format(text),
142
+ # }.get(task, "Invalid Task Selection for Strategy 2"),
143
+ # 'Strategy 3': {
144
+ # 'POS': demon_pos,
145
+ # 'Chunking': demon_chunk,
146
+ # 'Parsing': demon_parse,
147
+ # }.get(task, "Invalid Task Selection for Strategy 3"),
148
+ #}
149
+
150
+
151
+ # Gradio interface
152
+ iface = gr.Interface(
153
+ fn=process_text,
154
+ inputs=[
155
+ gr.Dropdown(model_options, label="Select Model"),
156
+ gr.Dropdown(task_options, label="Select Task"),
157
+ gr.Textbox(label="Input Text", placeholder="Enter the text to process..."),
158
+ ],
159
+ outputs=[
160
+ gr.Textbox(label="Strategy 1 QA Result"),
161
+ gr.Textbox(label="Strategy 2 Instruction Result"),
162
+ gr.Textbox(label="Strategy 3 Structured Prompting Result"),
163
+ ],
164
+ title = "LLM Evaluator For Linguistic Scrutiny",
165
+ theme = theme,
166
+ live=False,
167
+ )
168
+
169
+ iface.launch()
170
+
171
+