Skip to content

Commit

Permalink
Merge pull request #259 from COS301-SE-2024/feature/backend/Model_Dep…
Browse files Browse the repository at this point in the history
…loyment

Feature/backend/model deployment
  • Loading branch information
Rethakgetse-Manaka committed Aug 3, 2024
2 parents 6fabd85 + da2656f commit 470e9cb
Show file tree
Hide file tree
Showing 15 changed files with 1,592 additions and 123 deletions.
1,522 changes: 1,522 additions & 0 deletions datasets/output.csv

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions datasets/processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pandas as pd

# Define the levels based on capacity
def get_level(capacity):
if capacity >= 1600:
return 7
elif capacity >= 1400:
return 6
elif capacity >= 1200:
return 5
elif capacity >= 1000:
return 4
elif capacity >= 800:
return 3
elif capacity >= 600:
return 2
else:
return 1

# Read the CSV data into a DataFrame
file_path = 'datasets/Attendance_data(1).csv'
data = pd.read_csv(file_path)

df = pd.DataFrame(data)

# Apply the get_level function to create the Level column
df['Level'] = df['Number_Attended'].apply(get_level)

# Save the modified DataFrame back to a CSV file
df.to_csv('output.csv', index=False)

print(df)
6 changes: 5 additions & 1 deletion maestro/LSTM(1).py
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,9 @@ def predict_weekly_attendance(month, start_day):
plt.show()

# Save the model in the SavedModel format
model.save('attendance_model')
tf.saved_model.save(model, 'attendance_model/1')
# model.export('C:/Users/retha/Capstone/occupi/models/attendance_model/1')

new_model = tf.keras.models.load_model('C:/Users/retha/Capstone/occupi/attendance_model.keras')
new_model.summary()
tf.saved_model.save(new_model, 'serving/') # Save the model in the SavedModel format
2 changes: 1 addition & 1 deletion maestro/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sklearn.preprocessing import StandardScaler

# Define the URL for the TensorFlow Serving API
url = 'http://localhost:8503/v1/models/attendance_model:predict'
url = 'http://localhost:8501/v1/models/attendance_model:predict'

# Define the attendance levels based on the bin ranges
attendance_levels = ["0-300", "300-600", "600-900", "900-1200", "1200-1500", "1500-1800", "1800+"]
Expand Down
14 changes: 0 additions & 14 deletions models/Docker_Commands

This file was deleted.

17 changes: 0 additions & 17 deletions models/Dockerfile

This file was deleted.

19 changes: 0 additions & 19 deletions models/docker-compose.yml

This file was deleted.

29 changes: 0 additions & 29 deletions python-code/Docker_Commands.txt

This file was deleted.

23 changes: 0 additions & 23 deletions python-code/Dockerfile

This file was deleted.

Binary file removed python-code/__pycache__/prediction.cpython-312.pyc
Binary file not shown.
Binary file removed python-code/__pycache__/scaler.cpython-312.pyc
Binary file not shown.
24 changes: 13 additions & 11 deletions python-code/app.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# app.py
from flask import Flask, request, jsonify
import logging
from prediction import get_prediction
from scaler import scaler
from datetime import datetime, timedelta
from prediction import get_prediction
import joblib

# Initialize the Flask application
app = Flask(__name__)

# Load the scaler
scaler = joblib.load('attendance_scaler.pkl')

# Function to determine if a given date is a weekend
def is_weekend(date):
return date.weekday() >= 5

# Function to determine if a given date is a special event (placeholder logic)
def is_special_event(date):
special_events_dates = [(7, 4), (12, 25),(4,23),(12,9),(8,5),(3,6),(11,27),(3,10),(7,26)] # Example: 4th of July, Christmas
special_events_dates = [(7, 4), (12, 25), (4, 23), (12, 9), (8, 5), (3, 6), (11, 27), (3, 10), (7, 26)] # Example: 4th of July, Christmas
return 1 if (date.month, date.day) in special_events_dates else 0

@app.route('/predict', methods=['POST'])
@app.route('/predict', methods=['GET'])
def predict():
try:
# Get current date
Expand All @@ -29,8 +31,8 @@ def predict():
day_of_month = current_date.day
weekend = is_weekend(current_date)
special_event = is_special_event(current_date)
# Set factor based on special event
factor = 1.5 if special_event else 1.0
# Set factor based on special event
# factor = 1.5 if special_event else 1.0

predicted_class, predicted_attendance_level = get_prediction(day_of_week, month, day_of_month, weekend, special_event, scaler, factor)

Expand All @@ -47,7 +49,7 @@ def predict():
logging.error(f"Error in predict endpoint: {str(e)}")
return jsonify({"error": str(e)}), 500

@app.route('/predict_week', methods=['POST'])
@app.route('/predict_week', methods=['GET'])
def predict_week():
try:
# Get the current date
Expand All @@ -68,8 +70,8 @@ def predict_week():
weekend = is_weekend(date)
special_event = is_special_event(date)

# Set factor based on special event
factor = 1.5 if special_event else 1.0
# Set factor based on special event
# factor = 1.5 if special_event else 1.0

# Get prediction
predicted_class, predicted_attendance_level = get_prediction(day_of_week, month, day_of_month, weekend, special_event, scaler, factor)
Expand All @@ -92,4 +94,4 @@ def predict_week():
return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
app.run(debug=True, host='0.0.0.0', port=9000)
18 changes: 18 additions & 0 deletions python-code/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3.8'

services:
web:
build: .
ports:
- "9000:9000"
depends_on:
- model

model:
image: tensorflow/serving:latest
environment:
- MODEL_NAME=attendance_model
volumes:
- ../models/attendance_model:/models/attendance_model
ports:
- "8501:8501"
7 changes: 0 additions & 7 deletions python-code/models.config

This file was deleted.

2 changes: 1 addition & 1 deletion python-code/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging

# Define the URL for the TensorFlow Serving API
url = 'http://localhost:8501/v1/models/attendance_model:predict'
url = 'http://model:8501/v1/models/attendance_model:predict'

# Define the attendance levels based on the bin ranges
attendance_levels = ["0-300", "300-600", "600-900", "900-1200", "1200-1500", "1500-1800", "1800+"]
Expand Down

0 comments on commit 470e9cb

Please sign in to comment.