-
Notifications
You must be signed in to change notification settings - Fork 73
/
chat_bot.py
27 lines (23 loc) · 975 Bytes
/
chat_bot.py
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
# Import the required libraries
import torch
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load the pre-trained GPT-2 model and tokenizer
model_name = "gpt2" # You can choose a different model size if needed
model = GPT2LMHeadModel.from_pretrained(model_name)
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
# Set the device (CPU or GPU)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
# Define a function to generate responses
def generate_response(input_text, max_length=100):
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
output = model.generate(input_ids, max_length=max_length, num_return_sequences=1)
response = tokenizer.decode(output[0], skip_special_tokens=True)
return response
# Example conversation loop
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
break
response = generate_response(user_input)
print("Bot:", response)