Prerequisite: Deploy cloud function on Google Cloud Platform
Do you search for data to train your model online? What if we tell that you can generate your own data in just a few lines on code?
All you need is a Google Cloud Platform account and know how to deploy simple code to Cloud Function. We will be using Google Sheets to store the data. You can use Cloud SQL or Google Cloud Storage Bucket or Firebase to store the data. All you need to do is enable the necessary APIs.
Enabling the APIs and creating the credentials:
- Go to Marketplace in Cloud Console.
- Click on ENABLE APIS AND SERVICES
- Then Search for Google Drive API and enable it
- Then go to the Credentials tab on the left navigation bar on the screen.
- Then click on Create Credentials then select Service Account Key
- Then create a new service account by giving it a name and set the Role to Editor under the Projects sub-field and keep the key type as JSON and click on Create button. Keep the Downloaded JSON safely.
- Again go to Dashboard and follow the same steps. This time search for Google Sheets and enable the API.
After all these steps are done your page should look something like this
Creating the Spreadsheet
- Create a Spreadsheet in Google Sheets
- The look up the downloaded JSON file for the field client_email and copy that email.
- Open the newly created spreadsheet and click on the share option and type the paste the client_email there.
So, the boring part is done. Now, lets jump into the code.
Setting up Cloud Functions
- Create a new Cloud Function and change the Runtime to Python 3.7
- Go to requirements.txt and delete the boilerplate text and add the following lines to it.
- Now the most important and the best part, writing the code. Delete the entire boilerplate code and paste the following code
# geting the variables ready
# your client_json contents as dictionary
# use creds to create a client to interact with the Google Drive API
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
"% d/% m/% Y % H:% M:% S"
Code Explanation –
-> First, we get the necessary imports. gspread is the library for performing handling of Google Sheets. Then, we are importing oauth2client. This will handle our authentication of the generated credentials. Then, we are importing datetime to log the data correctly with the current time and date.
-> Coming into the upate(request), first we add our credentials in the data dictionary. Then, we store the user request parameters in a variable called request_json and then we are initializing temp and then assigning the ‘temp’ key value into the variable.
-> We are defining our scope for the authentication. We are using oauth2client to make the necessary authentication with the specified scopes.
Then, open your Google Spreadsheet by specifying your Sheet name there. Then, we are inserting the date and time of logging and the parameter in the sheet at the specified row in index variable.
- Now type upate in the Function to execute and then click on ddeploy
After that your Cloud Function page should look like this
After this, you need to click on the function name, here function-1
Then, go to trigger tab
After that, note the URL that is shown in there. This is the URL you will send GET request along with the data parameter to add the parameter value to the spreadsheet.
Setting up IoT device
You can use Nodemcu or Arduino for sending the data to Google Sheets, but you will be requiring a WiFi module along with it and ofcourse, Raspberry Pi can also be used. Now, all you have to do is send http request to the Cloud Function URL along with the parameters, here temp or temperature. This would edit the spreadsheet and add the parameter values to the spreadsheet.
With that being said, this is how you can use Cloud Functions to log data to Google Sheets. You can do the same for Cloud SQL or any other means of storage. The stored data can be used as training data for its relevant Machine Learning model.
- Learning Model Building in Scikit-learn : A Python Machine Learning Library
- seq2seq model in Machine Learning
- Saving a machine learning Model
- Deploy Machine Learning Model using Flask
- Creating a simple machine learning model
- Machine Learning Computing at the edge using model artifacts
- Python | Generate test datasets for Machine learning
- How Does Google Use Machine Learning?
- Difference between Big Data and Machine Learning
- ML | Introduction to Data in Machine Learning
- Machine Learning and Data Science
- Relationship between Data Mining and Machine Learning
- Difference between Data Science and Machine Learning
- Data Preprocessing for Machine learning in Python
- Difference Between Data mining and Machine learning
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.