chatbot.py
Browse files- chatbot.py +34 -0
chatbot.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
model_name = "microsoft/DialoGPT-medium" # You can also try blenderbot
|
| 5 |
+
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
chat_history_ids = None
|
| 10 |
+
step = 0
|
| 11 |
+
|
| 12 |
+
print("💌 AI GF: Hey love, I’m here to talk. What’s on your mind?")
|
| 13 |
+
while True:
|
| 14 |
+
user_input = input("You: ")
|
| 15 |
+
if user_input.lower() in ["exit", "quit"]:
|
| 16 |
+
print("AI GF: Bye sweetheart ❤️")
|
| 17 |
+
break
|
| 18 |
+
|
| 19 |
+
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
| 20 |
+
bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1) if step > 0 else new_input_ids
|
| 21 |
+
|
| 22 |
+
chat_history_ids = model.generate(
|
| 23 |
+
bot_input_ids,
|
| 24 |
+
max_length=1000,
|
| 25 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 26 |
+
do_sample=True,
|
| 27 |
+
top_k=50,
|
| 28 |
+
top_p=0.95,
|
| 29 |
+
temperature=0.75
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 33 |
+
print(f"💌 AI GF: {response}")
|
| 34 |
+
step += 1
|