Spaces:
Sleeping
Sleeping
Merge branch 'main' of https://huggingface.co/spaces/zmbfeng/testchatbot
Browse files
app.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import os
|
|
|
|
|
4 |
from huggingface_hub import login
|
5 |
from transformers import pipeline
|
6 |
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
@@ -9,33 +11,142 @@ login(os.environ["HF_TOKEN"])
|
|
9 |
#generator = pipeline('text-generation', model="microsoft/DialoGPT-medium")
|
10 |
tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-medium')
|
11 |
original_model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium')
|
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 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
import os
|
4 |
+
import copy
|
5 |
+
|
6 |
from huggingface_hub import login
|
7 |
from transformers import pipeline
|
8 |
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
|
|
11 |
#generator = pipeline('text-generation', model="microsoft/DialoGPT-medium")
|
12 |
tokenizer = GPT2Tokenizer.from_pretrained('microsoft/DialoGPT-medium')
|
13 |
original_model = GPT2LMHeadModel.from_pretrained('microsoft/DialoGPT-medium')
|
14 |
+
untethered_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240225_epochs_500')
|
15 |
+
untethered_paraphrased_model = GPT2LMHeadModel.from_pretrained('zmbfeng/untethered_20240227_epochs_350')
|
16 |
+
|
17 |
+
def create_response(input_str,
|
18 |
+
# num_beams,
|
19 |
+
num_return_sequences,
|
20 |
+
temperature,
|
21 |
+
repetition_penalty,
|
22 |
+
top_p,
|
23 |
+
# top_k,
|
24 |
+
do_sample,
|
25 |
+
model_name):
|
26 |
+
print("input_str="+input_str)
|
27 |
+
print("model_name="+str(model_name))
|
28 |
+
# num_beams = int(num_beams)
|
29 |
+
# print("num_beams=" + str(num_beams))
|
30 |
+
num_return_sequences=int(num_return_sequences)
|
31 |
+
print("num_return_sequences" + str(num_return_sequences))
|
32 |
+
print("top_p" + str(top_p))
|
33 |
+
# top_k=int(top_k)
|
34 |
+
# print("top_k" + str(top_k))
|
35 |
+
print("repetition_penalty" + str(repetition_penalty))
|
36 |
+
print("temperature" + str(temperature))
|
37 |
+
print("do_sample" + str(do_sample))
|
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),
|
66 |
+
gr.Number(label="num_return_sequences (integer) the number of outputs selected from num_beams possible output",
|
67 |
+
value=5),
|
68 |
+
gr.Number(
|
69 |
+
label="temperature (decimal) controls the creativity or randomness of the output. A higher temperature" +
|
70 |
+
" (e.g., 0.9) results in more diverse and creative output, while a lower temperature (e.g., 0.2)" +
|
71 |
+
" makes the output more deterministic and focused",
|
72 |
+
value=0.2),
|
73 |
+
gr.Number(label="repetition_penalty (decimal) penalizes words that have already appeared in the output, " +
|
74 |
+
"making them less likely to be generated again. A higher repetition_penalty (e.g., 1.5) results" +
|
75 |
+
"in more varied and non-repetitive output.",
|
76 |
+
value=1.5),
|
77 |
+
gr.Number(label="top_p (decimal) the model will only consider the words that have a high enough probability" +
|
78 |
+
" to reach a certain threshold",
|
79 |
+
value=0.9),
|
80 |
+
# gr.Number(label="top_k (integer) The number of highest probability vocabulary word will be considered" +
|
81 |
+
# "This means that only the tokens with the highest probabilities are considered for sampling" +
|
82 |
+
# "This reduces the diversity of the generated sequences, "+
|
83 |
+
# "but also makes them more likely to be coherent and fluent.",
|
84 |
+
# value=50),
|
85 |
+
gr.Checkbox(label="do_sample. If is set to False, num_return_sequences must be 1 because the generate function will use greedy decoding, " +
|
86 |
+
"which means that it will select the word with the highest probability at each step. " +
|
87 |
+
"This results in a deterministic and fluent output, but it might also lack diversity and creativity" +
|
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 |
+
input_component_list=copy.deepcopy(common_input_component_list)
|
93 |
+
input_component_list.append(gr.Textbox(label="model", lines=3, value="original_model",visible=False))
|
94 |
+
common_output_component_list=[gr.Textbox(label="output response", lines=30)]
|
95 |
+
common_examples=[
|
96 |
+
["What is death?",5,0.2,1.5,0.9,True], # The first example
|
97 |
+
["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
|
98 |
+
["what is your most meaningful relationship?",5,0.2,1.5,0.9,True], # The third example
|
99 |
+
["What actually gives life meaning?",5,0.2,1.5,0.9,True]
|
100 |
+
]
|
101 |
+
examples = copy.deepcopy(common_examples)
|
102 |
+
print(examples)
|
103 |
+
for example in examples:
|
104 |
+
example.append("original_model")
|
105 |
+
print(examples)
|
106 |
+
input_component_list=copy.deepcopy(common_input_component_list)
|
107 |
+
input_component_list.append(gr.Textbox(label="model", lines=3, value="untethered_model",visible=False))
|
108 |
+
output_component_list = copy.deepcopy(common_output_component_list)
|
109 |
+
interface_original = gr.Interface(fn=create_response,
|
110 |
+
title="original",
|
111 |
+
description="original language model, no fine tuning",
|
112 |
+
examples=examples,
|
113 |
+
inputs=input_component_list,
|
114 |
+
outputs=output_component_list
|
115 |
+
)
|
116 |
+
examples = copy.deepcopy(common_examples)
|
117 |
+
print(examples)
|
118 |
+
for example in examples:
|
119 |
+
example.append("untethered_model")
|
120 |
+
print(examples)
|
121 |
+
input_component_list=copy.deepcopy(common_input_component_list)
|
122 |
+
input_component_list.append(gr.Textbox(label="model", lines=3, value="untethered_paraphrased_model",visible=False))
|
123 |
+
output_component_list = copy.deepcopy(common_output_component_list)
|
124 |
+
interface_untethered_model = gr.Interface(fn=create_response,
|
125 |
+
title="untethered model",
|
126 |
+
description="language model fine tuned with'The Untethered Soul' chapter 17",
|
127 |
+
examples=examples,
|
128 |
+
inputs=input_component_list,
|
129 |
+
outputs=output_component_list
|
130 |
+
)
|
131 |
+
|
132 |
+
examples = copy.deepcopy(common_examples)
|
133 |
+
print(examples)
|
134 |
+
for example in examples:
|
135 |
+
example.append("untethered_paraphrased_model")
|
136 |
+
print(examples)
|
137 |
+
input_component_list=copy.deepcopy(common_input_component_list)
|
138 |
+
input_component_list.append(gr.Textbox(label="model", lines=3, value="untethered_model",visible=False))
|
139 |
+
output_component_list = copy.deepcopy(common_output_component_list)
|
140 |
+
interface_untethered_paraphrased_model = gr.Interface(fn=create_response,
|
141 |
+
title="untethered paraphrased_model",
|
142 |
+
description="language model fine tuned with'The Untethered Soul' chapter 17 paraphrased",
|
143 |
+
examples=examples,
|
144 |
+
inputs=input_component_list,
|
145 |
+
outputs= output_component_list
|
146 |
+
)
|
147 |
+
|
148 |
+
|
149 |
+
|
150 |
+
demo = gr.TabbedInterface([interface_original, interface_untethered_model, interface_untethered_paraphrased_model], ["Original", "Untethered", "Untethered paraphrased"])
|
151 |
+
|
152 |
demo.launch()
|