Question: ‘What do I install to run Python code’? Answer: …‘Nothing’.
So you heard AI would generate code for you, great, but then what? Well, Google will let you run it, on your browser, unlimited, for free!
“What won’t be simple, simply won’t be!”, unknown (so, Einstein)
Before we dive in, a time saving disclaimer:
If you code, trust me, this post won’t teach you anything new. This post is for those who don’t code and aren’t looking to code. Those who are looking to “use” code. So if you do code, why not tell your elder relatives about it? They’ll pick up a new skill and you’ll get to be their favorite for a bit, something like: “You’re not going to believe what Gina, my computering granddaughter, just showed me…” kinda thing.
What would we do here:
1. Introduce ChatGPT and how it would write computer code for you
2. Introducing Google Colab and how it will run your computer code
3. Show you how to get ChatGPT to generate code for you based on a simple written request within seconds
4. Show you how to run your very own code online within seconds
5. Provide you with simple resources to do it yourself
Hi adventurists of the computer code,
Have you ever wanted to have computer code automate some task for you? Or maybe you have an idea of optimizing some process but it involves too many operations for you to do yourself? Perhaps you are building something big and just want to save time by having someone else write the code for you while you chase investors? If either of these feel appealing, it may be awesome for you to know how ChatGPT can generate code for you.
ChatGPT, is a type of language model developed by OpenAI. It has been trained on a large dataset of text and is able to generate natural language text, and even code. This means that you can give ChatGPT a task description, and it will generate code that is meant to solve that task.
Fast forwarding…great, you have 20 lines of Python code, now what?
Simply generating the code isn’t enough. In order to run the code and see the results, you need a Python interpreter. One option is to install a Python interpreter on your local machine, but if you don’t have the necessary permissions or you don’t want to go through the installation process, no problemo my friend!
What you do need is a Google account…which you probably already have.
Introducing Google Colab!
Google Colab is a free, cloud-based Python interpreter that you can access with a Google account. It allows you to write and run Python code. And the best part is that it offers unlimited use, so you can use it as much as you want without worrying about running out of resources.
That’s it.
“AI won’t replace you, a person using AI will”, a tweet by too many people.
Let’s get started
So, we’ll experiment with some over simplified task just so to get the hang of things. Let’s say you have an Excel sheet with all the recent transactions from your credit card account.
*All the resources used here are made available at the bottom of this post.
The task you’re interested in: To classify each transaction by an expense category and aggregate the transactions to sum the expenses for each category. How would you automate that in a way that would run on every file of the above type?
Well, with ChatGPT, if you can articulate it, you can get it in code.
*Remark: ChatGPT may be at capacity at times. If that’s the case, I made the code and the notebook available for you in the Resources section below.
Generating Your Code with ChatGPT
You can paste this prompt to ChatGPT:
I have an Excel file called 'Chase-CREDIT.xlsx'.
The file has a single table of credit card transactions.
There are many columns in the tables, but the relevant ones are called "Transaction Date",
"Category", "Amount".
I want a Python notebook code that will run on Colab read in this file.
If the file can't be found, the code will hang and prompt the user to place the file in the folder and to let the user press any key to continue.
It will sum all the transactions amounts per category,and it will present the amount per category in a bar graph.
The categories' names on the x axis should be of an angle so to not overlap with each other.
The code should install all the dependencies before it uses them. Write the code in a single snippet.
*Mind you, ChatGPT isn’t consistent, your prompt may yield a slightly different output from it.
*As this example is just a very particular and over simplified one, at the bottom of the post there is a template for this type of tasks.
After you pasted that prompt into ChatGPT’s textbox, you should have gotten back your requested code along with explanations on what each code section does.
Running Your Code on Google Colab
Once you have the code, you can go to Google Colab and set it up.
- First, when on Google Colab, you need to create a new Python notebook by clicking on “New Notebook”.
- Then, you can upload the Excel sheet to the remote folder by clicking on the folder 📁 icon (on the left panel) and clicking the upload button.
- Next, you can copy and paste the code that ChatGPT generated into the Python notebook.
- You can then run the code by clicking “Runtime” -> “Run all”.
The code will execute, and you will see the results in the output cells. - You can skip any of the above steps other than step 2 by grabbing the code notebook I made for you. Just remember to do step 2 again after you open my notebook.
- And that’s it!
You have successfully generated and run Python code using ChatGPT and Google Colab. Using ChatGPT can save you time and effort, especially if you’re not familiar with Python coding. And with Google Colab, you have a convenient and free way to run your Python code.
P.S.
In this post you saw a simple task with exact details on how to deploy it. As you’ll experiment with your own ideas, you may find that you need to troubleshoot a bit. A code library may be missing, a code command is off, and such. Don’t despair, we programmers are used to it. Best advice is to search to error message on Google search, or describe it. Also, ChatGPT may already know the error you’re facing, so describe it to ChatGPT and it will try to instruct you.
Also, this post has a message section below for any feedback.
Resources
As promised, here are some resources for you:
- The example Excel file I was using.
Just click on “Files” -> “Download”. - The Colab notebook I created.
Use it to cut ahead of all the above and just run the code yourself. Don’t forget to upload the Excel file to it. - More about ChatGPT: By Jerry Keszka
- More about using Google Colab: By Vishakha Lall
- This is a prompt template that’s a bit more generic for describing coding tasks with Excel for ChatGPT to solve. It assumes the output you’re interested at is a graph. Just fill up the ____ parts:
I have an Excel file called '____.xlsx'.
The file has a single table of ____.
There are many columns in the tables,
but the relevant ones are called "___", "___", "___".
I want a Python notebook code that will run on Colab read in this file.
If the file can't be found, the code will hang and prompt the user to place the file in the
folder and to let the user press any key to continue.
It will ____, and it will present the ____ in a ____ graph. The categories' names on the x axis should be of an angle so to not overlap with each other.
The code should install all the dependencies
before it uses them. Write the code in a single snippet.
Some ideas for more tasks you can experiment with:
1. Identifying key text:
Your Excel table has a column where each cell holds a report (contract/form/filing/deposition). You want to identify every sentence, in each report, that has a person’s name in it along with an occupation. You want the code to create a 2nd column that would hold just these sentences for each report and save as a new Excel file.
2. Calculating distances:
With an Excel table that has addresses of all your friends/clients, have the code create another column which will tell how far each address is from your home/office. You’ll provide it with your address, and it will generate a new Excel file with the additional column.
3. Identifying ages:
Given an Excel table with patients’/clients’ reports, have the code add a column that indicates whether the person in the report is of a specific age group, e.g., are they between the ages of 18–67. Let the out put be a new Excel file with the additional column.
4. Masking passwords:
With an Excel table that has all your user names and passwords, have the code disguise some frequent sub-pattern of you passwords with an asterisk, so only you would know that “password_12_*” really means “password_12_Sxffd”.
*Pro tip: Everything we did here can be done on your smartphone, while commuting/before bedtime.
Thank you for reading!!!
This is my first post on Medium and I’d appreciate any feedback.
Anyone who follows me can quickly find their way to the future posts I’ll put up, so give it a try.
Best of luck harnessing AI to your advantage!