Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,58 @@
|
|
1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
-
from transformers import BlenderbotForConditionalGeneration
|
3 |
import torch
|
4 |
import gradio as gr
|
5 |
|
6 |
-
#
|
7 |
model_name = "microsoft/DialoGPT-medium"
|
8 |
-
|
9 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
10 |
|
11 |
-
def
|
12 |
-
user_input_ids =
|
13 |
|
14 |
-
#
|
15 |
-
chatbot_input_ids = torch.cat([torch.LongTensor(
|
16 |
|
17 |
-
#
|
18 |
-
chat_history =
|
19 |
print(chat_history)
|
20 |
|
21 |
-
response =
|
22 |
-
|
|
|
23 |
print(response)
|
24 |
-
|
25 |
# html for display
|
26 |
-
html = "<div class='
|
27 |
-
for x,
|
28 |
-
if x%2
|
29 |
-
|
30 |
-
|
31 |
-
else:
|
32 |
-
|
33 |
-
|
34 |
-
print("
|
35 |
print(x)
|
36 |
-
print("
|
37 |
-
print(
|
38 |
-
|
|
|
39 |
html += "</div>"
|
40 |
print(html)
|
41 |
return html, chat_history
|
42 |
|
43 |
-
|
|
|
44 |
css = """
|
45 |
-
.
|
46 |
-
.
|
47 |
-
.
|
48 |
-
.
|
49 |
-
.footer
|
50 |
"""
|
51 |
-
in_text = gr.inputs.Textbox(placeholder="Let's start a chat
|
52 |
-
gr.Interface(fn=
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer,BlenderbotForConditionalGeneration
|
|
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
|
5 |
+
#model_name = "facebook/blenderbot-400M-distill"
|
6 |
model_name = "microsoft/DialoGPT-medium"
|
7 |
+
chat_token = AutoTokenizer.from_pretrained(model_name)
|
8 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
10 |
+
def converse(user_input, chat_history=[]):
|
11 |
+
user_input_ids = chat_token(user_input + chat_token.eos_token, return_tensors='pt').input_ids
|
12 |
|
13 |
+
# keep history in the tensor
|
14 |
+
chatbot_input_ids = torch.cat([torch.LongTensor(chat_history), user_input_ids], dim=-1)
|
15 |
|
16 |
+
# get response
|
17 |
+
chat_history = mdl.generate(chatbot_input_ids, max_length=1000, pad_token_id=chat_token.eos_token_id).tolist()
|
18 |
print(chat_history)
|
19 |
|
20 |
+
response = chat_token.decode(chat_history[0]).split("<|endoftext|>")
|
21 |
+
|
22 |
+
print("Starting to print response...")
|
23 |
print(response)
|
24 |
+
|
25 |
# html for display
|
26 |
+
html = "<div class='mybot'>"
|
27 |
+
for x, mesg in enumerate(response):
|
28 |
+
if x%2!=0 :
|
29 |
+
mesg="Bot:" + mesg
|
30 |
+
clazz = "bot"
|
31 |
+
else :
|
32 |
+
clazz = "user"
|
33 |
+
|
34 |
+
print("value of x")
|
35 |
print(x)
|
36 |
+
print("message")
|
37 |
+
print (mesg)
|
38 |
+
|
39 |
+
html += "<div class='mesg {}'> {}</div>".format(clazz, mesg)
|
40 |
html += "</div>"
|
41 |
print(html)
|
42 |
return html, chat_history
|
43 |
|
44 |
+
|
45 |
+
|
46 |
css = """
|
47 |
+
.mybot {display:flex;flex-direction:column}
|
48 |
+
.mesg {padding:5px;margin-bottom:5px;border-radius:5px;width:75%}
|
49 |
+
.mesg.user {background-color:lightblue;color:white}
|
50 |
+
.mesg.bot {background-color:orange;color:white,align-self:self-end}
|
51 |
+
.footer {display:none !important}
|
52 |
"""
|
53 |
+
in_text = gr.inputs.Textbox(placeholder="Let's start a chat")
|
54 |
+
gr.Interface(fn=converse,
|
55 |
+
theme="default",
|
56 |
+
inputs=[in_text, "state"],
|
57 |
+
outputs=["html", "state"],
|
58 |
+
css=css).launch()
|