dennis-fast commited on
Commit
f0b1c44
1 Parent(s): b730ad7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import transformers
2
  import gradio as gr
3
  import torch
@@ -55,4 +56,50 @@ gr.Interface(fn=predict,
55
  outputs=["html", "state"],
56
  css=css
57
  ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
 
1
+ '''
2
  import transformers
3
  import gradio as gr
4
  import torch
 
56
  outputs=["html", "state"],
57
  css=css
58
  ).launch()
59
+ '''
60
+
61
+ from transformers import AutoModelForCausalLM, AutoTokenizer
62
+ import torch
63
+
64
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
65
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
66
+
67
+ def predict(input, history=[]):
68
+ # tokenize the new input sentence
69
+ new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
70
+
71
+ # append the new user input tokens to the chat history
72
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
73
+
74
+ # generate a response
75
+ history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
76
+
77
+ # convert the tokens to text, and then split the responses into lines
78
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
79
+ response.remove("")
80
+
81
+ # write some HTML
82
+ html = "<div class='chatbot'>"
83
+ for m, msg in enumerate(response):
84
+ cls = "user" if m%2 == 0 else "bot"
85
+ html += "<div class='msg {}'> {}</div>".format(cls, msg)
86
+ html += "</div>"
87
+
88
+ return html, history
89
+
90
+ import gradio as gr
91
+
92
+ css = """
93
+ .chatbox {display:flex;flex-direction:column}
94
+ .msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
95
+ .msg.user {background-color:cornflowerblue;color:white}
96
+ .msg.bot {background-color:lightgray;align-self:self-end}
97
+ .footer {display:none !important}
98
+ """
99
+
100
+ gr.Interface(fn=predict,
101
+ theme="default",
102
+ inputs=[gr.inputs.Textbox(placeholder="How are you?"), "state"],
103
+ outputs=["html", "state"],
104
+ css=css).launch()
105