Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,56 +12,9 @@ original_model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium')
|
|
12 |
untethered_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240225_epochs_500')
|
13 |
untethered_paraphrased_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240227_epochs_350')
|
14 |
|
15 |
-
def create_response_untethered_paraphrased(input_str,
|
16 |
-
# num_beams,
|
17 |
-
num_return_sequences,
|
18 |
-
temperature,
|
19 |
-
repetition_penalty,
|
20 |
-
top_p,
|
21 |
-
# top_k,
|
22 |
-
do_sample):
|
23 |
-
print("input_str="+input_str)
|
24 |
-
# num_beams = int(num_beams)
|
25 |
-
# print("num_beams=" + str(num_beams))
|
26 |
-
num_return_sequences=int(num_return_sequences)
|
27 |
-
print("num_return_sequences" + str(num_return_sequences))
|
28 |
-
print("top_p" + str(top_p))
|
29 |
-
# top_k=int(top_k)
|
30 |
-
#print("top_k" + str(top_k))
|
31 |
-
print("repetition_penalty" + str(repetition_penalty))
|
32 |
-
print("temperature" + str(temperature))
|
33 |
-
print("do_sample" + str(do_sample))
|
34 |
-
if not do_sample:
|
35 |
-
num_beams = 1
|
36 |
-
print("num_beams=" + str(num_beams))
|
37 |
-
|
38 |
-
#output_raw= generator(input_str)
|
39 |
-
"""print (output_raw)"""
|
40 |
-
|
41 |
-
#output_str = output_raw[0]['generated_text']
|
42 |
-
#output_str = output_str.replace("\n", "")
|
43 |
-
#output_str = output_str.replace(input_str, "")
|
44 |
-
#output_str = tokenizer.decode(model.generate(**tokenizer("What are John West's hobbies?"+tokenizer.eos_token,return_tensors="pt",max_length=200))[0])
|
45 |
-
# output_str = tokenizer.decode(original_model.generate(**tokenizer(input_str+tokenizer.eos_token,return_tensors="pt",max_length=200),
|
46 |
-
# num_beams=num_beams,
|
47 |
-
# num_return_sequences=num_return_sequences)[0])
|
48 |
-
|
49 |
-
#input_ids = tokenizer.encode(input_str + tokenizer.eos_token, return_tensors="pt")
|
50 |
-
encoded = tokenizer.encode_plus(input_str + tokenizer.eos_token, return_tensors="pt")
|
51 |
-
input_ids = encoded["input_ids"]
|
52 |
-
attention_mask = encoded["attention_mask"]
|
53 |
-
#output_ids = fine_tuned_model.generate(input_ids,do_sample=True, max_length=100, temperature=0.2, top_p=0.9, repetition_penalty=1.5,num_return_sequences=6)
|
54 |
-
#output_ids = untethered_paraphrased_model.generate(input_ids,do_sample=do_sample, max_length=100, temperature=temperature, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences, num_beams = num_beams)
|
55 |
-
output_ids = untethered_paraphrased_model.generate(input_ids,pad_token_id=tokenizer.eos_token_id,do_sample=True,attention_mask=attention_mask, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences)
|
56 |
-
outputs = ""
|
57 |
-
for output_id in output_ids:
|
58 |
-
output = tokenizer.decode(output_id, skip_special_tokens=True)
|
59 |
-
outputs=outputs+output+"<br/>"
|
60 |
-
return outputs
|
61 |
-
|
62 |
|
63 |
|
64 |
-
def
|
65 |
# num_beams,
|
66 |
num_return_sequences,
|
67 |
temperature,
|
@@ -85,100 +38,28 @@ def create_response_untethered(input_str,
|
|
85 |
if not do_sample:
|
86 |
num_beams = 1
|
87 |
print("num_beams=" + str(num_beams))
|
88 |
-
|
89 |
-
#output_raw= generator(input_str)
|
90 |
-
"""print (output_raw)"""
|
91 |
-
|
92 |
-
#output_str = output_raw[0]['generated_text']
|
93 |
-
#output_str = output_str.replace("\n", "")
|
94 |
-
#output_str = output_str.replace(input_str, "")
|
95 |
-
#output_str = tokenizer.decode(model.generate(**tokenizer("What are John West's hobbies?"+tokenizer.eos_token,return_tensors="pt",max_length=200))[0])
|
96 |
-
# output_str = tokenizer.decode(original_model.generate(**tokenizer(input_str+tokenizer.eos_token,return_tensors="pt",max_length=200),
|
97 |
-
# num_beams=num_beams,
|
98 |
-
# num_return_sequences=num_return_sequences)[0])
|
99 |
-
|
100 |
-
#input_ids = tokenizer.encode(input_str + tokenizer.eos_token, return_tensors="pt")
|
101 |
encoded = tokenizer.encode_plus(input_str + tokenizer.eos_token, return_tensors="pt")
|
102 |
input_ids = encoded["input_ids"]
|
103 |
attention_mask = encoded["attention_mask"]
|
104 |
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
def create_response_original(input_str,
|
115 |
-
# num_beams,
|
116 |
-
num_return_sequences,
|
117 |
-
temperature,
|
118 |
-
repetition_penalty,
|
119 |
-
top_p,
|
120 |
-
# top_k,
|
121 |
-
do_sample):
|
122 |
-
print("input_str="+input_str)
|
123 |
-
# num_beams = int(num_beams)
|
124 |
-
# print("num_beams=" + str(num_beams))
|
125 |
-
num_return_sequences=int(num_return_sequences)
|
126 |
-
print("num_return_sequences" + str(num_return_sequences))
|
127 |
-
print("top_p" + str(top_p))
|
128 |
-
# top_k=int(top_k)
|
129 |
-
# print("top_k" + str(top_k))
|
130 |
-
print("repetition_penalty" + str(repetition_penalty))
|
131 |
-
print("temperature" + str(temperature))
|
132 |
-
print("do_sample" + str(do_sample))
|
133 |
-
if not do_sample:
|
134 |
-
num_beams = 1
|
135 |
-
print("num_beams=" + str(num_beams))
|
136 |
|
137 |
-
|
138 |
-
"""print (output_raw)"""
|
139 |
-
|
140 |
-
#output_str = output_raw[0]['generated_text']
|
141 |
-
#output_str = output_str.replace("\n", "")
|
142 |
-
#output_str = output_str.replace(input_str, "")
|
143 |
-
#output_str = tokenizer.decode(model.generate(**tokenizer("What are John West's hobbies?"+tokenizer.eos_token,return_tensors="pt",max_length=200))[0])
|
144 |
-
# output_str = tokenizer.decode(original_model.generate(**tokenizer(input_str+tokenizer.eos_token,return_tensors="pt",max_length=200),
|
145 |
-
# num_beams=num_beams,
|
146 |
-
# num_return_sequences=num_return_sequences)[0])
|
147 |
-
|
148 |
-
#input_ids = tokenizer.encode(input_str + tokenizer.eos_token, return_tensors="pt")
|
149 |
-
encoded = tokenizer.encode_plus(input_str + tokenizer.eos_token, return_tensors="pt")
|
150 |
-
input_ids = encoded["input_ids"]
|
151 |
-
attention_mask = encoded["attention_mask"]
|
152 |
-
#output_ids = fine_tuned_model.generate(input_ids,do_sample=True, max_length=100, temperature=0.2, top_p=0.9, repetition_penalty=1.5,num_return_sequences=6)
|
153 |
-
output_ids = original_model.generate(input_ids,pad_token_id=tokenizer.eos_token_id,do_sample=do_sample,attention_mask=attention_mask, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences)
|
154 |
-
outputs = ""
|
155 |
for output_id in output_ids:
|
156 |
output = tokenizer.decode(output_id, skip_special_tokens=True)
|
157 |
-
outputs=outputs+output+"
|
158 |
return outputs
|
159 |
|
160 |
-
|
161 |
-
#output_raw= generator(input_str)
|
162 |
-
"""print (output_raw)"""
|
163 |
-
|
164 |
-
#output_str = output_raw[0]['generated_text']
|
165 |
-
#output_str = output_str.replace("\n", "")
|
166 |
-
#output_str = output_str.replace(input_str, "")
|
167 |
-
#output_str = tokenizer.decode(model.generate(**tokenizer("What are John West's hobbies?"+tokenizer.eos_token,return_tensors="pt",max_length=200))[0])
|
168 |
-
output_str = tokenizer.decode(fine_tuned_model.generate(**tokenizer(input_str+tokenizer.eos_token,return_tensors="pt",max_length=200))[0])
|
169 |
-
return (output_str)
|
170 |
-
|
171 |
-
|
172 |
-
interface1 = gr.Interface(fn=create_response_original,
|
173 |
-
title="original",
|
174 |
-
description="original language model, no fine tuning",
|
175 |
-
examples=[
|
176 |
-
["What is death?",5,0.2,1.5,0.9,True], # The first example
|
177 |
-
["One of the best teachers in all of life turns out to be what?",5,0.2,1.5,0.9,True], # The second example
|
178 |
-
["what is your most meaningful relationship?",5,0.2,1.5,0.9,True], # The third example
|
179 |
-
["What actually gives life meaning?",5,0.2,1.5,0.9,True]
|
180 |
-
],
|
181 |
-
inputs=[
|
182 |
gr.Textbox(label="input text here", lines=3),
|
183 |
# gr.Number(label="num_beams (integer) explores the specified number of possible outputs and selects the most " +
|
184 |
# "likely ones (specified in num_beams)", value=7),
|
@@ -207,101 +88,49 @@ interface1 = gr.Interface(fn=create_response_original,
|
|
207 |
"If is set to True, the generate function will use stochastic sampling, which means that it will randomly" +
|
208 |
" select a word from the probability distribution at each step. This results in a more diverse and creative" +
|
209 |
" output, but it might also introduce errors and inconsistencies ", value=True)
|
210 |
-
]
|
211 |
-
|
212 |
-
|
213 |
-
interface2 = gr.Interface(fn=create_response_untethered,
|
214 |
-
title="untethered",
|
215 |
-
description="untethered fine tuning",
|
216 |
-
examples=[
|
217 |
-
["What is death?",5,0.2,1.5,0.9,True,"untethered_model_name"], # The first example
|
218 |
-
["One of the best teachers in all of life turns out to be what?",5,0.2,1.5,0.9,True,"untethered_model_name"], # The second example
|
219 |
-
["what is your most meaningful relationship?",5,0.2,1.5,0.9,True,"untethered_model_name"], # The third example
|
220 |
-
["What actually gives life meaning?",5,0.2,1.5,0.9,True,"untethered_model_name"]
|
221 |
-
],
|
222 |
-
inputs=[
|
223 |
-
gr.Textbox(label="input text here", lines=3),
|
224 |
-
# gr.Number(label="num_beams (integer) explores the specified number of possible outputs and selects the most " +
|
225 |
-
# "likely ones (specified in num_beams)", value=7),
|
226 |
-
gr.Number(label="num_return_sequences (integer) the number of outputs selected from num_beams possible output",
|
227 |
-
value=5),
|
228 |
-
gr.Number(
|
229 |
-
label="temperature (decimal) controls the creativity or randomness of the output. A higher temperature" +
|
230 |
-
" (e.g., 0.9) results in more diverse and creative output, while a lower temperature (e.g., 0.2)" +
|
231 |
-
" makes the output more deterministic and focused",
|
232 |
-
value=0.2),
|
233 |
-
gr.Number(label="repetition_penalty (decimal) penalizes words that have already appeared in the output, " +
|
234 |
-
"making them less likely to be generated again. A higher repetition_penalty (e.g., 1.5) results" +
|
235 |
-
"in more varied and non-repetitive output.",
|
236 |
-
value=1.5),
|
237 |
-
gr.Number(label="top_p (decimal) the model will only consider the words that have a high enough probability" +
|
238 |
-
" to reach a certain threshold",
|
239 |
-
value=0.9),
|
240 |
-
# gr.Number(label="top_k (integer) The number of highest probability vocabulary word will be considered" +
|
241 |
-
# "This means that only the tokens with the highest probabilities are considered for sampling" +
|
242 |
-
# "This reduces the diversity of the generated sequences, "+
|
243 |
-
# "but also makes them more likely to be coherent and fluent.",
|
244 |
-
# value=50),
|
245 |
-
gr.Checkbox(label="do_sample. If is set to False, num_return_sequences must be 1 because the generate function will use greedy decoding, " +
|
246 |
-
"which means that it will select the word with the highest probability at each step. " +
|
247 |
-
"This results in a deterministic and fluent output, but it might also lack diversity and creativity" +
|
248 |
-
"If is set to True, the generate function will use stochastic sampling, which means that it will randomly" +
|
249 |
-
" select a word from the probability distribution at each step. This results in a more diverse and creative" +
|
250 |
-
" output, but it might also introduce errors and inconsistencies ", value=True),
|
251 |
-
gr.Textbox(label="model", lines=3, value="untethered_model_name",visible=False)
|
252 |
-
#,visible=False
|
253 |
-
], outputs=[gr.Textbox(label="output response", lines=30)])
|
254 |
-
|
255 |
-
interface3 = gr.Interface(fn=create_response_untethered_paraphrased,
|
256 |
-
title="untethered paraphrased",
|
257 |
-
description="untethered paraphrased fine tuning",
|
258 |
-
examples=[
|
259 |
["What is death?",5,0.2,1.5,0.9,True], # The first example
|
260 |
["One of the best teachers in all of life turns out to be what?",5,0.2,1.5,0.9,True], # The second example
|
261 |
["what is your most meaningful relationship?",5,0.2,1.5,0.9,True], # The third example
|
262 |
["What actually gives life meaning?",5,0.2,1.5,0.9,True]
|
263 |
],
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
|
301 |
-
#interface2 = gr.Interface(fn=create_response_fine_tuned, inputs="text", outputs="text", title="Fine Tuned")
|
302 |
-
demo = gr.TabbedInterface([interface1, interface2, interface3], ["Original", "Untethered", "Untethered paraphrased"])
|
303 |
-
#demo = gr.TabbedInterface([interface1, interface3], ["Original", "Untethered paraphrased"])
|
304 |
-
# with gr.Blocks() as demo:
|
305 |
-
# with gr.Row():
|
306 |
-
#
|
307 |
demo.launch()
|
|
|
12 |
untethered_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240225_epochs_500')
|
13 |
untethered_paraphrased_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240227_epochs_350')
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
|
17 |
+
def create_response(input_str,
|
18 |
# num_beams,
|
19 |
num_return_sequences,
|
20 |
temperature,
|
|
|
38 |
if not do_sample:
|
39 |
num_beams = 1
|
40 |
print("num_beams=" + str(num_beams))
|
41 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
encoded = tokenizer.encode_plus(input_str + tokenizer.eos_token, return_tensors="pt")
|
43 |
input_ids = encoded["input_ids"]
|
44 |
attention_mask = encoded["attention_mask"]
|
45 |
|
46 |
|
47 |
+
if model_name == "original_model":
|
48 |
+
output_ids = original_model.generate(input_ids,pad_token_id=tokenizer.eos_token_id,do_sample=do_sample, attention_mask=attention_mask, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences )
|
49 |
+
elif model_name == "untethered_model":
|
50 |
+
output_ids = untethered_model.generate(input_ids,pad_token_id=tokenizer.eos_token_id,do_sample=do_sample, attention_mask=attention_mask, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences )
|
51 |
+
elif model_name == "untethered_paraphrased_model":
|
52 |
+
output_ids = untethered_paraphrased_model.generate(input_ids,pad_token_id=tokenizer.eos_token_id,do_sample=do_sample, attention_mask=attention_mask, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences )
|
53 |
+
else:
|
54 |
+
output_ids = original_model.generate(input_ids,pad_token_id=tokenizer.eos_token_id,do_sample=do_sample, attention_mask=attention_mask, max_length=100, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty,num_return_sequences=num_return_sequences )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
outputs = model_name+"generated <br>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
for output_id in output_ids:
|
58 |
output = tokenizer.decode(output_id, skip_special_tokens=True)
|
59 |
+
outputs=outputs+output+"<br/>"
|
60 |
return outputs
|
61 |
|
62 |
+
common_input_component_list = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
gr.Textbox(label="input text here", lines=3),
|
64 |
# gr.Number(label="num_beams (integer) explores the specified number of possible outputs and selects the most " +
|
65 |
# "likely ones (specified in num_beams)", value=7),
|
|
|
88 |
"If is set to True, the generate function will use stochastic sampling, which means that it will randomly" +
|
89 |
" select a word from the probability distribution at each step. This results in a more diverse and creative" +
|
90 |
" output, but it might also introduce errors and inconsistencies ", value=True)
|
91 |
+
]
|
92 |
+
common_output_component_list=[gr.Textbox(label="output response", lines=30)]
|
93 |
+
common_examples=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
["What is death?",5,0.2,1.5,0.9,True], # The first example
|
95 |
["One of the best teachers in all of life turns out to be what?",5,0.2,1.5,0.9,True], # The second example
|
96 |
["what is your most meaningful relationship?",5,0.2,1.5,0.9,True], # The third example
|
97 |
["What actually gives life meaning?",5,0.2,1.5,0.9,True]
|
98 |
],
|
99 |
+
examples = common_examples.copy()
|
100 |
+
for example in examples:
|
101 |
+
example.append("original_model")
|
102 |
+
|
103 |
+
interface_original = gr.Interface(fn=create_response,
|
104 |
+
title="original",
|
105 |
+
description="original language model, no fine tuning",
|
106 |
+
examples=examples,
|
107 |
+
inputs=common_input_component_list.append(gr.Textbox(label="model", lines=3, value="original_model",visible=False)),
|
108 |
+
outputs=common_output_component_list
|
109 |
+
)
|
110 |
+
examples = common_examples.copy()
|
111 |
+
for example in examples:
|
112 |
+
example.append("untethered_model")
|
113 |
+
interface_untethered_model = gr.Interface(fn=create_response,
|
114 |
+
title="untethered model",
|
115 |
+
description="language model fine tuned with'The Untethered Soul' chapter 17",
|
116 |
+
examples=examples,
|
117 |
+
inputs=common_input_component_list.append(gr.Textbox(label="model", lines=3, value="untethered_model",visible=False)),
|
118 |
+
outputs=common_output_component_list
|
119 |
+
)
|
120 |
+
|
121 |
+
examples = common_examples.copy()
|
122 |
+
for example in examples:
|
123 |
+
example.append("untethered_paraphrased_model")
|
124 |
+
interface_untethered_model = gr.Interface(fn=create_response,
|
125 |
+
title="untethered paraphrased_model",
|
126 |
+
description="language model fine tuned with'The Untethered Soul' chapter 17 paraphrased",
|
127 |
+
examples=examples,
|
128 |
+
inputs=common_input_component_list.append(gr.Textbox(label="model", lines=3, value="untethered_paraphrased_model",visible=False)),
|
129 |
+
outputs=common_output_component_list
|
130 |
+
)
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
demo = gr.TabbedInterface([interface_original, interface_untethered_model, interface_untethered_model], ["Original", "Untethered", "Untethered paraphrased"])
|
135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
demo.launch()
|