ChatGPT is a powerful natural language generation model that can create realistic and engaging text from a given prompt. It can be used for various purposes, such as writing stories, jokes, lyrics, code, and more. But did you know that you can also use chatGPT to spice up your whatsapp conversations?
In this blog post, I will show you how you can make chatGPT work in your whatsapp using a simple Python script. You will need some basic programming skills and a few tools to get started. Here are the steps:
1. Install the required libraries. You will need to install the transformers library from Hugging Face, which provides an easy way to access chatGPT and other pre-trained models. You will also need to install selenium, which is a tool for automating web browsers. You can use pip to install them:
pip install transformers
pip install selenium
2. Load the chatGPT model and tokenizer. You can use the transformers library to load the chatGPT model and tokenizer with a few lines of code. You can choose from different versions of chatGPT, such as small, medium, large, or extra large. For this example, I will use the medium one:
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
3. Connect to whatsapp web. You will need to use selenium to open a web browser and navigate to whatsapp web. You will also need to scan the QR code with your phone to log in. You can use the following code to do that:
from selenium import webdriver
driver = webdriver.Chrome() # you can use other browsers as well
driver.get("https://web.whatsapp.com/")
input("Scan the QR code and press enter")
4. Select a chat and generate a response. You will need to use selenium to find the chat element by its name and click on it. Then you will need to get the last message from the chat and pass it to the chatGPT model as a prompt. The model will generate a response and you will need to send it back to the chat using selenium. You can use the following code to do that:
import random
chat_name = "Your friend's name" # change this to the name of your chat
chat = driver.find_element_by_xpath(f"//span[@title='{chat_name}']")
chat.click()
last_message = driver.find_elements_by_class_name("_1RAno")[-1].text
prompt = tokenizer.encode(last_message + tokenizer.eos_token, return_tensors="pt")
response = model.generate(prompt, max_length=50, do_sample=True)
response = tokenizer.decode(response[0], skip_special_tokens=True)
message_box = driver.find_element_by_class_name("_13mgZ")
message_box.send_keys(response)
send_button = driver.find_element_by_class_name("_4sWnG")
send_button.click()
5. Repeat step 4 as many times as you want. You can use a loop or a function to repeat step 4 for different chats or messages. You can also modify the parameters of the model.generate() function to change the length, randomness, or temperature of the response.
That's it! You have successfully made chatGPT work in your whatsapp. Now you can have fun and creative conversations with your friends using chatGPT. Just be careful not to reveal your secret or offend anyone with your generated messages. Enjoy!
Comments
Post a Comment