Skip to content

Commit

Permalink
Create chat_bot.py
Browse files Browse the repository at this point in the history
  • Loading branch information
AYUSHSURYAVANSHI authored Oct 11, 2023
1 parent 0f701bb commit 65fe238
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions chat_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)

0 comments on commit 65fe238

Please sign in to comment.