nmaina commited on
Commit
e021d98
·
1 Parent(s): f189c7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -2,22 +2,25 @@ import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
- from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
6
 
7
- model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50")
8
- tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50", src_lang="en_XX", tgt_lang="ro_RO")
9
 
10
- src_text = " UN Chief Says There Is No Military Solution in Syria"
11
- tgt_text = "Şeful ONU declară că nu există o soluţie militară în Siria"
12
 
13
- model_inputs = tokenizer(src_text, return_tensors="pt")
14
- with tokenizer.as_target_tokenizer():
15
- labels = tokenizer(tgt_text, return_tensors="pt").input_ids
16
-
17
- model(**model_inputs, labels=labels) # forward pass
18
 
 
 
19
 
 
 
20
 
 
 
 
 
21
 
22
  with gr.Blocks() as demo:
23
  chatbot = gr.Chatbot()
 
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
 
5
 
 
 
6
 
7
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
8
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
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 lines
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
  with gr.Blocks() as demo:
26
  chatbot = gr.Chatbot()