This repository contains a Flask API that serves machine learning models for predicting power consumption of a device and detecting anomalies in the device readings. The API utilizes Firebase to store the machine learning models and provides endpoints for predicting power consumption and detecting anomalies in device readings.
To run this Flask API, please follow the instructions below:
- Clone this repository
git clone <repository-url>
- Navigate into the repository
cd <repository-name>
- Install the required packages
pip install -r requirements.txt
- Set up Firebase credentials and download the json file and placing it in the root directory of the repository.
- Start the Flask API
python app.py
To predict the power consumption of a device, send a POST request to the /cforcast endpoint with the device ID in the request body:
{
"device_id": "your device id "
}
The response will be a JSON object containing the device ID and a dictionary with the predicted power consumption for each hour of each day of the week:
{"data":{"0":[[0.7487779683051776],[0.7487779683051776],[0.5005482412399646],[0.5005482412399646],[0.4921948659275354],[0.49229498246858727],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591]],"1":[[0.7487779683051776],[0.7487779683051776],[0.5005482412399646],[0.5005482412399646],[0.4921948659275354],[0.49229498246858727],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591]],"2":[[0.7487779683051776],[0.7487779683051776],[0.5005482412399646],[0.5005482412399646],[0.4921948659275354],[0.49229498246858727],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591],[0.4945098617401591]],"3":[[1.3959007353357729],[1.3959007353357729],[0.8682316315982567],[0.8682316315982567],[0.8304635850571641],[0.8305637015982161],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788],[0.832778580869788]],"4":[[1.3841842065022611],[1.3841842065022611],[0.8565151027647446],[0.8565151027647446],[0.8186081931150939],[0.8185694465475873],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159],[0.820784325819159]],"5":[[1.0337234086713951],[1.0337234086713951],[0.5060543049338797],[0.5060543049338797],[0.46582731709719316],[0.4653197525737444],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888],[0.469749511116888]],"6":[[0.9019937126139514],[0.9019937126139514],[0.37432460887643637],[0.37432460887643637],[0.3334460292620181],[0.3330388706838465],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017],[0.33746862922699017]]},"device_id":"your device id "}
To detect anomalies in device readings, send a POST request to the /predict endpoint with the device ID and data reading in the request body
{
"device_id": "your device id ",
"data_reading": {
"i": 0.9900436818128375,
"time": 1676362048419,
"v": 0.5681495890359043
}
}
The response will be a JSON object containing the device ID, the requested value, the predicted response, and the data reading:
{
"requested device id": "nodered_9612620c856b38f3",
"requested_value": [
[
1,
"08"
]
],
"response": [
true
]
}