Skip to content

Commit c287b15

Browse files
committed
final
0 parents  commit c287b15

22 files changed

+649
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.env

Jenkinsfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
pipeline{
2+
agent any
3+
stages{
4+
5+
stage(" Install FastAPI on Test Server") {
6+
steps {
7+
ansiblePlaybook credentialsId: 'jenkins_pk', disableHostKeyChecking: true, installation: 'Ansible',
8+
inventory: 'hosts', playbook: 'playbooks/install-fast-on-test.yaml'
9+
}
10+
}
11+
12+
13+
stage(" Install FastAPI on Prod Server") {
14+
steps {
15+
ansiblePlaybook credentialsId: 'jenkins_pk', disableHostKeyChecking: true, installation: 'Ansible',
16+
inventory: 'hosts', playbook: 'playbooks/install-fast-on-prod.yaml'
17+
}
18+
}
19+
}
20+
}

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
# Electricity Consumption Forecasting and Deploy and CI/CD
3+
4+
This project has been completed as the final project of VBO's 3rd MLOps bootcamp. A time-series model was built using real-time energy consumption data acquired by EPİAŞ. Based on this model, forecasting are made for a given day and hour.
5+
6+
7+
8+
## 1. Dataset:
9+
For this project, we obtained the electricity consumption data from the "Real-Time Consumption" section of the website https://seffaflik.epias.com.tr/transparency/tuketim/gerceklesen-tuketim/gercek-zamanli-tuketim.xhtml. The dataset provides real-time electricity consumption information, which is continuously updated.
10+
11+
## 2. Modeling:
12+
### 2.1. Daily Consumption Prediction:
13+
Firstly, I developed a Machine Learning model to predict the electricity consumption for the next 5 days. This model was trained using historical consumption data to forecast future daily consumption.
14+
15+
### 2.2. Hourly Consumption Prediction:
16+
Additionally, I created another Machine Learning model to predict the electricity consumption for the next 24 hours. This model was trained using historical hourly consumption data to forecast future hourly consumption.
17+
18+
### 2.3. ML Pipeline:
19+
To automate the process of model development and prediction, I implemented an ML Pipeline. This pipeline incorporates data preprocessing, model training, and prediction steps, allowing for efficient and streamlined model updates and predictions as new data becomes available.
20+
21+
## 3. Deployment:
22+
### 3.1. API Development:
23+
I developed an API with separate endpoints for obtaining daily and hourly consumption predictions. This API enables users to retrieve electricity consumption predictions by providing parameters such as date, number of days, and hour.
24+
25+
### 3.2. Path and Query Parameters:
26+
The API accepts date, number of days, and hour as both path and query parameters. This provides flexibility to users in specifying the desired data range and receiving the corresponding consumption predictions.
27+
28+
### 3.3. FastAPI Results:
29+
The FastAPI returns the predicted electricity consumption for the requested day(s) or hour. This allows users to obtain consumption predictions for specific time intervals and plan their energy usage accordingly.
30+
31+
### 3.4. Model Concept/Data Drift:
32+
To monitor the performance of the model, I implemented a mechanism to detect concept drift and data drift. Concept drift identifies changes in data distribution to determine if the model needs to be updated, while data drift detects differences between the incoming new data and the data the model was trained on. This mechanism ensures the accuracy and reliability of the model.
33+
34+
### 3.5. Model Deployment Automation:
35+
For automating model deployment, I utilized tools such as Jenkins and Gitea. These tools facilitate model updates, automated testing, and integration of the updated model with the API. This ensures fast and secure deployment of the updated model.
36+
37+
### 3.6 Jenkins and Ansible for Server Provisioning:
38+
39+
Jenkins and Ansible enable improved traceability and reproducibility in server provisioning by automating the process and allowing for easy monitoring and reuse of installation and configuration steps. Additionally, they enhance collaboration among teams by providing a unified environment for continuous integration and sharing of configuration code.
40+
41+
42+
## 4. Infrastructure:
43+
### 4.1. Docker Container:
44+
The project leverages Docker containers as the infrastructure. Docker containers provide portability and isolation, allowing the project to run smoothly across different environments.
45+
46+
### 4.2. MySQL Database:
47+
I used MySQL as the database system. MySQL is a relational database system used to store and manage the consumption data for the project. The database stores the prediction results, enabling further analysis and future updates.
48+
49+
### 4.3. Writing Prediction Results to the Database:
50+
The prediction results returned by the FastAPI are written to the MySQL database. This allows the storage of prediction data for future reporting, analysis, or updates.
51+
52+
This project utilizes Machine Learning models to predict electricity consumption and makes them accessible through an FastAPI. It also incorporates a mechanism to track data and model drift and provides an infrastructure for automated model deployment. The project offers a valuable tool for energy consumption analysis and planning.

hosts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[all:vars]
2+
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
3+
ansible_connection = ssh
4+
ansible_python_interpreter = /usr/bin/python3
5+
6+
[172.18.0.8]
7+
test ansible_host=test ansible_user=test_user
8+
9+
[172.18.0.7]
10+
prod ansible_host=prod ansible_user=prod_user

playbooks/.DS_Store

6 KB
Binary file not shown.

playbooks/install-fast-on-prod.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
- hosts: prod
2+
become: yes
3+
tasks:
4+
- name: Install rsync
5+
yum:
6+
name: rsync
7+
state: latest
8+
- name: Copy files to remote server
9+
synchronize:
10+
src: src
11+
dest: /opt/fastapi
12+
- name: Copy service file
13+
synchronize:
14+
src: prod/fastapi.service
15+
dest: /etc/systemd/system/fastapi.service
16+
- name: Upgrade pip
17+
pip:
18+
name: pip
19+
state: latest
20+
executable: pip3
21+
- name: Install pip requirements
22+
pip:
23+
requirements: /opt/fastapi/src/fastapi_energy_prediction/requirements.txt
24+
- name: Env variables for fastapi
25+
shell: |
26+
export LC_ALL=en_US.utf-8
27+
export LANG=en_US.utf-8
28+
- name: Check if Service Exists
29+
stat: path=/etc/systemd/system/fastapi.service
30+
register: service_status
31+
- name: Stop Service
32+
service: name=fastapi state=stopped
33+
when: service_status.stat.exists
34+
register: service_stopped
35+
- name: Start fastapi
36+
systemd:
37+
name: fastapi
38+
daemon_reload: yes
39+
state: started
40+
enabled: yes

playbooks/install-fast-on-test.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
- hosts: test
2+
become: yes
3+
tasks:
4+
- name: Install rsync
5+
yum:
6+
name: rsync
7+
state: latest
8+
9+
- name: Copy files to remote server
10+
synchronize:
11+
src: src
12+
dest: /opt/fastapi
13+
14+
- name: Copy service file
15+
synchronize:
16+
src: test/fastapi.service
17+
dest: /etc/systemd/system/fastapi.service
18+
19+
- name: Upgrade pip
20+
pip:
21+
name: pip
22+
state: latest
23+
executable: pip3
24+
25+
- name: Install pip requirements
26+
pip:
27+
requirements: /opt/fastapi/src/fastapi_energy_prediction/requirements.txt
28+
29+
- name: Env variables for fastapi
30+
shell: |
31+
export LC_ALL=en_US.utf-8
32+
export LANG=en_US.utf-8
33+
34+
- name: Check if Service Exists
35+
stat: path=/etc/systemd/system/fastapi.service
36+
register: service_status
37+
38+
- name: Stop Service
39+
service: name=fastapi state=stopped
40+
when: service_status.stat.exists
41+
register: service_stopped
42+
43+
- name: Start fastapi
44+
systemd:
45+
name: fastapi
46+
daemon_reload: yes
47+
state: started
48+
enabled: yes

playbooks/prod/fastapi.service

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=Gunicorn
3+
Documentation=https://docs.gunicorn.org/en/stable/deploy.html
4+
5+
[Service]
6+
Type=simple
7+
ExecStart=/bin/bash -c 'cd /opt/fastapi/src/fastapi_energy_prediction/ && /usr/local/bin/gunicorn main:app --workers 1 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000'
8+
ExecStop=pkill -f python3
9+
10+
[Install]
11+
WantedBy=multi-user.target

playbooks/src/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)