Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -20,28 +20,30 @@ examples = [["How are you?"]]
|
|
20 |
|
21 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
22 |
tokenizer.padding_side = 'left'
|
|
|
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 |
-
|
|
|
|
20 |
|
21 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
22 |
tokenizer.padding_side = 'left'
|
23 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
24 |
|
25 |
+
class ChatBot:
|
26 |
+
def __init__(self):
|
27 |
+
self.history = []
|
28 |
+
|
29 |
+
def predict(self, input):
|
30 |
+
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt")
|
31 |
+
bot_input_ids = torch.cat([torch.tensor(self.history), new_user_input_ids], dim=-1) if self.history else new_user_input_ids
|
32 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id)
|
33 |
+
self.history.append(chat_history_ids[:, bot_input_ids.shape[-1]:].tolist())
|
34 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
35 |
+
return response
|
36 |
+
|
37 |
+
bot = ChatBot()
|
38 |
+
|
39 |
+
iface = gr.Interface(
|
40 |
+
fn=bot.predict,
|
41 |
+
title=title,
|
42 |
+
description=description,
|
43 |
+
examples=examples,
|
44 |
+
inputs="text",
|
45 |
+
outputs="text",
|
46 |
+
theme="ParityError/Anime",
|
47 |
+
)
|
48 |
+
|
49 |
+
iface.launch()
|