ryderprogram commited on
Commit
986e40b
·
1 Parent(s): d9c8b41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -5
app.py CHANGED
@@ -1,7 +1,32 @@
1
  import gradio as gr
2
- from transformers import pipeline, Conversation
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
- pipeline = pipeline(task="conversational", model="facebook/blenderbot-400M-distill")
5
- con1 = Conversation("Going to the movies tonight - any suggestions?")
6
- con2 = Conversation("What's the last book you have read?")
7
- converse([con1, con2])
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, BlenderbotForConditionalGeneration, BlenderbotForCausalLM, BlenderbotTokenizer
4
+
5
+ tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
6
+ model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill",add_cross_attention=False)
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
+ # append the new user input tokens to the chat history
13
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
14
+
15
+ # generate a response
16
+ history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
17
+
18
+ # convert the tokens to text, and then split the responses into the right format
19
+ response = tokenizer.decode(history[0]).replace("<s>","").split("</s>")
20
+ response = [(response[i], response[i+1]) for i in range(0, len(response), 2)] # convert to tuples of list
21
+ return response, history
22
+
23
+ gr.Interface(
24
+ fn = predict,
25
+ inputs = ["textbox","state"],
26
+ outputs = ["chatbot","state"],
27
+ theme ="seafoam",
28
+ title = title,
29
+ description = description,
30
+ article = article
31
+ ).launch(enable_queue=True)
32