Wednesday, November 30, 2022

Read, Send and Delete Gmails using Gmail API

 After Generating Client ID and Client Secret

1) Generate Authorization Code (One Time)

https://accounts.google.com/o/oauth2/auth?client_id=XXXXXXXX&redirect_uri=http://localhost&response_type=code&scope=https://mail.google.com/&access_type=offline

2) Generate Refresh Token (One Time)

curl --request POST --data "code=XXXXXXXX&client_id=XXXXXXXX&client_secret=XXXXXXXX&redirect_uri=http://localhost&grant_type=authorization_code" https://oauth2.googleapis.com/token

3) Generate Access token from Refresh Token (Every Time)

curl --request POST --data "client_id=XXXXXXXX&client_secret=XXXXXXXX&refresh_token=XXXXXXXX&grant_type=refresh_token" https://oauth2.googleapis.com/token

4) Read Email

List Message IDs of all emails
curl -X GET \
  'https://gmail.googleapis.com/gmail/v1/users/me/messages?q=label:inbox' \
  -H 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX'

List Content of Specific email
curl -sX GET   'https://gmail.googleapis.com/gmail/v1/users/me/messages/<msg-id>'   -H 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX' | jq -r '.payload.parts[0].body.data' | base64 --decode

5) Send Email
Mail Details in Plain Text

 
echo "From: corecodejs@gmail.com
To: corecodejs@gmail.com
Subject: Test Email Subject

This is the body of the email." > email.txt

cat email.txt

File encoded in base64

base64 email.txt | tr -d '\n' | tr '+/' '-_' > email_encoded.txt
cat email_encoded.txt

Send email
curl -X POST \
  'https://gmail.googleapis.com/gmail/v1/users/me/messages/send' \
  -H 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX' \
  -H 'Content-Type: application/json' \
  -d '{
    "raw": "'$(cat email_encoded.txt)'"
}'

6) Delete Email

curl -X DELETE \
  'https://gmail.googleapis.com/gmail/v1/users/me/messages/<msg-id>' \
  -H 'Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXX'

Sunday, November 27, 2022

IBM / MAX Text Sentiment Analysis

Run an already trained model instance

git clone https://github.com/IBM/MAX-Text-Sentiment-Classifier.git
cd MAX-Text-Sentiment-Classifier
docker build -t max-text-sentiment-classifier .
docker run -it -p 5000:5000 max-text-sentiment-classifier
It will create a build in a docker container. We can check the application on port 5000.
http://localhost:5000

Open it by a domain / subdomain or it can be accessed by http://ip:5000
It will open a web interface. Since its model is already trained and it gives you output as positive and negative.

We need to pass the sentence and it will tell you, either the sentence is positive or negative. Once docker container is running, we can send the request using curl and collect the output in response.

curl -d "{ \"text\": [ \"The Model Asset Exchange is a crucial element of a developer's toolkit.\" ]}" -X POST "http://localhost:5000/model/predict" -H "Content-Type: application/json"

Response :

{
  "status": "ok",
  "predictions": [
    [
      {
        "positive": 0.9977352619171143,
        "negative": 0.0022646968718618155
      }
    ]
  ]
}

To Train the Model :

We are already in the folder MAX-Text-Sentiment-Classifier, cd into training folder. Create a virtual env.

cd training/
pip install -r requirements.txt
python setup_max_model_training.py max-text-classifier-training-config.yaml
Now this command will need account set up in IBM Cloud.  How much the output is accurate, it depends on the training tsv file.
https://cloud.ibm.com/login
Go On Manage > Access > API Keys > generate a key
Download the file apikey.json, copy on the server and Provide your key path.

Now run the docker instance, it will be launched based on this training.
docker build -t max-text-sentiment-classifier --build-arg use_pre_trained_model=false .
docker run -it -p 5000:5000 max-text-sentiment-classifier
Open url
http://ip:5000
Now it will give us the response with neutral output too as it has been trained based on the given tsv input.

To reset everything on cloud, delete Created service and Created storage in the cloud IBM account.
Remove all keys Entries from /etc/environment, exit or close the terminal, open and login again and run

python setup_max_model_training.py max-text-classifier-training-config.yaml
Note :

https://github.com/IBM/MAX-Text-Sentiment-Classifier


Saturday, November 19, 2022

Bash Sleep Sort

#!/bin/bash

function f() {
sleep "$1"
echo "$1"

}
while [ -n "$1" ]
do
f "$1" &
shift
done
wait

 Run the script.

./file.sh 6 1 3 7 8 4 2

Output :

It will sort the numbers and print them after 'printed number' seconds.

ex- 7 will be printed after 7 seconds.