Spaces:
Build error
Build error
dennis-fast
commited on
Commit
•
c5d4354
1
Parent(s):
443f792
Update app.py
Browse files
app.py
CHANGED
@@ -28,78 +28,11 @@ def predict(input, history=[]):
|
|
28 |
# convert the tokens to text, and then split the responses into lines
|
29 |
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
30 |
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
31 |
-
#response.remove("")
|
32 |
-
|
33 |
-
# write some HTML
|
34 |
-
html = "<div class='chatbot'>"
|
35 |
-
for m, msg in enumerate(response):
|
36 |
-
cls = "user" if m%2 == 0 else "bot"
|
37 |
-
html += "<div class='msg {}'> {}</div>".format(cls, msg)
|
38 |
-
html += "</div>"
|
39 |
|
40 |
return response, history
|
41 |
-
#return html, history
|
42 |
|
43 |
-
css = """
|
44 |
-
.chatbox {display:flex;flex-direction:column}
|
45 |
-
.msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
|
46 |
-
.msg.user {background-color:cornflowerblue;color:white;margin-right:10px}
|
47 |
-
.msg.bot {background-color:lightgray;align-self:self-end;margin-left:10px}
|
48 |
-
.footer {display:none !important}
|
49 |
-
"""
|
50 |
-
|
51 |
gr.Interface(fn=predict,
|
52 |
-
theme="grass",
|
53 |
title="DialoGPT-large",
|
54 |
inputs=["text", "state"],
|
55 |
outputs=["chatbot", "state"],
|
56 |
-
|
57 |
-
).launch()
|
58 |
-
|
59 |
-
'''
|
60 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
61 |
-
import torch
|
62 |
-
|
63 |
-
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
64 |
-
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
65 |
-
|
66 |
-
def predict(input, history=[]):
|
67 |
-
# tokenize the new input sentence
|
68 |
-
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
69 |
-
|
70 |
-
# append the new user input tokens to the chat history
|
71 |
-
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
72 |
-
|
73 |
-
# generate a response
|
74 |
-
history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
|
75 |
-
|
76 |
-
# convert the tokens to text, and then split the responses into lines
|
77 |
-
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
78 |
-
response.remove("")
|
79 |
-
|
80 |
-
# write some HTML
|
81 |
-
html = "<div class='chatbot'>"
|
82 |
-
for m, msg in enumerate(response):
|
83 |
-
cls = "user" if m%2 == 0 else "bot"
|
84 |
-
html += "<div class='msg {}'> {}</div>".format(cls, msg)
|
85 |
-
html += "</div>"
|
86 |
-
|
87 |
-
return html, history
|
88 |
-
|
89 |
-
import gradio as gr
|
90 |
-
|
91 |
-
css = """
|
92 |
-
.chatbox {display:flex;flex-direction:column}
|
93 |
-
.msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
|
94 |
-
.msg.user {background-color:cornflowerblue;color:white}
|
95 |
-
.msg.bot {background-color:lightgray;align-self:self-end}
|
96 |
-
.footer {display:none !important}
|
97 |
-
"""
|
98 |
-
|
99 |
-
gr.Interface(fn=predict,
|
100 |
-
theme="default",
|
101 |
-
inputs=[gr.inputs.Textbox(placeholder="How are you?"), "state"],
|
102 |
-
outputs=["html", "state"],
|
103 |
-
css=css).launch()
|
104 |
-
'''
|
105 |
-
|
|
|
28 |
# convert the tokens to text, and then split the responses into lines
|
29 |
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
30 |
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
return response, history
|
|
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
gr.Interface(fn=predict,
|
|
|
35 |
title="DialoGPT-large",
|
36 |
inputs=["text", "state"],
|
37 |
outputs=["chatbot", "state"],
|
38 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|