-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicChatbotUsingBlenderBot
35 lines (23 loc) · 1.23 KB
/
BasicChatbotUsingBlenderBot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
model_name = "facebook/blenderbot-400M-distill"
# model is an instance of the class AutoModelForSeq2SeqLM, which allows you to interact with your chosen language model.
# tokenizer is an instance of the class AutoTokenizer, which optimizes your input and passes it to the language model efficiently.
# It does so by converting your text input to “tokens”, which is how the model interprets the text.
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
conversation_history = []
while True:
# Create conversation history string
history_string = "\n".join(conversation_history)
# Get the input data from the user
input_text = input("> ")
# Tokenize the input text and history
inputs = tokenizer.encode_plus(history_string, input_text, return_tensors="pt")
# Generate the response from the model
outputs = model.generate(**inputs)
# Decode the response
response = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
print(response)
# Add interaction to conversation history
conversation_history.append(input_text)
conversation_history.append(response)