zmbfeng commited on
Commit
d72eb57
·
1 Parent(s): 610743f

simplified

Browse files
Files changed (1) hide show
  1. app.py +34 -161
app.py CHANGED
@@ -8,7 +8,10 @@ from transformers import pipeline
8
  from transformers import GPT2Tokenizer, GPT2LMHeadModel,set_seed
9
  import datetime
10
 
 
11
  login(os.environ["HF_TOKEN"])
 
 
12
  #https://huggingface.co/facebook/opt-1.3b
13
  #generator = pipeline('text-generation', model="microsoft/DialoGPT-medium")
14
 
@@ -19,98 +22,56 @@ print("loading models")
19
  tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-medium')
20
  original_model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium')
21
  untethered_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240225_epochs_500')
22
- untethered_paraphrased_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240227_epochs_350')
23
- default_num_return_sequences=5
24
- default_temperature=0.5
25
- default_repetition_penalty=1.5
26
- default_top_p=2
27
- default_top_k=50
28
- default_do_sample=True
29
  default_seed=43
30
- def create_response(input_str,
31
- # num_beams,
32
- num_return_sequences,
33
  temperature,
34
- repetition_penalty,
35
- top_p,
36
- top_k,
37
- do_sample,
38
- seed,
39
  model_name):
40
  print("input_str="+input_str)
41
  print("model_name="+str(model_name))
42
- # num_beams = int(num_beams)
43
- # print("num_beams=" + str(num_beams))
44
- num_return_sequences=int(num_return_sequences)
45
- print("num_return_sequences" + str(num_return_sequences))
46
- print("top_p" + str(top_p))
47
- top_k=int(top_k)
48
- print("top_k" + str(top_k))
49
- print("repetition_penalty" + str(repetition_penalty))
50
  print("temperature" + str(temperature))
51
- print("do_sample" + str(do_sample))
52
- if not do_sample:
53
- num_beams = 1
54
- print("num_beams=" + str(num_beams))
55
  seed=int(seed)
56
- print("seed" + str(seed))
57
  input_ids = tokenizer.encode(input_str + tokenizer.eos_token, return_tensors="pt")
 
 
58
  #= encoded["input_ids"]
59
  #attention_mask = encoded["attention_mask"]
60
 
61
  if seed != -1:
62
- set_seed(seed)
63
- if model_name == "original_model":
64
- output = original_model.generate(input_ids,do_sample=do_sample, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences,return_dict_in_generate=True, output_scores=True )
65
- transition_scores = original_model.compute_transition_scores(output.sequences, output.scores,
66
- normalize_logits=False)
67
-
68
- elif model_name == "untethered_model":
69
- output = untethered_model.generate(input_ids, do_sample=do_sample, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences,return_dict_in_generate=True, output_scores=True )
70
- transition_scores = untethered_model.compute_transition_scores(output.sequences, output.scores,
71
- normalize_logits=False)
72
- elif model_name == "untethered_paraphrased_model":
73
- output = untethered_paraphrased_model.generate(input_ids, do_sample=do_sample, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences,return_dict_in_generate=True, output_scores=True )
74
- transition_scores = untethered_paraphrased_model.compute_transition_scores(output.sequences, output.scores,
75
- normalize_logits=False)
76
  else:
77
- output = original_model.generate(input_ids,do_sample=do_sample, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences,return_dict_in_generate=True, output_scores=True )
78
- transition_scores = original_model.compute_transition_scores(output.sequences, output.scores,
79
- normalize_logits=False)
80
- score_list = []
81
- for scores in transition_scores:
82
- # exp_tensor_list = torch.exp(scores)
83
- # print(scores)
84
- # print(exp_tensor_list)
85
- sum_exp_tensor_list = torch.sum(scores)
86
- len_exp_tensor_list = torch.numel(scores)
87
- average_exp_tensor_list = sum_exp_tensor_list / len_exp_tensor_list
88
- print(average_exp_tensor_list)
89
- score_list.append(round(average_exp_tensor_list.item(),2))
90
  outputs = model_name+" generated <br>"
91
- for index, output_id in enumerate(output.sequences):
92
- sentence = tokenizer.decode(output_id, skip_special_tokens=True)
93
- #print(sentence + " score:" + str(score_list[index]))
94
- outputs = outputs + sentence + " score:" + str(score_list[index]) +"<br/>"
95
- # for output_id in output_ids:
96
- # output = tokenizer.decode(output_id, skip_special_tokens=True)
97
- # outputs=outputs+output+"<br/>"
98
  return outputs
99
 
100
 
101
  common_examples_string="<br/>Sample Inputs:<br/>What is death?<br/>One of the best teachers in all of life turns out to be what?<br/>what is your most meaningful relationship?<br/>What actually gives life meaning?<br/>"
102
- common_examples=[
103
- ["What is death?",default_num_return_sequences,default_temperature,default_repetition_penalty,default_top_p,default_top_k,default_do_sample], # The first example
104
- ["One of the best teachers in all of life turns out to be what?",default_num_return_sequences,default_temperature,default_repetition_penalty,default_top_p,default_top_k,default_do_sample], # The second example
105
- ["what is your most meaningful relationship?",default_num_return_sequences,default_temperature,default_repetition_penalty,default_top_p,default_top_k,default_do_sample], # The third example
106
- ["What actually gives life meaning?",default_num_return_sequences,default_temperature,default_repetition_penalty,default_top_p,default_top_k,default_do_sample]
107
  ]
108
  examples = copy.deepcopy(common_examples)
109
  print(examples)
110
  for example in examples:
111
  example.append("original_model")
112
- print(examples)
113
- interface_original = gr.Interface(fn=create_response,
114
  title="original",
115
  description="original language model, no fine tuning"+common_examples_string,
116
  #examples=examples,
@@ -118,31 +79,11 @@ interface_original = gr.Interface(fn=create_response,
118
  gr.Textbox(label="input text here", lines=3),
119
  # gr.Number(label="num_beams (integer) explores the specified number of possible outputs and selects the most " +
120
  # "likely ones (specified in num_beams)", value=7),
121
- gr.Number(label="num_return_sequences (integer) the number of outputs selected from num_beams possible output",
122
- value=default_num_return_sequences),
123
  gr.Number(
124
  label="temperature (decimal) controls the creativity or randomness of the output. A higher temperature" +
125
- " (e.g., 0.9) results in more diverse and creative output, while a lower temperature (e.g., 0.2)" +
126
  " makes the output more deterministic and focused",
127
  value=default_temperature),
128
- gr.Number(label="repetition_penalty (decimal) penalizes words that have already appeared in the output, " +
129
- "making them less likely to be generated again. A higher repetition_penalty (e.g., 1.5) results" +
130
- "in more varied and non-repetitive output.",
131
- value=default_repetition_penalty),
132
- gr.Number(label="top_p (decimal) the model will only consider the words that have a high enough probability" +
133
- " to reach a certain threshold",
134
- value=default_top_p),
135
- gr.Number(label="top_k (integer) The number of highest probability vocabulary word will be considered" +
136
- "This means that only the tokens with the highest probabilities are considered for sampling" +
137
- "This reduces the diversity of the generated sequences, "+
138
- "but also makes them more likely to be coherent and fluent.",
139
- value=default_top_k),
140
- gr.Checkbox(label="do_sample. If is set to False, num_return_sequences must be 1 because the generate function will use greedy decoding, " +
141
- "which means that it will select the word with the highest probability at each step. " +
142
- "This results in a deterministic and fluent output, but it might also lack diversity and creativity" +
143
- "If is set to True, the generate function will use stochastic sampling, which means that it will randomly" +
144
- " select a word from the probability distribution at each step. This results in a more diverse and creative" +
145
- " output, but it might also introduce errors and inconsistencies ", value=default_do_sample),
146
  gr.Number(
147
  label="seed (integer) random seed, set to -1 to use a random seed everytime",
148
  value=default_seed),
@@ -157,39 +98,17 @@ for example in examples:
157
  print(examples)
158
 
159
 
160
- interface_untethered_model = gr.Interface(fn=create_response,
161
  title="untethered model",
162
  description="language model fine tuned with'The Untethered Soul' chapter 17"+common_examples_string,
163
  #examples=examples,
164
  inputs=[
165
  gr.Textbox(label="input text here", lines=3),
166
- # gr.Number(label="num_beams (integer) explores the specified number of possible outputs and selects the most " +
167
- # "likely ones (specified in num_beams)", value=7),
168
- gr.Number(label="num_return_sequences (integer) the number of outputs selected from num_beams possible output",
169
- value=default_num_return_sequences),
170
  gr.Number(
171
  label="temperature (decimal) controls the creativity or randomness of the output. A higher temperature" +
172
- " (e.g., 0.9) results in more diverse and creative output, while a lower temperature (e.g., 0.2)" +
173
  " makes the output more deterministic and focused",
174
  value=default_temperature),
175
- gr.Number(label="repetition_penalty (decimal) penalizes words that have already appeared in the output, " +
176
- "making them less likely to be generated again. A higher repetition_penalty (e.g., 1.5) results" +
177
- "in more varied and non-repetitive output.",
178
- value=default_repetition_penalty),
179
- gr.Number(label="top_p (decimal) the model will only consider the words that have a high enough probability" +
180
- " to reach a certain threshold",
181
- value=default_top_p),
182
- gr.Number(label="top_k (integer) The number of highest probability vocabulary word will be considered" +
183
- "This means that only the tokens with the highest probabilities are considered for sampling" +
184
- "This reduces the diversity of the generated sequences, "+
185
- "but also makes them more likely to be coherent and fluent.",
186
- value=default_top_k),
187
- gr.Checkbox(label="do_sample. If is set to False, num_return_sequences must be 1 because the generate function will use greedy decoding, " +
188
- "which means that it will select the word with the highest probability at each step. " +
189
- "This results in a deterministic and fluent output, but it might also lack diversity and creativity" +
190
- "If is set to True, the generate function will use stochastic sampling, which means that it will randomly" +
191
- " select a word from the probability distribution at each step. This results in a more diverse and creative" +
192
- " output, but it might also introduce errors and inconsistencies ", value=default_do_sample),
193
  gr.Number(
194
  label="seed (integer) random seed, set to -1 to use a random seed everytime",
195
  value=default_seed),
@@ -198,54 +117,8 @@ interface_untethered_model = gr.Interface(fn=create_response,
198
  outputs="html"
199
  )
200
 
201
- examples = copy.deepcopy(common_examples)
202
- print(examples)
203
- for example in examples:
204
- example.append("untethered_paraphrased_model")
205
- print(examples)
206
- interface_untethered_paraphrased_model = gr.Interface(fn=create_response,
207
- title="untethered paraphrased_model",
208
- description="language model fine tuned with'The Untethered Soul' chapter 17 paraphrased"+common_examples_string,
209
- #examples=examples,
210
- inputs=[
211
- gr.Textbox(label="input text here", lines=3),
212
- # gr.Number(label="num_beams (integer) explores the specified number of possible outputs and selects the most " +
213
- # "likely ones (specified in num_beams)", value=7),
214
- gr.Number(label="num_return_sequences (integer) the number of outputs selected from num_beams possible output",
215
- value=default_num_return_sequences),
216
- gr.Number(
217
- label="temperature (decimal) controls the creativity or randomness of the output. A higher temperature" +
218
- " (e.g., 0.9) results in more diverse and creative output, while a lower temperature (e.g., 0.2)" +
219
- " makes the output more deterministic and focused",
220
- value=default_temperature),
221
- gr.Number(label="repetition_penalty (decimal) penalizes words that have already appeared in the output, " +
222
- "making them less likely to be generated again. A higher repetition_penalty (e.g., 1.5) results" +
223
- "in more varied and non-repetitive output.",
224
- value=default_repetition_penalty),
225
- gr.Number(label="top_p (decimal) the model will only consider the words that have a high enough probability" +
226
- " to reach a certain threshold",
227
- value=default_top_p),
228
- gr.Number(label="top_k (integer) The number of highest probability vocabulary word will be considered" +
229
- "This means that only the tokens with the highest probabilities are considered for sampling" +
230
- "This reduces the diversity of the generated sequences, "+
231
- "but also makes them more likely to be coherent and fluent.",
232
- value=default_top_k),
233
- gr.Checkbox(label="do_sample. If is set to False, num_return_sequences must be 1 because the generate function will use greedy decoding, " +
234
- "which means that it will select the word with the highest probability at each step. " +
235
- "This results in a deterministic and fluent output, but it might also lack diversity and creativity" +
236
- "If is set to True, the generate function will use stochastic sampling, which means that it will randomly" +
237
- " select a word from the probability distribution at each step. This results in a more diverse and creative" +
238
- " output, but it might also introduce errors and inconsistencies ", value=default_do_sample),
239
- gr.Number(
240
- label="seed (integer) random seed, set to -1 to use a random seed everytime",
241
- value=default_seed),
242
- gr.Textbox(label="model", lines=3, value="untethered_paraphrased_model",visible=False)
243
- ],
244
- outputs= "html"
245
- )
246
-
247
 
248
 
249
- demo = gr.TabbedInterface([interface_original, interface_untethered_model, interface_untethered_paraphrased_model], ["Original", "Untethered", "Untethered paraphrased"])
250
 
251
  demo.launch()
 
8
  from transformers import GPT2Tokenizer, GPT2LMHeadModel,set_seed
9
  import datetime
10
 
11
+
12
  login(os.environ["HF_TOKEN"])
13
+
14
+
15
  #https://huggingface.co/facebook/opt-1.3b
16
  #generator = pipeline('text-generation', model="microsoft/DialoGPT-medium")
17
 
 
22
  tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-medium')
23
  original_model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium')
24
  untethered_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240225_epochs_500')
25
+ # tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-medium',cache_dir="G:\My Drive\Avatar\language_models_windows")
26
+ # original_model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium',cache_dir="G:\My Drive\Avatar\language_models_windows")
27
+ # untethered_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240225_epochs_500',cache_dir="G:\My Drive\Avatar\language_models_windows")
28
+
29
+ default_temperature=0.01
 
 
30
  default_seed=43
31
+ def create_response(input_str,
 
 
32
  temperature,
33
+ seed,
 
 
 
 
34
  model_name):
35
  print("input_str="+input_str)
36
  print("model_name="+str(model_name))
 
 
 
 
 
 
 
 
37
  print("temperature" + str(temperature))
 
 
 
 
38
  seed=int(seed)
39
+ print("seed" + str(seed))
40
  input_ids = tokenizer.encode(input_str + tokenizer.eos_token, return_tensors="pt")
41
+
42
+
43
  #= encoded["input_ids"]
44
  #attention_mask = encoded["attention_mask"]
45
 
46
  if seed != -1:
47
+ set_seed(seed)
48
+ if model_name == "original_model":
49
+ output = original_model.generate(input_ids, max_length=100, temperature=temperature, pad_token_id=tokenizer.eos_token_id)
50
+
51
+ #elif model_name == "untethered_model":
 
 
 
 
 
 
 
 
 
52
  else:
53
+ output = original_model.generate(input_ids, max_length=100, temperature=temperature, pad_token_id=tokenizer.eos_token_id )
54
+
 
 
 
 
 
 
 
 
 
 
 
55
  outputs = model_name+" generated <br>"
56
+ sentence = tokenizer.decode(output[0], skip_special_tokens=True)
57
+ outputs = outputs + sentence+ "<br/>"
58
+
 
 
 
 
59
  return outputs
60
 
61
 
62
  common_examples_string="<br/>Sample Inputs:<br/>What is death?<br/>One of the best teachers in all of life turns out to be what?<br/>what is your most meaningful relationship?<br/>What actually gives life meaning?<br/>"
63
+ common_examples=[
64
+ ["What is death?",default_temperature], # The first example
65
+ ["One of the best teachers in all of life turns out to be what?",default_temperature], # The second example
66
+ ["what is your most meaningful relationship?",default_temperature], # The third example
67
+ ["What actually gives life meaning?",default_temperature]
68
  ]
69
  examples = copy.deepcopy(common_examples)
70
  print(examples)
71
  for example in examples:
72
  example.append("original_model")
73
+ print(examples)
74
+ interface_original = gr.Interface(fn=create_response,
75
  title="original",
76
  description="original language model, no fine tuning"+common_examples_string,
77
  #examples=examples,
 
79
  gr.Textbox(label="input text here", lines=3),
80
  # gr.Number(label="num_beams (integer) explores the specified number of possible outputs and selects the most " +
81
  # "likely ones (specified in num_beams)", value=7),
 
 
82
  gr.Number(
83
  label="temperature (decimal) controls the creativity or randomness of the output. A higher temperature" +
84
+ " (e.g., 1.6) results in more diverse and creative output, while a lower temperature (e.g., 0.02)" +
85
  " makes the output more deterministic and focused",
86
  value=default_temperature),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  gr.Number(
88
  label="seed (integer) random seed, set to -1 to use a random seed everytime",
89
  value=default_seed),
 
98
  print(examples)
99
 
100
 
101
+ interface_untethered_model = gr.Interface(fn=create_response,
102
  title="untethered model",
103
  description="language model fine tuned with'The Untethered Soul' chapter 17"+common_examples_string,
104
  #examples=examples,
105
  inputs=[
106
  gr.Textbox(label="input text here", lines=3),
 
 
 
 
107
  gr.Number(
108
  label="temperature (decimal) controls the creativity or randomness of the output. A higher temperature" +
109
+ " (e.g., 1.6) results in more diverse and creative output, while a lower temperature (e.g., 0.02)" +
110
  " makes the output more deterministic and focused",
111
  value=default_temperature),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  gr.Number(
113
  label="seed (integer) random seed, set to -1 to use a random seed everytime",
114
  value=default_seed),
 
117
  outputs="html"
118
  )
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
 
122
+ demo = gr.TabbedInterface([interface_original, interface_untethered_model], ["Original", "Untethered"])
123
 
124
  demo.launch()