Week14 - Selesfia/ComputerNetwork GitHub Wiki

CloudSQL

  1. Open Google Cloud Shell -> gcloud sql connect mydb --user=root. (CloudShell1)
  2. Open another cloud shell -> gcloud sql databases create testdb --instance=mydb, -i mydb. (CloudShell2)
  3. On (CloudShell1) ->
use testdb;
CREATE TABLE info (
id INT NOT NULL AUTO_INCREMENT,
firstname VARCHAR(20),
lastname VARCHAR(20),
age VARCHAR(3),
collegename VARCHAR(150),
PRIMARY KEY (id)
);
  1. On (CloudShell2) -> mkdir cf_mysql ->cd_mysql -> Make main.py file cat > main.py -> vim main.py -> paste the following text -> esc :wq (to exit vim). Remember to change the project id
import sqlalchemy
#connection name we noted earlier
connection_name = "YOUR PROJECT_ID:us-central1:mydb"
#database name
db_name = "testdb"
db_user = "root"
db_password = "admin1234"
driver_name = 'mysql+pymysql'
query_string = dict({"unix_socket": "/cloudsql/{}".format(connection_name)})

def writeToSql(request):
   #You can change this to match your personal details
   stmt = sqlalchemy.text("INSERT INTO info ( firstname, lastname, age, collegename) values ('Sagadevan', 'Kounder', '21', 'XYZ College')")

   db = sqlalchemy.create_engine(
   sqlalchemy.engine.url.URL(
   drivername=driver_name,
   username=db_user,
   password=db_password,
   database=db_name,
   query=query_string,
   ),
   pool_size=5,
   max_overflow=2,
   pool_timeout=30,
   pool_recycle=1800
   )
   try:
      with db.connect() as conn:
        conn.execute(stmt)
        print("Insert successful")
   except Exception as e:
      print ("Some exception occured" + e)
      return 'Error: {}'.format(str(e))
   return 'ok'
  1. Make requirements.txt file cat > requirements.txt -> vim requirements.txt -> paste the following text -> esc :wq (to exit vim)
SQLAlchemy==1.3.12
PyMySQL==0.9.3
  1. gcloud functions deploy writeToSql --entry-point writeToSql --runtime python310 --trigger-http --allow-unauthenticated --no-gen2 --source . -> Go to cloud run function to check if the funtion has been created -> Deploy the function -> After successfully deploy the function go to testing and copy the url -> Paste on CloudShell2 -> If the output is ok it means the function is running perfectly
  2. Go to CloudShell1 -> select * from info

  1. On CloudShell2 -> mkdir test-flask -> cd test-flask -> touch app.yaml main.py requirements.txt
  2. app.yaml
runtime: python39
service: myflask
  1. main.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World! 2024/7/22"

if __name__ == "__main__":
    app.run(debug=True)
  1. requirements.txt
flask
  1. Back to CloudShell2 -> gcloud app deploy -> gcloud app browse -s myflask -> Click the link
  2. vim app.yaml -> :esc wq to exit vim
runtime: python39
service: default
  1. pip install flask -> python main.py

Prepare the IRIS prediction example in GAE

  1. cd test-gae -> mkdir test-iris -> touch app.yaml client.py main.py requirements.txt train_model.py
  2. train_model.py
# -*- coding: utf-8 -*-
import pickle
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn import tree

# simple demo for traing and saving model
iris=datasets.load_iris()
x=iris.data
y=iris.target

#labels for iris dataset
labels ={
  0: "setosa",
  1: "versicolor",
  2: "virginica"
}

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.25)
classifier=tree.DecisionTreeClassifier()
classifier.fit(x_train,y_train)
predictions=classifier.predict(x_test)

#export the model
model_name = 'model.pkl'
print("finished training and dump the model as {0}".format(model_name))
pickle.dump(classifier, open(model_name,'wb'))
  1. requirements.txt
scikit-learn 
flask
  1. main.py
import pickle

from flask import Flask, request, jsonify

app = Flask(__name)

# Load the model
model = pickle.load(open('model.pkl', 'rb'))
labels = {
  0: "versicolor",
  1: "setosa",
  2: "virginica"
}

@app.route("/", methods=["GET"])
def index():
    """Basic HTML response."""
    body = (
        "<html>"
        "<body style='padding: 10px;'>"
        "<h1>Welcome to my Flask API</h1>"
        "</body>"
        "</html>"
    )
    return body

@app.route('/api', methods=['POST'])
def predict():
    # Get the data from the POST request.
    data = request.get_json(force = True)
    predict = model.predict(data['feature'])
    return jsonify(predict[0].tolist())

if name == 'main__':
    app.run(debug = True, host = '0.0.0.0', port=8080)
  1. client.py
# -*- coding: utf-8 -*-
import requests
# Change the value of experience that you want to test
url = 'https://iris-predict-dot-mygcp-436602.de.r.appspot.com/api'   # 改成 127.0.0.1
feature = [[5.8, 4.0, 1.2, 0.2]]
labels ={
  0: "setosa",
  1: "versicolor",
  2: "virginica"
}

r = requests.post(url,json={'feature': feature})
print(labels[r.json()])
  1. app.yaml
runtime: python39
service: iris-predict
  1. conda activate pyhon3.9 -> pip install -r requirements.txt -> python train_model.py -> python main.py -> Open another terminal and run python client.py. Output: setosa
    Make sure the port number are the same 8080.
  2. On the terminal that run main.py -> gcloud app deploy -> Click link the target url

Docker

  1. Open Cloud Shell -> sudo docker run hello-world -> sudo docker images -> sudo docker ps -> sudo docker ps -a
  2. Go here , search apache2 -> Copy the Dcoker Pull Command
  3. Back to cloud shell -> sudo docker pull ubuntu/apache2 -> sudo docker -d -p 8080:80 ubuntu/apache2 -> sudo docker ps -> mkdir test-dockerfile -> cd test-dockerfile/ -> vim Dockerfile -> esc :wq to exit vim
FROM centos:centos7
RUN yum -y install httpd
EXPOSE 80
ADD index.html /var/www/html
CMD ['apachectl', 'DFOREGROUND']
  1. echo "hi test" > index.html -> docker build -t mywww:1.0 .

PS : Remember to delete the resources that you created if you don't use it anymore.

10/12/2024

⚠️ **GitHub.com Fallback** ⚠️