Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,15 @@
|
|
|
|
1 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
import torch
|
3 |
|
4 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
5 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
6 |
|
|
|
7 |
def predict(input, history=[]):
|
8 |
# tokenize the new input sentence
|
|
|
|
|
9 |
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
10 |
|
11 |
# append the new user input tokens to the chat history
|
@@ -16,30 +20,17 @@ def predict(input, history=[]):
|
|
16 |
|
17 |
# convert the tokens to text, and then split the responses into lines
|
18 |
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
19 |
-
response
|
20 |
-
|
21 |
-
# write some HTML
|
22 |
-
html = "<div class='chatbot'>"
|
23 |
-
for m, msg in enumerate(response):
|
24 |
-
cls = "user" if m%2 == 0 else "bot"
|
25 |
-
html += "<div class='msg {}'> {}</div>".format(cls, msg)
|
26 |
-
html += "</div>"
|
27 |
-
|
28 |
-
return html, history
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
.
|
35 |
-
.
|
36 |
-
|
37 |
-
.
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
theme="default",
|
42 |
-
inputs=[gr.inputs.Textbox(placeholder="How are you?"), "state"],
|
43 |
-
outputs=["html", "state"],
|
44 |
-
css=css).launch()
|
45 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
6 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
7 |
|
8 |
+
|
9 |
def predict(input, history=[]):
|
10 |
# tokenize the new input sentence
|
11 |
+
print(input)
|
12 |
+
print(tokenizer.eos_token)
|
13 |
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
14 |
|
15 |
# append the new user input tokens to the chat history
|
|
|
20 |
|
21 |
# convert the tokens to text, and then split the responses into lines
|
22 |
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
23 |
+
response = [(response[i], response[i+1]) for i in range(0, len(response)-1, 2)] # convert to tuples of list
|
24 |
+
return response, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
with gr.Blocks() as demo:
|
27 |
+
chatbot = gr.Chatbot()
|
28 |
+
state = gr.State([])
|
29 |
+
|
30 |
+
with gr.Row():
|
31 |
+
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
32 |
+
|
33 |
+
txt.submit(predict, [txt, state], [chatbot, state])
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|