research14 commited on
Commit
f9ca505
1 Parent(s): 037e269

test chatbot

Browse files
Files changed (2) hide show
  1. app.py +25 -166
  2. placeholder.py +175 -0
app.py CHANGED
@@ -1,175 +1,34 @@
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
- demo = gr.Blocks()
21
-
22
- uni_tags = list(_UNIVERSAL_TAGS)
23
- uni_tags[-1] = 'PUNC'
24
-
25
- bio_tags = ['B', 'I', 'O']
26
- chunk_tags = ['ADJP', 'ADVP', 'CONJP', 'INTJ', 'LST', 'NP', 'O', 'PP', 'PRT', 'SBAR', 'UCP', 'VP']
27
-
28
- syntags = ['NP', 'S', 'VP', 'ADJP', 'ADVP', 'SBAR', 'TOP', 'PP', 'POS', 'NAC', "''", 'SINV', 'PRN', 'QP', 'WHNP', 'RB', 'FRAG',
29
- 'WHADVP', 'NX', 'PRT', 'VBZ', 'VBP', 'MD', 'NN', 'WHPP', 'SQ', 'SBARQ', 'LST', 'INTJ', 'X', 'UCP', 'CONJP', 'NNP', 'CD', 'JJ',
30
- 'VBD', 'WHADJP', 'PRP', 'RRC', 'NNS', 'SYM', 'CC']
31
-
32
- openai.api_key = " "
33
-
34
- # determinant vs. determiner
35
- # https://wikidiff.com/determiner/determinant
36
- ents_prompt = ['Noun','Verb','Adjective','Adverb','Preposition/Subord','Coordinating Conjunction',# 'Cardinal Number',
37
- 'Determiner',
38
- 'Noun Phrase','Verb Phrase','Adjective Phrase','Adverb Phrase','Preposition Phrase','Conjunction Phrase','Coordinate Phrase','Quantitave Phrase','Complex Nominal',
39
- 'Clause','Dependent Clause','Fragment Clause','T-unit','Complex T-unit',# 'Fragment T-unit',
40
- ][7:]
41
- ents = ['NN', 'VB', 'JJ', 'RB', 'IN', 'CC', 'DT', 'NP', 'VP', 'ADJP', 'ADVP', 'PP', 'CONJP', 'CP', 'QP', 'CN', 'C', 'DC', 'FC', 'T', 'CT'][7:]
42
-
43
-
44
- ents_prompt_uni_tags = ['Verb', 'Noun', 'Pronoun', 'Adjective', 'Adverb', 'Preposition and Postposition', 'Coordinating Conjunction',
45
- 'Determiner', 'Cardinal Number', 'Particles or other function words',
46
- 'Words that cannot be assigned a POS tag', 'Punctuation']
47
-
48
- ents = uni_tags + ents
49
- ents_prompt = ents_prompt_uni_tags + ents_prompt
50
-
51
- for i, j in zip(ents, ents_prompt):
52
- print(i, j)
53
-
54
- model_mapping = {
55
- 'gpt3.5': 'gpt2',
56
- #'vicuna-7b': 'lmsys/vicuna-7b-v1.3',
57
- #'llama-7b': './llama/hf/7B',
58
- }
59
-
60
- with open('sample_uniform_1k_2.txt', 'r') as f:
61
- selected_idx = f.readlines()
62
- selected_idx = [int(i.strip()) for i in selected_idx]#[s:e]
63
-
64
- ptb = []
65
- with open('ptb.jsonl', 'r') as f:
66
- for l in f:
67
- ptb.append(json.loads(l))
68
-
69
-
70
- ## Prompt 1
71
- 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: "{}"'''
72
- template_single = '''Please output any <{}> in the following sentence one per line without any additional text: "{}"'''
73
-
74
- ## Prompt 2
75
- prompt2_pos = '''Please pos tag the following sentence using Universal POS tag set without generating any additional text: {}'''
76
- prompt2_chunk = '''Please do sentence chunking for the following sentence as in CoNLL 2000 shared task without generating any addtional text: {}'''
77
- prompt2_parse = '''Generate textual representation of the constituency parse tree of the following sentence using Penn TreeBank tag set without outputing any additional text: {}'''
78
-
79
- prompt2_chunk = '''Please chunk the following sentence in CoNLL 2000 format with BIO tags without outputing any additional text: {}'''
80
-
81
- ## Prompt 3
82
- with open('demonstration_3_42_pos.txt', 'r') as f:
83
- demon_pos = f.read()
84
- with open('demonstration_3_42_chunk.txt', 'r') as f:
85
- demon_chunk = f.read()
86
- with open('demonstration_3_42_parse.txt', 'r') as f:
87
- demon_parse = f.read()
88
-
89
- # Your existing code
90
- theme = gr.themes.Soft()
91
-
92
- # issue get request for gpt 3.5
93
- gpt_pipeline = pipeline(task="text2text-generation", model="gpt2")
94
- #vicuna7b_pipeline = pipeline(task="text2text-generation", model="lmsys/vicuna-7b-v1.3")
95
- #llama7b_pipeline = pipeline(task="text2text-generation", model="./llama/hf/7B")
96
 
97
- # Dropdown options for model and task
98
- model_options = list(model_mapping.keys())
99
- task_options = ['POS', 'Chunking'] # remove parsing
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_POS = 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_Chunk = gr.Textbox(id="prompt_Chunk", 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_Chunk], outputs=[model1_S1_output, model1_S1_output, model1_S1_output])
172
- send_button_Chunk.click(process_text, inputs=["Chunk Tab", prompt_POS], outputs=[model1_S1_output, model1_S1_output, model1_S1_output])
173
 
174
- demo.launch()
 
 
 
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ # Load the models and tokenizers
5
+ gpt35_model = AutoModelForCausalLM.from_pretrained("gpt-3.5-turbo-0613")
6
+ gpt35_tokenizer = AutoTokenizer.from_pretrained("gpt-3.5-turbo-0613")
7
 
8
+ vicuna_model = AutoModelForCausalLM.from_pretrained("lmsys/vicuna-7b-v1.3")
9
+ vicuna_tokenizer = AutoTokenizer.from_pretrained("lmsys/vicuna-7b-v1.3")
10
 
11
+ llama_model = AutoModelForCausalLM.from_pretrained("./llama/hf/7B")
12
+ llama_tokenizer = AutoTokenizer.from_pretrained("./llama/hf/7B")
 
 
 
 
13
 
14
+ # Define the function for generating responses
15
+ def generate_response(model, tokenizer, prompt):
16
+ inputs = tokenizer(prompt, return_tensors="pt")
17
+ outputs = model.generate(**inputs, max_length=100, pad_token_id=tokenizer.eos_token_id)
18
+ response = tokenizer.decode(outputs[0])
19
+ return response
 
 
 
20
 
21
+ # Define the Gradio interface
22
+ def chatbot_interface(prompt):
23
+ gpt35_response = generate_response(gpt35_model, gpt35_tokenizer, prompt)
24
+ vicuna_response = generate_response(vicuna_model, vicuna_tokenizer, prompt)
25
+ llama_response = generate_response(llama_model, llama_tokenizer, prompt)
26
 
27
+ return {"GPT-3.5": gpt35_response, "Vicuna-7B": vicuna_response, "Llama-7B": llama_response}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ iface = gr.Interface(fn=chatbot_interface,
30
+ inputs="text",
31
+ outputs="panel",
32
+ title="Chatbot with Three Models")
33
 
34
+ iface.launch()
placeholder.py ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ demo = gr.Blocks()
21
+
22
+ uni_tags = list(_UNIVERSAL_TAGS)
23
+ uni_tags[-1] = 'PUNC'
24
+
25
+ bio_tags = ['B', 'I', 'O']
26
+ chunk_tags = ['ADJP', 'ADVP', 'CONJP', 'INTJ', 'LST', 'NP', 'O', 'PP', 'PRT', 'SBAR', 'UCP', 'VP']
27
+
28
+ syntags = ['NP', 'S', 'VP', 'ADJP', 'ADVP', 'SBAR', 'TOP', 'PP', 'POS', 'NAC', "''", 'SINV', 'PRN', 'QP', 'WHNP', 'RB', 'FRAG',
29
+ 'WHADVP', 'NX', 'PRT', 'VBZ', 'VBP', 'MD', 'NN', 'WHPP', 'SQ', 'SBARQ', 'LST', 'INTJ', 'X', 'UCP', 'CONJP', 'NNP', 'CD', 'JJ',
30
+ 'VBD', 'WHADJP', 'PRP', 'RRC', 'NNS', 'SYM', 'CC']
31
+
32
+ openai.api_key = " "
33
+
34
+ # determinant vs. determiner
35
+ # https://wikidiff.com/determiner/determinant
36
+ ents_prompt = ['Noun','Verb','Adjective','Adverb','Preposition/Subord','Coordinating Conjunction',# 'Cardinal Number',
37
+ 'Determiner',
38
+ 'Noun Phrase','Verb Phrase','Adjective Phrase','Adverb Phrase','Preposition Phrase','Conjunction Phrase','Coordinate Phrase','Quantitave Phrase','Complex Nominal',
39
+ 'Clause','Dependent Clause','Fragment Clause','T-unit','Complex T-unit',# 'Fragment T-unit',
40
+ ][7:]
41
+ ents = ['NN', 'VB', 'JJ', 'RB', 'IN', 'CC', 'DT', 'NP', 'VP', 'ADJP', 'ADVP', 'PP', 'CONJP', 'CP', 'QP', 'CN', 'C', 'DC', 'FC', 'T', 'CT'][7:]
42
+
43
+
44
+ ents_prompt_uni_tags = ['Verb', 'Noun', 'Pronoun', 'Adjective', 'Adverb', 'Preposition and Postposition', 'Coordinating Conjunction',
45
+ 'Determiner', 'Cardinal Number', 'Particles or other function words',
46
+ 'Words that cannot be assigned a POS tag', 'Punctuation']
47
+
48
+ ents = uni_tags + ents
49
+ ents_prompt = ents_prompt_uni_tags + ents_prompt
50
+
51
+ for i, j in zip(ents, ents_prompt):
52
+ print(i, j)
53
+
54
+ model_mapping = {
55
+ 'gpt3.5': 'gpt2',
56
+ #'vicuna-7b': 'lmsys/vicuna-7b-v1.3',
57
+ #'llama-7b': './llama/hf/7B',
58
+ }
59
+
60
+ with open('sample_uniform_1k_2.txt', 'r') as f:
61
+ selected_idx = f.readlines()
62
+ selected_idx = [int(i.strip()) for i in selected_idx]#[s:e]
63
+
64
+ ptb = []
65
+ with open('ptb.jsonl', 'r') as f:
66
+ for l in f:
67
+ ptb.append(json.loads(l))
68
+
69
+
70
+ ## Prompt 1
71
+ 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: "{}"'''
72
+ template_single = '''Please output any <{}> in the following sentence one per line without any additional text: "{}"'''
73
+
74
+ ## Prompt 2
75
+ prompt2_pos = '''Please pos tag the following sentence using Universal POS tag set without generating any additional text: {}'''
76
+ prompt2_chunk = '''Please do sentence chunking for the following sentence as in CoNLL 2000 shared task without generating any addtional text: {}'''
77
+ prompt2_parse = '''Generate textual representation of the constituency parse tree of the following sentence using Penn TreeBank tag set without outputing any additional text: {}'''
78
+
79
+ prompt2_chunk = '''Please chunk the following sentence in CoNLL 2000 format with BIO tags without outputing any additional text: {}'''
80
+
81
+ ## Prompt 3
82
+ with open('demonstration_3_42_pos.txt', 'r') as f:
83
+ demon_pos = f.read()
84
+ with open('demonstration_3_42_chunk.txt', 'r') as f:
85
+ demon_chunk = f.read()
86
+ with open('demonstration_3_42_parse.txt', 'r') as f:
87
+ demon_parse = f.read()
88
+
89
+ # Your existing code
90
+ theme = gr.themes.Soft()
91
+
92
+ # issue get request for gpt 3.5
93
+ gpt_pipeline = pipeline(task="text2text-generation", model="gpt2")
94
+ #vicuna7b_pipeline = pipeline(task="text2text-generation", model="lmsys/vicuna-7b-v1.3")
95
+ #llama7b_pipeline = pipeline(task="text2text-generation", model="./llama/hf/7B")
96
+
97
+ # Dropdown options for model and task
98
+ model_options = list(model_mapping.keys())
99
+ task_options = ['POS', 'Chunking'] # remove parsing
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_POS = 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_Chunk = gr.Textbox(id="prompt_Chunk", 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_Chunk], outputs=[model1_S1_output, model1_S1_output, model1_S1_output])
172
+ send_button_Chunk.click(process_text, inputs=["Chunk Tab", prompt_POS], outputs=[model1_S1_output, model1_S1_output, model1_S1_output])
173
+
174
+ demo.launch()
175
+