forked from xiaoyachong/huggingface-DailoGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
68 lines (52 loc) · 2.59 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gradio as gr
import torch
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
def load_data():
tokenizer = AutoTokenizer.from_pretrained("SheilaCXY/DialoGPT-RickBot")
model = AutoModelForCausalLM.from_pretrained("SheilaCXY/DialoGPT-RickBot")
return tokenizer, model
tokenizer, model = load_data()
def chat(message, history):
history = history or []
new_user_input_ids = tokenizer.encode(message + tokenizer.eos_token, return_tensors='pt')
if len(history) > 0 and len(history) < 2:
for i in range(0,len(history)):
encoded_message = tokenizer.encode(history[i][0] + tokenizer.eos_token, return_tensors='pt')
encoded_response = tokenizer.encode(history[i][1] + tokenizer.eos_token, return_tensors='pt')
if i == 0:
chat_history_ids = encoded_message
chat_history_ids = torch.cat([chat_history_ids,encoded_response], dim=-1)
else:
chat_history_ids = torch.cat([chat_history_ids,encoded_message], dim=-1)
chat_history_ids = torch.cat([chat_history_ids,encoded_response], dim=-1)
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
elif len(history) >= 2:
for i in range(len(history)-1, len(history)):
encoded_message = tokenizer.encode(history[i][0] + tokenizer.eos_token, return_tensors='pt')
encoded_response = tokenizer.encode(history[i][1] + tokenizer.eos_token, return_tensors='pt')
if i == (len(history)-1):
chat_history_ids = encoded_message
chat_history_ids = torch.cat([chat_history_ids,encoded_response], dim=-1)
else:
chat_history_ids = torch.cat([chat_history_ids,encoded_message], dim=-1)
chat_history_ids = torch.cat([chat_history_ids,encoded_response], dim=-1)
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
elif len(history) == 0:
bot_input_ids = new_user_input_ids
chat_history_ids = model.generate(bot_input_ids, max_length=1000, do_sample=True, top_p=0.9, temperature=0.8, pad_token_id=tokenizer.eos_token_id)
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
history.append((message, response))
return history, history
title = "DialoGPT fine-tuned on Kaggle"
description = "Gradio demo for open-domain dialog using DialoGPT. Model was fine-tuned on the Kaggle dataset"
iface = gr.Interface(
chat,
["text", "state"],
["chatbot", "state"],
allow_screenshot=False,
allow_flagging="never",
title=title,
description=description
)
iface.launch(debug=True)