Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,61 @@
|
|
1 |
-
import
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
def predict(input, history=[]):
|
9 |
-
# tokenize the new input sentence
|
10 |
-
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
11 |
|
12 |
-
|
13 |
-
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
14 |
|
15 |
-
|
16 |
-
history =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
# convert the tokens to text, and then split the responses into lines
|
19 |
-
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
20 |
-
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
21 |
-
return response, history
|
22 |
|
23 |
-
gr.Interface(
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
|
|
2 |
import torch
|
3 |
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
mname = "facebook/blenderbot-400M-distill"
|
7 |
+
model = BlenderbotForConditionalGeneration.from_pretrained(mname)
|
8 |
+
tokenizer = BlenderbotTokenizer.from_pretrained(mname)
|
9 |
+
|
10 |
+
|
11 |
+
def take_last_tokens(inputs, note_history, history):
|
12 |
+
"""Filter the last 128 tokens"""
|
13 |
+
if inputs['input_ids'].shape[1] > 128:
|
14 |
+
inputs['input_ids'] = torch.tensor([inputs['input_ids'][0][-128:].tolist()])
|
15 |
+
inputs['attention_mask'] = torch.tensor([inputs['attention_mask'][0][-128:].tolist()])
|
16 |
+
note_history = ['</s> <s>'.join(note_history[0].split('</s> <s>')[2:])]
|
17 |
+
history = history[1:]
|
18 |
+
|
19 |
+
return inputs, note_history, history
|
20 |
+
|
21 |
+
|
22 |
+
def add_note_to_history(note, note_history):
|
23 |
+
"""Add a note to the historical information"""
|
24 |
+
note_history.append(note)
|
25 |
+
note_history = '</s> <s>'.join(note_history)
|
26 |
+
return [note_history]
|
27 |
|
|
|
|
|
|
|
28 |
|
29 |
+
title = "Mantain a conversation with the bot"
|
|
|
30 |
|
31 |
+
def chatbot(message, history):
|
32 |
+
history = history or []
|
33 |
+
if history:
|
34 |
+
history_useful = ['</s> <s>'.join([str(a[0])+'</s> <s>'+str(a[1]) for a in history])]
|
35 |
+
else:
|
36 |
+
history_useful = []
|
37 |
+
|
38 |
+
history_useful = add_note_to_history(message, history_useful)
|
39 |
+
|
40 |
+
inputs = tokenizer(history_useful, return_tensors="pt")
|
41 |
+
inputs, history_useful, history = take_last_tokens(inputs, history_useful, history)
|
42 |
+
|
43 |
+
reply_ids = model.generate(**inputs)
|
44 |
+
response = tokenizer.batch_decode(reply_ids, skip_special_tokens=True)[0]
|
45 |
+
history_useful = add_note_to_history(response, history_useful)
|
46 |
+
|
47 |
+
|
48 |
+
list_history = history_useful[0].split('</s> <s>')
|
49 |
+
history.append((list_history[-2], list_history[-1]))
|
50 |
+
|
51 |
+
return history, history
|
52 |
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
gr.Interface(
|
55 |
+
fn=chatbot,
|
56 |
+
css=".footer {display:none !important}",
|
57 |
+
inputs=["text", "state"],
|
58 |
+
outputs=["chatbot", "state"],
|
59 |
+
title=title,
|
60 |
+
allow_flagging="never",
|
61 |
+
).launch( debug= True)
|