2023-12-10 13:56:56 +05:30
|
|
|
import functions_framework
|
|
|
|
|
# from google.cloud.firestore_v1.base_query import FieldFilter
|
|
|
|
|
# from firebase_admin import initialize_app, credentials, firestore
|
2023-08-21 19:14:24 +05:30
|
|
|
import os
|
2023-12-10 13:56:56 +05:30
|
|
|
from datetime import datetime
|
|
|
|
|
import pickle
|
|
|
|
|
# import pandas as pd
|
|
|
|
|
|
|
|
|
|
@functions_framework.http
|
|
|
|
|
def my_function(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': 'GET',
|
|
|
|
|
'Access-Control-Allow-Headers': 'Content-Type',
|
|
|
|
|
# 'Access-Control-Max-Age': '3600'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ('', 204, headers)
|
|
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
|
'Access-Control-Allow-Origin': '*'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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': 0.231057205, 'deviceRatio': 0.231057205,'led1Buffer': 23776.33, 'led2Buffer': 26401.67,
|
|
|
|
|
'led1Sample': 16286, 'led2Sample': 6952.67}, index=[0])
|
|
|
|
|
|
|
|
|
|
predicted_result = loaded_model.predict(input_data)
|
|
|
|
|
print(predicted_result.item())
|
2023-08-21 19:14:24 +05:30
|
|
|
|
2023-12-10 13:56:56 +05:30
|
|
|
# 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 ('prediction applied: {}!'.format(0), 200, headers)
|