Create Chat GPT Project Using Python and Free Pre-trained Models using AI: Just 20 lines of code, Easy Setup 2023

Create Chat GPT

Create Chat GPT for your computer, very simply just with 20 lines of code you can create this project using python and you can use any free pre-trained models that I have mentioned below in this article according to your requirement. And the exciting part is that everything is for free, you can build this entire assistant for free. You can use this as a python or artificial intelligence projects for educational purposes.

Make your own Chat GPT

You may be wondering what are Pre-trained models?

So basically, pre-trained models are those models which are trained on large amount of data to process our user request to give an accurate answer. Larger the data, more accurate the result will be.

To know more about pre-trained models visit this article – https://innovatequests.com/pre-trained-models-and-deep-learning-with-cnn/

Let’s get started –

The most important and basic requirement to run this project using python is that, you must have python installed on your system and you must have a code editor where you can perform coding and run your program and good internet.

You can download python from here – https://www.python.org/downloads/

And I’m using VS Code – https://code.visualstudio.com/

Step 1 –

Make a folder in your local disk and name it anything of your choice like “Virtual Assistant” for example. And open the folder in your text editor (VS Code).

Step 2 –

Open terminal and check your python version using this command:

Open New Terminal VS Code
python --version

Then press enter it will show the version of your Python installed on your computer.

Now in terminal install these one by one:

1.	pip install transformers
2.	pip install torch==1.9.0
Type Commands in terminal VS Code

Here in the highlighted area paste your current python version. And pip meaning is, it is a package manager for Python programming language which allows you to simply install, upgrade and manage the python packages and their dependencies.

Step 3 –

Now create a new file and name it “app.py”.

Create New File in VS Code

In that paste the code given below and save it:

# Importing Libraries
from transformers import AutoModelForCausalLM, AutoTokenizer

# Loading the model
model_name = "microsoft/DialoGPT-medium"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Code to interact with user
while True:
    user_input = input("User: ")
    input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
    model_output = model.generate(input_ids=input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
    model_response = tokenizer.decode(model_output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
    print("ChatGPT: " + model_response)

In this code we have used python while loop so that it should keeps on interacting with the user without any break. We can also use python for loop but while loop was convenient. In this project using python and the free pre-trained models made the whole process effort less as this programming language is very easy to understand.

Paste code In VS Code

That’s it! your chat GPT is created on your local computer.

Step 4 –

This is the final step

Again, open the terminal and paste this command:

python app.py

It will start the GPT and now you can use your assistant.

These are some pre-trained models to create Chat GPT like ChatBot

GPT-2:

Code –

model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
GPT-2(Medium):

Code –

model_name = "gpt2-medium"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
GPT-2(Large):

Code –

model_name = "gpt2-large"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
GPT-3: Generative pre-trained transformer 3 (gpt-3)

Code –

model_name = "gpt3"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
GPT-Neo(125M):

Code –

model_name = "EleutherAI/gpt-neo-125M"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

You can use these free pre-trained models of your choice just replace the code and this project using python is ready to use, just remember this as I said before that, if larger the model it will perform well so please note this point. These models are from https://huggingface.co/models

Links to visit –

To know more about pre-trained models visit here – https://innovatequests.com/pre-trained-models-and-deep-learning-with-cnn/

To know what is Chat GPT and how to use it visit here – https://innovatequests.com/chat-gpt-ai-powered-chat-bot-2023/

Know what is artificial intelligence? and how it works in our real life – https://innovatequests.com/artificial-intelligence-explained-2023-cost-free-knowledge/

To explore more about innovation and artificial intelligence visit here – https://innovatequests.com/

FAQS

What is a Chat Assistant or ChatBot?

Chat assistants or Chatbots are AI based technologies where we can gain virtual assistance regarding anything. They will assist us by chatting or texting with us. Mostly they are used in websites to guide you when you face any issue in that website or software. Like Google assistant or Chat GPT, open ai based technology.

What is Python?

Python is a high level programing language. It is very easy to understand and it is a perfect coding language to start with, for beginners. Python programming is the most widely used coding language in the whole world and can be used on any platform like windows, mac or Linux.

What are pre-trained models?

Pre-trained models are those models which are trained on large amount of data to process user request to give an accurate answer. If the model is trained on a larger dataset it will give more accurate the result.

What is artificial intelligence?

Artificial intelligence is a human brain like technology consisting of various neural networks to function like a human brain. It can train it-self, predict the outcome and more which may be beyond human thinking capacity, simply artificial intelligence means man-made thinking capacity.

What is the full form of Chat GPT?

The full form of Chat GPT is Chat Generative Pre-trained Model.

2 Comments on “Create Chat GPT Project Using Python and Free Pre-trained Models using AI: Just 20 lines of code, Easy Setup 2023”

  1. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *