Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,46 @@
|
|
1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
-
import gradio as gr
|
3 |
-
import torch
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
tokenizer.
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
examples=examples,
|
48 |
-
inputs=["text", "state"],
|
49 |
-
outputs=["chatbot", "state"],
|
50 |
-
theme="ParityError/Anime",
|
51 |
-
).launch()
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
|
5 |
+
title = "EZChat"
|
6 |
+
description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT-medium)"
|
7 |
+
examples = [["How are you?"]]
|
8 |
+
|
9 |
+
# Set the padding token to be used and initialize the model
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
11 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
12 |
+
tokenizer.add_special_tokens({'pad_token': '[EOS]'})
|
13 |
+
tokenizer.pad_token = tokenizer.eos_token
|
14 |
+
|
15 |
+
#predict
|
16 |
+
def predict(input, history=[]):
|
17 |
+
# tokenize the new input sentence
|
18 |
+
new_user_input_ids = tokenizer.encode(
|
19 |
+
input + tokenizer.eos_token, return_tensors="pt"
|
20 |
+
)
|
21 |
+
|
22 |
+
# append the new user input tokens to the chat history
|
23 |
+
bot_input_ids = torch.cat([torch.tensor(history), new_user_input_ids], dim=-1) if history else new_user_input_ids
|
24 |
+
|
25 |
+
# generate a response
|
26 |
+
chat_history_ids = model.generate(
|
27 |
+
bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
|
28 |
+
)
|
29 |
+
|
30 |
+
# convert the tokens to text, and then split the responses into lines
|
31 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
32 |
+
|
33 |
+
return response, chat_history_ids.tolist()[0]
|
34 |
+
|
35 |
+
|
36 |
+
iface = gr.Interface(
|
37 |
+
fn=predict,
|
38 |
+
title=title,
|
39 |
+
description=description,
|
40 |
+
examples=examples,
|
41 |
+
inputs=["text", gr.inputs.Slider(0, 4000, default=2000, label='Chat History')],
|
42 |
+
outputs=["text", "text"],
|
43 |
+
theme="ParityError/Anime",
|
44 |
+
)
|
45 |
+
|
46 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|