40226743 commited on
Commit
f9877eb
1 Parent(s): 0462352

Add application file

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelWithLMHead, AutoTokenizer
2
+ import torch
3
+ import gradio as gr
4
+
5
+ tokenizer = AutoTokenizer.from_pretrained('microsoft/DialoGPT-medium')
6
+ model = AutoModelWithLMHead.from_pretrained('output-medium')
7
+
8
+ chat_history_ids = None
9
+ step = 0
10
+
11
+
12
+ def predict(input, chat_history_ids=chat_history_ids, step=step):
13
+ # encode the new user input, add the eos_token and return a tensor in Pytorch
14
+ new_user_input_ids = tokenizer.encode(
15
+ input + tokenizer.eos_token, return_tensors='pt')
16
+
17
+ # append the new user input tokens to the chat history
18
+ bot_input_ids = torch.cat(
19
+ [chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids
20
+
21
+ # generated a response while limiting the total chat history to 1000 tokens,
22
+ chat_history_ids = model.generate(
23
+ bot_input_ids, max_length=1000,
24
+ pad_token_id=tokenizer.eos_token_id,
25
+ no_repeat_ngram_size=3,
26
+ do_sample=True,
27
+ top_k=100,
28
+ top_p=0.7,
29
+ temperature=0.8
30
+ )
31
+ step = step + 1
32
+ output = tokenizer.decode(
33
+ chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
34
+ return output
35
+
36
+
37
+ gr.Interface(fn=predict, inputs="text", outputs="text").launch(debug=True)