0% found this document useful (0 votes)
87 views

Conversational Ai Chatbot

The document defines a ChatBot class that implements a conversational agent using the DialoGPT model. The class initializes the model and tokenizer, greets the user, accepts user input, responds to the user by generating a response with the model or random response, and ends the conversation when the user enters "bye", "quit", or "exit". An instance of the ChatBot class is created and runs in a loop, interacting with the user until they exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

Conversational Ai Chatbot

The document defines a ChatBot class that implements a conversational agent using the DialoGPT model. The class initializes the model and tokenizer, greets the user, accepts user input, responds to the user by generating a response with the model or random response, and ends the conversation when the user enters "bye", "quit", or "exit". An instance of the ChatBot class is created and runs in a loop, interacting with the user until they exit.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

import numpy as np

import time
import os
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

checkpoint = "microsoft/DialoGPT-medium"

tokenizer = AutoTokenizer.from_pretrained(checkpoint)

model = AutoModelForCausalLM.from_pretrained(checkpoint)

{"model_id":"1a57186592b5425f8ac1b71a1816d40c","version_major":2,"version_min
or":0}

{"model_id":"f3a07af02cc543b8a202d8a148e9e326","version_major":2,"version_min
or":0}

{"model_id":"324f2626dc0e44988d1bbce09b8d78a9","version_major":2,"version_min
or":0}

{"model_id":"b517ddd83b2c4e2092def32ec5f05594","version_major":2,"version_min
or":0}

{"model_id":"0e4cdb3b7d784b2293d5399227a24338","version_major":2,"version_min
or":0}

class ChatBot():

def __init__(self):

self.chat_history_ids = None

self.bot_input_ids = None

self.end_chat = False

self.welcome()

def welcome(self):
print("Initializing ChatBot ...")

time.sleep(2)
print('Type "bye" or "quit" or "exit" to end chat \n')

time.sleep(3)

greeting = np.random.choice([
"Welcome, I am ChatBot, here for your kind service",
"Hey, Great day! I am your virtual assistant",
"Hello, it's my pleasure meeting you",
"Hi, I am a ChatBot. Let's chat!"
])
print("ChatBot >> " + greeting)

def user_input(self):

text = input("User >> ")

if text.lower().strip() in ['bye', 'quit', 'exit']:

self.end_chat=True

print('ChatBot >> See you soon! Bye!')


time.sleep(1)
print('\nQuitting ChatBot ...')
else:

self.new_user_input_ids = tokenizer.encode(text +
tokenizer.eos_token, \
return_tensors='pt')

def bot_response(self):

if self.chat_history_ids is not None:


self.bot_input_ids = torch.cat([self.chat_history_ids,
self.new_user_input_ids], dim=-1)
else:

self.bot_input_ids = self.new_user_input_ids

self.chat_history_ids = model.generate(self.bot_input_ids,
max_length=1000, \

pad_token_id=tokenizer.eos_token_id)

response = tokenizer.decode(self.chat_history_ids[:,
self.bot_input_ids.shape[-1]:][0], \
skip_special_tokens=True)

if response == "":
response = self.random_response()

print('ChatBot >> '+ response)

def random_response(self):
i = -1
response = tokenizer.decode(self.chat_history_ids[:,
self.bot_input_ids.shape[i]:][0], \
skip_special_tokens=True)

while response == '':


i = i-1
response = tokenizer.decode(self.chat_history_ids[:,
self.bot_input_ids.shape[i]:][0], \
skip_special_tokens=True)

if response.strip() == '?':
reply = np.random.choice(["I don't know",
"I am not sure"])

else:
reply = np.random.choice(["Great",
"Fine. What's up?",
"Okay"
])
return reply

bot = ChatBot()

while True:

bot.user_input()

if bot.end_chat:
break

bot.bot_response()

Initializing ChatBot ...


Type "bye" or "quit" or "exit" to end chat

ChatBot >> Hey, Great day! I am your virtual assistant

User >> How are you?

ChatBot >> I'm good, how are you?

User >> I am fine. My name is Hanzla.

ChatBot >> Nice to meet you Hanzla.

User >> Nice to meet you too. I study in Delhi India

ChatBot >> Nice to meet you too.

User >> what is your favourite book?

ChatBot >> I don't read books.


User >> It was nice to meet you. Bye!

ChatBot >> Bye!

User >> exit

ChatBot >> See you soon! Bye!

Quitting ChatBot ...

You might also like