Skip to content

Commit

Permalink
Merge pull request #162 from COS301-SE-2023/feature/retrain_for_new_u…
Browse files Browse the repository at this point in the history
…sers

Merge Feature/RetrainForNewUsers into Dev
  • Loading branch information
MrME-CodeSmith authored Sep 22, 2023
2 parents b0f4cb6 + 72702fe commit ea7583b
Show file tree
Hide file tree
Showing 14 changed files with 73 additions and 7 deletions.
66 changes: 64 additions & 2 deletions AI/Koja-AI.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import requests
import schedule as schedule
from flask import Flask, request, jsonify
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_recommenders as tfrs
import json
import time
import schedule
import datetime

import os

app = Flask(__name__)


def load_data_and_models():
f = open('test.json')
data = json.load(f)
Expand Down Expand Up @@ -58,22 +65,63 @@ def load_data_and_models():

return user_model_index, weekday_model_index, time_frame_model_index


user_model_index, weekday_model_index, time_frame_model_index = load_data_and_models()


def clean_training_data(training_data):
data = json.load(training_data)
all_events = []

for block in data:
expanded_events = []
for event in block['training'] + block['testing']:
for time_frame in event['timeFrame']:
new_event = event.copy()
new_event['startTime'] = time_frame['first'].strip()
new_event['endTime'] = time_frame['second'].strip()
new_event['category'] = event['category'].strip()
new_event['weekday'] = event['weekday'].strip()
new_event['userID'] = event['userID'].strip()
new_event['timeFrame'] = f"{new_event['startTime']}-{new_event['endTime']}"
expanded_events.append(new_event)

all_events += expanded_events

return all_events


def retrain_for_new_users(training_data):
events_data = clean_training_data(training_data)


def auto_scheduler(training_data):
now = datetime.datetime.now()
next_retrain_time = now.replace(hour=23, minute=59, second=0, microsecond=0)
if now > next_retrain_time:
next_retrain_time += datetime.timedelta(days=1)

schedule.every().day.at(next_retrain_time.strftime("%H:%M")).do(retrain_for_new_users(training_data))

while True:
schedule.run_pending()
time.sleep(1)


@app.route('/recommendations', methods=['POST'])
def recommend_categories():
if request.is_json:
data = request.get_json()
user_id = data['userID']

maxOutput = 7
max_output = 7
_, titles = user_model_index(np.array([user_id]))

recommendations = []
for category_id in titles[0][:10]:
# Convert TensorFlow EagerTensor to numpy array
category_id_np = category_id.numpy().decode("utf-8")
_, weekdays = weekday_model_index(np.array([category_id_np]), k=maxOutput)
_, weekdays = weekday_model_index(np.array([category_id_np]), k=max_output)
_, time_frames = time_frame_model_index(np.array([category_id_np]))

recommendations.append({
Expand All @@ -87,6 +135,20 @@ def recommend_categories():
return "Request was not JSON", 400


@app.route('/training-data')
def get_training_data():
api_url = "koja api endpoint"
headers = {'Authorization': 'Bearer YOUR_ACCESS_TOKEN'}
response = requests.get(api_url, headers=headers)

if response.status_code == 200:
data = response.json()
return jsonify(data)
else:
return jsonify({'error': 'Failed to fetch data'}), 500


if __name__ == "__main__":
auto_scheduler(get_training_data())
port = int(os.getenv("PORT", 6000))
app.run(host='0.0.0.0', port=port, debug=True)
2 changes: 1 addition & 1 deletion AI/category_model/fingerprint.pb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
�ޫ�ޏ�������Ϥ��ݻ������ �ݻ����� (���������2
�ޫ�ޏ�������Ϥ��ݻ������ �ݻ����� (ߓ�������2
Binary file modified AI/category_model/variables/variables.data-00000-of-00001
Binary file not shown.
Binary file modified AI/category_model/variables/variables.index
Binary file not shown.
6 changes: 5 additions & 1 deletion AI/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from abc import ABC
from typing import Dict, Text
import requests
import pandas as pd
Expand Down Expand Up @@ -129,8 +130,9 @@
candidates=all_candidates
))


# # Create a TFRS model
class CategoryRecommender(tfrs.models.Model):
class CategoryRecommender(tfrs.models.Model, ABC):
def __init__(self, user_model, category_model, weekday_model, time_frame_model, task):
super().__init__()
self.user_model = user_model
Expand All @@ -154,6 +156,7 @@ def compute_loss(self, features, training=False):
# Combine these losses
return user_loss + category_loss + weekday_loss + time_frame_loss


model = CategoryRecommender(user_model, category_model, weekday_model, time_frame_model, task)

# # Configure and train the model
Expand Down Expand Up @@ -196,3 +199,4 @@ def compute_loss(self, features, training=False):
category_model.save("category_model")
weekday_model.save("weekday_model")
time_frame_model.save("time_frame_model")

2 changes: 1 addition & 1 deletion AI/time_frame_model/fingerprint.pb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
�����������ڈ��㬁Ͻ��IJۋ �׿쩉���(ז��Ө��O2
�����������ڈ��㬁Ͻ��IJۋ �׿쩉���(��Ђ���ۅ2
Binary file modified AI/time_frame_model/variables/variables.data-00000-of-00001
Binary file not shown.
Binary file modified AI/time_frame_model/variables/variables.index
Binary file not shown.
2 changes: 1 addition & 1 deletion AI/user_model/fingerprint.pb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
��������qՃ������M���Ɏ���� �ļ�ˮ���(�񰏍ܮ��2
��������qՃ������M���Ɏ���� �ļ�ˮ���(�������r2
Binary file modified AI/user_model/variables/variables.data-00000-of-00001
Binary file not shown.
Binary file modified AI/user_model/variables/variables.index
Binary file not shown.
2 changes: 1 addition & 1 deletion AI/weekday_model/fingerprint.pb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ꓱ�����\�����▭���������� ��������(������ò=2
ꓱ�����\�����▭���������� ��������(‐��Я�D2
Binary file modified AI/weekday_model/variables/variables.data-00000-of-00001
Binary file not shown.
Binary file modified AI/weekday_model/variables/variables.index
Binary file not shown.

0 comments on commit ea7583b

Please sign in to comment.