Cloudy in the clouds

  • frontend
  • backend
from google.cloud import vision
from google.cloud import firestore
import secrets
import proto
def on_image_upload(event, context):
file_name = event['name']
file_bucket = event['bucket']
client = vision.ImageAnnotatorClient()
image = vision.Image()
image.source.gcs_image_uri = f"gs://{file_bucket}/{file_name}"
response = client.label_detection(image=image)
labels = response.label_annotations

image_labels = {}
for label in labels:
image_labels["label_" + secrets.token_hex(8)] = proto.Message.to_dict(label)
db = firestore.Client()
db_ref = db.collection(u'atsapp_labels')
db_ref.add({ 'labels' : image_labels})
import time
import datetime
import os
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from google.cloud import storage
import firebase_admin
from firebase_admin import credentials, firestore
from PIL import Image
import numpy as np
from pandas.io.json._normalize import nested_to_record
# firebase
cred = credentials.Certificate('auth/yourKey.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
# gcs
client = storage.Client(project='yourProject')
bucket = client.get_bucket('yourBucket')
blobs = bucket.list_blobs(prefix='pics/')

# save
strFile = "my_plot.jpg"
# iterations
iterations = 7200
def firebase_reader():
users_ref = db.collection(u'atsapp_labels')
docs = users_ref.stream()
dict_temp = {}
list_temp = []

for doc in docs:
attrlabels = doc.get('labels')
flat = nested_to_record(attrlabels, sep='_')
for key, value in flat.items():
#print(key)
if 'description' in key:
dict_temp[key] = value
list_temp.append(dict_temp[key])
return list_temp

def gcs_clearner():
bucket = client.get_bucket('dj-reader')
blobs = bucket.list_blobs(prefix='pics/')
for blob in blobs:
blob.delete()
def gcs_uploader():
blob = bucket.blob('pics/my_plot.jpg')
blob.upload_from_filename('my_plot.jpg', content_type='image/jpeg')
for count in range(iterations):
time.sleep(2)
list_of_dict_values = firebase_reader()
str1 = ' '.join(list_of_dict_values)
mask = np.array(Image.open('comment.png'))
word_cloud = WordCloud(mask=mask,
collocations = False,
background_color = 'white',
width=mask.shape[1],
height=mask.shape[0]).generate(str1)
word_cloud.to_file(strFile)
gcs_uploader()
credentials.Certificate('auth/yourKey.json')
from flask import Flask
import matplotlib.pyplot as plt
import io
import base64
from google.cloud import storage
from PIL import Image
from io import BytesIO

app = Flask(__name__)

client = storage.Client.from_service_account_json('yourProject')
bucket = client.get_bucket('yourBucket')

@app.route('/')
def build_plot():
blob = bucket.get_blob('pics/my_plot.jpg')
try:
pic = blob.download_as_bytes()
img = BytesIO(pic)
reloader = str(60)
except:
blob = bucket.get_blob('static/reload.png')
pic = blob.download_as_bytes()
img = BytesIO(pic)
reloader = str(1)

plot_url = base64.b64encode(img.getvalue()).decode()
ret = '<meta http-equiv="refresh" content="{}" /><p style="text-align:center;"><img src="data:image/png;base64,{}"width="700" height="700"></p>'.format(reloader, plot_url)

return ret

if __name__ == '__main__':
app.debug = True
app.run()Graphical representation of the concept:
  • Use triggers to process the data — that will allow avoiding “cycle/sleep” practice
  • Transfer backend code to Cloud Function or Dataflow
  • The frontend must be transferred to GKE, which should lower the costs
  • “Overwrite” while reading the generated image

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store