wuki-king commited on
Commit
a7ac4c4
1 Parent(s): 8bcae99

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+ import torch
4
+
5
+
6
+ title = " AI-test"
7
+ description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
8
+ examples = [["How are you?"]]
9
+
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
12
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
13
+
14
+
15
+ def predict(input, history=[]):
16
+ # 将新输入的句子进行分词
17
+ new_user_input_ids = tokenizer.encode(
18
+ input + tokenizer.eos_token, return_tensors="pt"
19
+ )
20
+
21
+ # 将新用户输入的令牌附加到聊天历史记录中
22
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
23
+
24
+ # 生成一个响应
25
+ history = model.generate(
26
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
27
+ ).tolist()
28
+
29
+ # 将令牌转换为文本,然后将响应拆分为行
30
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
31
+ # print('decoded_response-->>'+str(response))
32
+ response = [
33
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
34
+ ] # 转换为列表的图元
35
+ # print('response-->>'+str(response))
36
+ return response, history
37
+
38
+
39
+ gr.Interface(
40
+ fn=predict,
41
+ title=title,
42
+ description=description,
43
+ examples=examples,
44
+ inputs=["text", "state"],
45
+ outputs=["chatbot", "state"],
46
+ theme="finlaymacklon/boxy_violet",
47
+ ).launch()