Building an AI-Powered Personal Assistant Using Selenium and OpenAI GPT-3
Today, I'm excited to share with you a fascinating project I recently completed. Using Python, Selenium, and OpenAI GPT-3, I developed a robust personal assistant that interacts with users on WhatsApp, delivering accurate responses to their queries. To make things even more interesting, this assistant is equipped with a wide range of knowledge, including software development, Python programming, data science, machine learning, and even teaching skills! Let's delve into how I brought this project to life.
The Core Components
The entire project hinged upon three main components: Selenium, OpenAI GPT-3, and Python.
- Selenium, an open-source web-based automation tool, was used to interact with the WhatsApp web interface, identifying new messages, and sending replies.
- OpenAI GPT-3 served as the backbone of the assistant, processing user queries, and generating human-like responses.
- And finally, Python, our versatile programming language, was the glue that held everything together.
The Process
The process began with setting up the Selenium WebDriver for Chrome. We used it to open the WhatsApp Web and had it wait for the user to scan the QR code and login.
driver = webdriver.Chrome() driver.get('https://web.whatsapp.com/') wait = WebDriverWait(driver, 600) print('Scan QR Code, And then Enter') input() print("Logged In")
Once logged in, the program loaded the OpenAI API key from a local JSON file, which allowed the Python script to interact with the OpenAI GPT-3 API.
with open('secrets.json') as f: data = json.load(f) openai.api_key = data['api_key']
The AI then started in a system role, informing the user about its capabilities.
To ensure our assistant responded to new messages and not old ones, I incorporated a check for timestamps on incoming messages. The assistant responded only if the message had arrived in the last 60 seconds.
timestamp = datetime.strptime(timestamp_str, "%I:%M %p")
timestamp = timestamp.replace(year=current_date.year, month=current_date.month, day=current_date.day)
current_time = datetime.now()
if (current_time - timestamp < timedelta(seconds=70)) and ...
Another challenge was to clean the messages from extra characters and ensure the assistant didn't respond to its messages. For this, we created a 'clean_text' function and used it to filter and compare the messages.
def clean_text(msg):
return msg.replace('\n', '').replace('\ue008\ue007\ue008', '').replace('`', '').replace('(Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT)', '')
We then integrated the GPT-3 model to generate responses for the incoming messages, replacing newline characters with the appropriate keys for a newline in WhatsApp.
my_message = chat_response(message_text).replace('\n', (Keys.SHIFT)+(Keys.ENTER)+(Keys.SHIFT))
msg_box.send_keys(my_message + Keys.ENTER)
And there you have it! An AI-powered personal assistant, capable of providing information and guidance on a range of topics via WhatsApp. The project was an insightful journey into how AI can be harnessed to build practical and user-friendly solutions. If you have any questions or would like to delve deeper into any aspect, please leave a comment below!
1 Comments
Interesting!!!
ReplyDeleteWill connect with you ankush to get ideas about the projects.