|
from transformers import GPT2Tokenizer, GPT2LMHeadModel |
|
|
|
|
|
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") |
|
|
|
|
|
model = GPT2LMHeadModel.from_pretrained("./fine_tuned_model") |
|
|
|
|
|
model.save_pretrained("./fine_tuned_model") |
|
tokenizer.save_pretrained("./fine_tuned_model") |
|
|
|
while True: |
|
try: |
|
|
|
prompt_text = input("You: ") |
|
|
|
|
|
input_ids = tokenizer.encode(prompt_text, return_tensors="pt") |
|
|
|
|
|
output = model.generate(input_ids, max_length=100, num_return_sequences=1, temperature=0.7, do_sample=True) |
|
|
|
|
|
generated_response = tokenizer.decode(output[0], skip_special_tokens=True) |
|
print("Bot:", generated_response) |
|
|
|
except Exception as e: |
|
print("An error occurred:", str(e)) |
|
|