88 lines
3.2 KiB
Python
88 lines
3.2 KiB
Python
import functions_framework
|
|
# from google.cloud.firestore_v1.base_query import FieldFilter
|
|
# from firebase_admin import initialize_app, credentials, firestore
|
|
import os
|
|
from datetime import datetime
|
|
import pickle
|
|
import pandas as pd
|
|
from flask import jsonify
|
|
|
|
# initialize_app()
|
|
|
|
@functions_framework.http
|
|
def prediction(request):
|
|
"""HTTP Cloud Function.
|
|
Args:
|
|
request (flask.Request): The request object.
|
|
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
|
|
Returns:
|
|
The response text, or any set of values that can be turned into a
|
|
Response object using `make_response`
|
|
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
|
|
"""
|
|
if request.method == 'OPTIONS':
|
|
headers = {
|
|
'Access-Control-Allow-Origin': '*',
|
|
'Access-Control-Allow-Methods': 'POST',
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
|
# 'Access-Control-Max-Age': '3600'
|
|
}
|
|
|
|
return ('', 204, headers)
|
|
|
|
headers = {
|
|
'Access-Control-Allow-Origin': '*'
|
|
}
|
|
|
|
request_json = request.get_json(silent=True)
|
|
if request_json and 'calculatedRatio' in request_json and 'deviceRatio' in request_json:
|
|
calculated_ratio = request_json['calculatedRatio']
|
|
device_ratio = request_json['deviceRatio']
|
|
led1_buffer = request_json['led1Buffer']
|
|
led2_buffer = request_json['led2Buffer']
|
|
led1_sample = request_json['led1Sample']
|
|
led2_sample = request_json['led2Sample']
|
|
else:
|
|
calculated_ratio = 0.231057205
|
|
device_ratio = 0.231057205
|
|
led1_buffer = 23776.33
|
|
led2_buffer = 26401.67
|
|
led1_sample = 16286
|
|
led2_sample = 6952.67
|
|
|
|
with open('label_encoder.pkl', 'rb') as label_encoder_file:
|
|
loaded_label_encoder = pickle.load(label_encoder_file)
|
|
|
|
with open('gaussian_naive_bayes_model.pkl', 'rb') as model_file:
|
|
loaded_model = pickle.load(model_file)
|
|
|
|
input_data = pd.DataFrame({'calculatedRatio': calculated_ratio, 'deviceRatio': device_ratio,'led1Buffer': led1_buffer, 'led2Buffer': led2_buffer,
|
|
'led1Sample': led1_sample, 'led2Sample': led2_sample}, index=[0])
|
|
|
|
predicted_result = loaded_model.predict(input_data)
|
|
print(predicted_result.item())
|
|
|
|
# db = firestore.client()
|
|
# source_collection = "testData"
|
|
|
|
# # Get all documents from the source collection
|
|
# source_docs = db.collection(source_collection).where(filter=FieldFilter("createdAt", ">=", datetime.today().strftime("%Y-%m-%d"))).stream()
|
|
# count = 0
|
|
# for doc in source_docs:
|
|
# # Extract the document ID
|
|
# doc_id = doc.id
|
|
# print(doc_id)
|
|
|
|
# # Get the document data
|
|
# doc_data = doc.to_dict()
|
|
|
|
# try:
|
|
# # Delete the document from the source collection
|
|
# db.collection(source_collection).document(doc_id).delete()
|
|
# print(f"Document with ID '{doc_id}' updated")
|
|
# count = count + 1
|
|
# except Exception as e:
|
|
# print(f"Error deleting document with ID '{doc_id}' from the source collection: {e}")
|
|
|
|
return (jsonify({"predictedClass": predicted_result.item()}), 200, headers)
|