Chatbot Using Python

Create Chatbot Using Python

Chatbot Using Python Step by Step Guide

Today we are going to make a chatbot with the help of Python, so let’s learn step by step about it in an easy way. This chatbot made of Python is made using some modules like tkinter, numpy, simpledialog, you can use it for this AI project. This is a small sample of chatgpt, bard.

Chatbot has been created by using these few modules:-

  • tkinter
  • numpy
  • simpledialog

Before coding, you must have Python installed in your PC and interpreter and some modules which are very important for the chatbot to work.

Chatbot Using Python

Step 1: First let’s create some moduels and responses:-

import tkinter as tk
from tkinter import simpledialog

# Dictionary of predefined responses
responses = {
    "hello": "Hello👋! How can I assist you?",
    "how are you": "I am a chatBOT, I don't have emotions. How are you?",
    "kya name hai tumhara": "My name is codewithrandom_ka_BOT. What's your name?",
    # ... (other responses)
    "default": "I didn't understand. Could you please rephrase that?😵‍💫",
}

Chatbot Using Python

Step 2: Creating the Main Chat Loop:-

# Function to get response
def get_response(message):
    message = message.lower()
    return responses.get(message, responses["default"])

# Function to update conversation
def update_conversation(user_input, bot_response):
    conversation_text.config(state=tk.NORMAL)
    conversation_text.insert(tk.END, f"You: {user_input}\n")
    conversation_text.insert(tk.END, f"Bot: {bot_response}\n\n")
    conversation_text.see(tk.END)
    conversation_text.config(state=tk.DISABLED)

# Main loop for chat bot
def chat_loop():
    while True:
        user_input = simpledialog.askstring("Chat Bot", "You:")
        if user_input is None or user_input.lower() == "exit":
            print("Chat bot: Goodbye!")
            break

        response = get_response(user_input)
        update_conversation(user_input, response)

For this code, we will create a function for chatting and will also create a chat loop which will get response again and again.

Step 3: Creating the GUI:-

# Create main window
root = tk.Tk()
root.title("Chat Bot")

# Create Text widget to display conversation
conversation_text = tk.Text(root, wrap=tk.WORD, width=40, height=10, state=tk.DISABLED,)
conversation_text.pack(padx=10, pady=10)

# Start the chat loop
chat_loop()

# Close the main window when the loop is exited
root.destroy()

Chatbot Using Python

Using this line of code, we will create the user interface of the chatbot which will create an end button which can give input to the chatbot.

Here is full code of Chatbot Using Python:-

import tkinter as tk
from tkinter import simpledialog

# Dictionary of predefined responses
responses = {
    "hello": "Hello👋! Kaise madad kar sakta hoon?",
    "how are you": "Main ek chatBOT hoon, mujhe emotions nahi hote. Aap kaise ho?",
    "kya name hai tumhara": "Mera naam codewithrandom_ka_BOT hai. Aapka naam kya hai?",
    "bye": "Goodbye! Milte hain phir🫂.",
    "hero hai tum": "kyun! hila dala na😎",
    "codewithrandom": "codewithrandom sir aap toh hero hain😘",
    "tumhe kya pasand hai": "codewithrandom website visit karna",
    "default": "Mujhe samajh nahi aaya. Kya aap dobara acche se puch sakte hain?😵‍💫",
}

# Function to get response
def get_response(message):
    message = message.lower()
    return responses.get(message, responses["default"])

# Function to update conversation
def update_conversation(user_input, bot_response):
    conversation_text.config(state=tk.NORMAL)  # Set state to normal to allow editing
    conversation_text.insert(tk.END, f"You: {user_input}\n")
    conversation_text.insert(tk.END, f"Bot: {bot_response}\n\n")
    conversation_text.see(tk.END)  # Scroll to the end to show the latest messages
    conversation_text.config(state=tk.DISABLED)  # Set state back to disabled

# Main loop for chat bot
def chat_loop():
    while True:
        user_input = simpledialog.askstring("Chat Bot", "You:")
        if user_input is None or user_input.lower() == "exit":
            print("Chat bot: Goodbye!")
            break

        response = get_response(user_input)
        update_conversation(user_input, response)

# Create main window
root = tk.Tk()
root.title("Chat Bot")

# Create Text widget to display conversation
conversation_text = tk.Text(root, wrap=tk.WORD, width=40, height=10, state=tk.DISABLED,)
conversation_text.pack(padx=10, pady=10)

# Start the chat loop
chat_loop()
4
# Close the main window when the loop is exited
root.destroy();

Final OutPut :-

Conclusion:-

In this project, we created a chatbot using Python and some Python modules, which looks at our data, processes it and gives us a reply.

Thanks For Reading My artical
Author:- Ashutosh Mishra 

Source code Credit: nikitasilaparasetty

Using which module has this chatbot been created?

To create chatbot we have used ( tkinter, numpy, simpledialog ) modules.

Can we get our favorite reply from this chatbot?

Yes brother, you can get your favorite response from this chatbot by performing some of these functions. responses = { } .



Leave a Reply