Spaces:
Runtime error
Runtime error
dennis-fast
commited on
Commit
·
1258daf
1
Parent(s):
4eb2b3c
Update app.py
Browse files
app.py
CHANGED
@@ -7,11 +7,20 @@ from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
|
7 |
tokenizer = GPT2Tokenizer.from_pretrained('dennis-fast/DialoGPT-ElonMusk')
|
8 |
model = GPT2LMHeadModel.from_pretrained('dennis-fast/DialoGPT-ElonMusk')
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
gr.Interface(fn=predict,
|
17 |
title="Chat with Elon",
|
|
|
7 |
tokenizer = GPT2Tokenizer.from_pretrained('dennis-fast/DialoGPT-ElonMusk')
|
8 |
model = GPT2LMHeadModel.from_pretrained('dennis-fast/DialoGPT-ElonMusk')
|
9 |
|
10 |
+
def predict(input, history=[]):
|
11 |
+
# tokenize the new input sentence
|
12 |
+
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
13 |
+
|
14 |
+
# append the new user input tokens to the chat history
|
15 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
16 |
+
|
17 |
+
# generate a response
|
18 |
+
history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
|
19 |
+
|
20 |
+
# convert the tokens to text, and then split the responses into the right format
|
21 |
+
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
22 |
+
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
23 |
+
return response, history
|
24 |
|
25 |
gr.Interface(fn=predict,
|
26 |
title="Chat with Elon",
|