Create Your Own AI Question-Answering System using Python and Tkinter | Natural Language Processing Project Tutorial in Sinhala

Posted by


In this tutorial, we will learn how to build our very own AI question-answering system using Python and Tkinter. We will be using Natural Language Processing (NLP) techniques to analyze and understand the text input and provide relevant answers to user questions.

To get started, make sure you have Python installed on your computer. You can download Python from the official website if you don’t already have it installed. We will also be using the tkinter library for creating the user interface.

Step 1: Install Required Libraries
First, we need to install the necessary libraries for this project. Open your command prompt or terminal and run the following commands:

pip install tkinter
pip install nltk
pip install transformers

Step 2: Preprocessing Data
Before we start building the question-answering system, we need to preprocess the text data. We will use the Natural Language Toolkit (nltk) library for this task. NLTK provides various tools for text processing, such as tokenization, stopword removal, and stemming.

Create a new Python script and import the required libraries:

import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
nltk.download('punkt')
nltk.download('stopwords')

def preprocess_text(text):
    # Tokenize the text
    tokens = word_tokenize(text)

    # Remove stopwords
    stop_words = set(stopwords.words('english'))
    filtered_tokens = [word for word in tokens if word.lower() not in stop_words]

    # Stem the words
    stemmer = PorterStemmer()
    stemmed_tokens = [stemmer.stem(word) for word in filtered_tokens]

    return ' '.join(stemmed_tokens)

Step 3: Building the Question-Answering System
Now, we will build the question-answering system using the transformers library, which provides pre-trained models for Natural Language Processing tasks. We will use the ‘distilbert-base-uncased’ model for this project.

Create a new Python script and import the required libraries:

from transformers import pipeline
nlp = pipeline('question-answering', model='distilbert-base-uncased', tokenizer='distilbert-base-uncased')

Define a function to answer user questions based on the given context:

def get_answer(context, question):
    answer = nlp(question=question, context=context)
    return answer['answer']

Step 4: Creating the User Interface with Tkinter
Now, we will create the user interface for our question-answering system using the tkinter library. Create a new Python script and import the required libraries:

import tkinter as tk

def get_input():
    context = context_input.get("1.0", "end-1c")
    question = question_input.get("1.0", "end-1c")
    answer = get_answer(context, question)
    answer_output.delete(1.0, tk.END)
    answer_output.insert(tk.END, answer)

root = tk.Tk()
root.title("AI Question-Answering System")

context_label = tk.Label(root, text="Context:")
context_label.pack()
context_input = tk.Text(root, height=10, width=50)
context_input.pack()

question_label = tk.Label(root, text="Question:")
question_label.pack()
question_input = tk.Text(root, height=1, width=50)
question_input.pack()

answer_button = tk.Button(root, text="Get Answer", command=get_input)
answer_button.pack()

answer_output = tk.Text(root, height=10, width=50)
answer_output.pack()

root.mainloop()

Step 5: Testing the Question-Answering System
Run the Python script and test the question-answering system by providing a context and asking a question. The system should provide relevant answers based on the input.

Congratulations! You have successfully built your own AI question-answering system using Python and Tkinter. Feel free to experiment with different pre-trained models and customize the user interface to enhance the functionality of the system. Happy coding!

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@suranjanpolpitiya8099
28 days ago

🔅🔆👍👍👍👍👍👍👍🔆🔅

@priyaniranasinghe496
28 days ago

Superb 🎉

@Kalum_Namal
28 days ago

මේ..අයියා..දවසකට හරි සතියකට හරි එක time එකක් දාමුකො live එන්න…පුලුවන් නම්..❤