Spaces:
Sleeping
Sleeping
Create chatbot.py
Browse files- chatbot.py +12 -0
chatbot.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# chatbot.py
|
2 |
+
from transformers import BlenderbotTokenizer, BlenderbotForConditionalGeneration
|
3 |
+
|
4 |
+
tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")
|
5 |
+
model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
|
6 |
+
|
7 |
+
def generate_response(user_input, chat_history=[]):
|
8 |
+
inputs = tokenizer([user_input], return_tensors='pt')
|
9 |
+
chat_history_ids = model.generate(**inputs)
|
10 |
+
bot_response = tokenizer.decode(chat_history_ids[0], skip_special_tokens=True)
|
11 |
+
chat_history.append({"role": "assistant", "content": bot_response})
|
12 |
+
return bot_response, chat_history
|