update code
Browse files
app.py
CHANGED
@@ -1,28 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModel
|
|
|
3 |
|
4 |
def chat(prompt):
|
5 |
|
6 |
global model, tokenizer
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
-
|
|
|
|
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
if __name__ == '__main__':
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
model.eval()
|
27 |
iface = gr.Interface(fn=chat, inputs="text", outputs="text")
|
28 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModel
|
3 |
+
from transformers import GPT2Tokenizer,GPT2LMHeadModel
|
4 |
|
5 |
def chat(prompt):
|
6 |
|
7 |
global model, tokenizer
|
8 |
+
inputs = tokenizer(prompt, return_tensors='pt')
|
9 |
+
generation_output = model.generate(**inputs,
|
10 |
+
return_dict_in_generate=True,
|
11 |
+
output_scores=True,
|
12 |
+
max_length=150,
|
13 |
+
# max_new_tokens=80,
|
14 |
+
do_sample=True,
|
15 |
+
top_p=0.6,
|
16 |
+
# num_beams=5,
|
17 |
+
eos_token_id=50256,
|
18 |
+
pad_token_id=0,
|
19 |
+
num_return_sequences=1)
|
20 |
|
21 |
+
for idx, sentence in enumerate(generation_output.sequences):
|
22 |
+
|
23 |
+
return tokenizer.decode(sentence).split('<|endoftext|>')[0]
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
if __name__ == '__main__':
|
30 |
+
hf_model_path = 'IDEA-CCNL/Yuyuan-GPT2-110M-SciFi-Chinese'
|
31 |
+
tokenizer = GPT2Tokenizer.from_pretrained(hf_model_path)
|
32 |
+
model = GPT2LMHeadModel.from_pretrained(hf_model_path)
|
33 |
+
|
34 |
model.eval()
|
35 |
iface = gr.Interface(fn=chat, inputs="text", outputs="text")
|
36 |
iface.launch()
|