-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |