Cloudy in the clouds

  • frontend
  • backend
from import vision
from 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 import storage
import firebase_admin
from firebase_admin import credentials, firestore
from PIL import Image
import numpy as np
from import nested_to_record
# firebase
cred = credentials.Certificate('auth/yourKey.json')
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 =
dict_temp = {}
list_temp = []

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

def gcs_clearner():
bucket = client.get_bucket('dj-reader')
blobs = bucket.list_blobs(prefix='pics/')
for blob in blobs:
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):
list_of_dict_values = firebase_reader()
str1 = ' '.join(list_of_dict_values)
mask = np.array('comment.png'))
word_cloud = WordCloud(mask=mask,
collocations = False,
background_color = 'white',
from flask import Flask
import matplotlib.pyplot as plt
import io
import base64
from 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')

def build_plot():
blob = bucket.get_blob('pics/my_plot.jpg')
pic = blob.download_as_bytes()
img = BytesIO(pic)
reloader = str(60)
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 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