more stuff
Activate cloud shell
Run describe
verify project is working
gcloud projects describe $GOOGLE_CLOUD_PROJECT
output of command:
createTime: '2019-05-29T21:21:10.187Z'
lifecycleState: ACTIVE
name: helloml
projectId: helloml-xxxxx
projectNumber: '881692383648'
gcloud config set project $GOOGLE_CLOUD_PROJECT
gcloud app create
this will ask for the region. Go ahead and pick us-central [12]
Creating App Engine application in project [helloml-xxx] and region [us-central]....done.
Success! The app is now created. Please use `gcloud app deploy` to deploy your first app.
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/appengine/standard_python37/hello_world
git clone https://github.com/noahgift/gcp-hello-ml.git
# Update .cloudshellcustomimagerepo.json with project and image name
# TIP: enable "Boost Mode" in in Cloudshell
cloudshell env build-local
cloudshell env push
cloudshell env update-default-image
# Restart Cloudshell VM
virtualenv --python $(which python) venv
source venv/bin/activate
double check it works:
which python
/home/noah_gift/python-docs-samples/appengine/standard_python37/hello_world/venv/bin/python
pip install -r requirements.txt
this should install flask
Flask==1.0.2
this runs flask locally in gcp shell
python main.py
from flask import Flask
from flask import jsonify
app = Flask(__name__)
@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello I like to make AI Apps'
@app.route('/name/<value>')
def name(value):
val = {"value": value}
return jsonify(val)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
@app.route('/name/<value>')
def name(value):
val = {"value": value}
return jsonify(val)
For example, calling this route will take the word lion and pass into the name function in flask:
https://8080-dot-3104625-dot-devshell.appspot.com/name/lion
returns value in web browser:
{
value: "lion"
}
gcloud app deploy
Warning first deploy could take about 10 minutes FYI!!! you may also need to enable cloud build API.
Do you want to continue (Y/n)? y
Beginning deployment of service [default]...
╔════════════════════════════════════════════════════════════╗
╠═ Uploading 934 files to Google Cloud Storage ═╣
gcloud app logs tail -s default
Setting traffic split for service [default]...done.
Deployed service [default] to [https://helloml-xxx.appspot.com]
You can stream logs from the command line by running:
$ gcloud app logs tail -s default
$ gcloud app browse
(venv) noah_gift@cloudshell:~/python-docs-samples/appengine/standard_python37/hello_world (helloml-242121)$ gcloud app
logs tail -s default
Waiting for new log entries...
2019-05-29 22:45:02 default[20190529t150420] [2019-05-29 22:45:02 +0000] [8] [INFO] Starting gunicorn 19.9.0
2019-05-29 22:45:02 default[20190529t150420] [2019-05-29 22:45:02 +0000] [8] [INFO] Listening at: http://0.0.0.0:8081
(8)
2019-05-29 22:45:02 default[20190529t150420] [2019-05-29 22:45:02 +0000] [8] [INFO] Using worker: threads
2019-05-29 22:45:02 default[20190529t150420] [2019-05-29 22:45:02 +0000] [25] [INFO] Booting worker with pid: 25
2019-05-29 22:45:02 default[20190529t150420] [2019-05-29 22:45:02 +0000] [27] [INFO] Booting worker with pid: 27
2019-05-29 22:45:04 default[20190529t150420] "GET /favicon.ico HTTP/1.1" 404
2019-05-29 22:46:25 default[20190529t150420] "GET /name/usf HTTP/1.1" 200
@app.route('/html')
def html():
"""Returns some custom HTML"""
return """
<title>This is a Hello World World Page</title>
<p>Hello</p>
<p><b>World</b></p>
"""
At this point, you may want to consider creating a Makefile and do this:
touch Makefile
#this goes inside that file
install:
pip install -r requirements.txt
you also may want to setup lint:
pylint --disable=R,C main.py
------------------------------------
Your code has been rated at 10.00/10
Route looks like this:
add pandas import at top:
import pandas as pd
@app.route('/pandas')
def pandas_sugar():
df = pd.read_csv("https://raw.githubusercontent.com/noahgift/sugar/master/data/education_sugar_cdc_2003.csv")
return jsonify(df.to_dict())
When you call the route https://<yourapp>.appspot.com/pandas
you should get something like this:
21.Add this wikipedia route
import wikipedia
@app.route('/wikipedia/<company>')
def wikipedia_route(company):
result = wikipedia.summary(company, sentences=10)
return result
A. https://github.com/noahgift/recommendations/blob/master/notebooks/NLP_GCP.ipynb
B. Enable Cloud Natural Language API
C. pip install google-cloud-language
test it out in IPython
In [1]: from google.cloud import language
In [2]:
In [2]: text = "LeBron James plays for the Cleveland Cavaliers."
...: client = language.LanguageServiceClient()
...: document = language.Document(
...: content=result,
...: type_=language.Document.Type.PLAIN_TEXT)
...: encoding_type = language.EncodingType.UTF8
...: entities = client.analyze_entities(request = {'document': document, 'encoding_type': encoding_type}).entities
In [3]: entities
End to End AI API Example
from flask import Flask
from flask import jsonify
import pandas as pd
import wikipedia
app = Flask(__name__)
@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello I like to make AI Apps'
@app.route('/name/<value>')
def name(value):
val = {"value": value}
return jsonify(val)
@app.route('/html')
def html():
"""Returns some custom HTML"""
return """
<title>This is a Hello World World Page</title>
<p>Hello</p>
<p><b>World</b></p>
"""
@app.route('/pandas')
def pandas_sugar():
df = pd.read_csv("https://raw.githubusercontent.com/noahgift/sugar/master/data/education_sugar_cdc_2003.csv")
return jsonify(df.to_dict())
@app.route('/wikipedia/<company>')
def wikipedia_route(company):
# Imports the Google Cloud client library
from google.cloud import language
result = wikipedia.summary(company, sentences=10)
client = language.LanguageServiceClient()
document = language.Document(
content=result,
type_=language.Document.Type.PLAIN_TEXT)
encoding_type = language.EncodingType.UTF8
entities = client.analyze_entities(request = {'document': document, 'encoding_type': encoding_type}).entities
return str(entities)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)