-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
413 lines (318 loc) · 11.8 KB
/
app.py
File metadata and controls
413 lines (318 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# docker build -t ml-endpoint .
# docker run --env-file .env -p 8000:8000 ml-endpoint
from fastapi import FastAPI, Header, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List, Optional
import os
import pandas as pd
import numpy as np
import tensorflow as tf
import re
from openai import OpenAI
import time
from pymongo import MongoClient
import tempfile
import bson
from datetime import datetime, timezone
from bson import ObjectId
from mangum import Mangum
ML_API_SECRET = os.environ.get("ML_API_SECRET")
# Connect to MongoDB and instantiate models collection
client = MongoClient(os.environ.get("MONGO_CONNECTION_STRING"))
db = client["spending-tracker-db"]
GLOBAL_MODEL_RETRIEVAL_VERBOSE=True
USER_MODEL_RETRIEVAL_VERBOSE=True
global_model_db = db["global_model"]
users_db = db["users"]
user_models_db = db["user_models"]
SAVE_NAME = "model_"
# SAVE_FOLDER_PATH = "./saved_models/mlp/text_embedding_3_small"
MODEL_EXTENSION = ".h5"
RULES_PATH = './data/rules.csv'
CASE_SENSITIVE_RULES_PATH = './data/case-sensitive-rules.csv'
CATEGORIES = ["Groceries", "Housing & Bills", "Finance & Fees", "Transport", "Income", "Shopping",
"Eating Out", "Entertainment", "Health & Fitness", "Transfer", "Other / Misc"]
NUM_LABELS = len(CATEGORIES)
idx_to_labels = { k : v for k, v in enumerate(CATEGORIES)}
labels_to_idx = { k : v for v, k in enumerate(CATEGORIES)}
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
EMBEDDING_MODEL = "text-embedding-3-small"
EMBEDDING_DIMS = 1536
EMBEDDING_VERBOSE=True
def save_global_model(model, collection, metadata={}):
with tempfile.NamedTemporaryFile(suffix=MODEL_EXTENSION, delete=False) as tmp:
tmp_filepath = tmp.name
try:
model.save(tmp_filepath)
# Read back as bytes
with open(tmp_filepath, "rb") as f:
model_bytes = f.read()
# Insert into MongoDB
collection.insert_one({
"modelBlob": bson.Binary(model_bytes),
"createdAt": datetime.now(timezone.utc),
"metadata": metadata,
"stable": False,
})
finally:
os.remove(tmp_filepath)
def get_global_model(collection, verbose=GLOBAL_MODEL_RETRIEVAL_VERBOSE):
start = time.time()
# Find latest model version
doc = collection.find_one(
{ "stable": True },
sort=[("createdAt", -1)]
)
model_bytes = doc["modelBlob"]
# Write bytes to a temporary file
with tempfile.NamedTemporaryFile(suffix=MODEL_EXTENSION, delete=False) as tmp:
tmp.write(model_bytes)
tmp_filepath = tmp.name
try:
loaded_model = tf.keras.models.load_model(tmp_filepath)
return loaded_model
finally:
elapsed = time.time() - start
if verbose:
print(f"Time taken to retrieve model from global_model: {elapsed:2f} seconds")
os.remove(tmp_filepath)
def save_user_model(model, collection, uid, is_auth_0_user):
with tempfile.NamedTemporaryFile(suffix=MODEL_EXTENSION, delete=False) as tmp:
tmp_filepath = tmp.name
try:
model.save(tmp_filepath)
# Read back as bytes
with open(tmp_filepath, "rb") as f:
model_bytes = f.read()
if not uid:
raise ValueError("uid must be provided")
if is_auth_0_user:
collection.update_one(
{ "auth0Uid": uid },
{
"$set": { "modelBlob": bson.Binary(model_bytes) },
"$setOnInsert": { "auth0Uid": uid, "uid": None },
},
upsert=True,
)
else:
collection.update_one(
{ "uid": ObjectId(uid) },
{
"$set": {"modelBlob": bson.Binary(model_bytes)},
"$setOnInsert": { "uid": ObjectId(uid), "auth0Uid": None },
},
upsert=True,
)
finally:
os.remove(tmp_filepath)
def get_user_model(collection, uid, is_auth0_user, verbose=USER_MODEL_RETRIEVAL_VERBOSE):
start = time.time()
if not uid:
raise ValueError(f"uid must be provided")
doc = None
if is_auth0_user:
doc = collection.find_one({ "auth0Uid": uid })
else:
doc = collection.find_one({ "uid": ObjectId(uid) })
if not doc:
return None
model_bytes = doc["modelBlob"]
if not model_bytes:
print("Model not found for user")
return None
# Write bytes to a temporary file
with tempfile.NamedTemporaryFile(suffix=MODEL_EXTENSION, delete=False) as tmp:
tmp.write(model_bytes)
tmp_filepath = tmp.name
try:
loaded_model = tf.keras.models.load_model(tmp_filepath)
return loaded_model
finally:
elapsed = time.time() - start
if verbose:
print(f"Time taken to retrieve model from users: {elapsed:2f} seconds")
os.remove(tmp_filepath)
def create_embeddings(sentences, open_ai_model=EMBEDDING_MODEL, verbose=EMBEDDING_VERBOSE):
start_time = time.time()
response = client.embeddings.create(
model=open_ai_model,
input=sentences
)
end_time = time.time()
elapsed = end_time - start_time
if verbose:
print(f"Time to embed {len(sentences)} sentences: {elapsed:.2f} seconds")
# Extract the embedding vectors into a numpy array
embeddings = np.array([object.embedding for object in response.data], dtype=np.float32)
return embeddings
def format_description(description, lower=True):
formatted_description = str(description).split('\t')[0].strip()
if lower:
formatted_description.lower()
return re.sub(r"\s+", " ", formatted_description).strip()
def save_model(model, folder_path, EXTENSION=MODEL_EXTENSION, SAVE_NAME=SAVE_NAME):
files = os.listdir(folder_path)
files = [os.path.splitext(file)[0] for file in files if not file.startswith('.')]
filename = SAVE_NAME
num = 0
if len(files) > 0:
for file in files:
num = max(num, int(file.split('_')[-1]) + 1)
filename += str(num)
if EXTENSION: filename += EXTENSION
folderpath = os.path.join(folder_path, filename)
# Convert to forward slashes
folderpath = folderpath.replace("\\", "/")
model.save(folderpath)
return folderpath
def get_latest_model(folder_path, EXTENSION=MODEL_EXTENSION, SAVE_NAME=SAVE_NAME):
files = os.listdir(folder_path)
files = [os.path.splitext(file)[0] for file in files if not file.startswith('.')]
filename = SAVE_NAME
num = 0
if len(files) > 0:
for file in files:
num = max(num, int(file.split('_')[-1]))
filename += str(num)
if EXTENSION: filename += EXTENSION
folderpath = os.path.join(folder_path, filename)
# Convert to forward slashes
folderpath = folderpath.replace("\\", "/")
return folderpath
# Load model on start
MLP_MODEL = get_global_model(global_model_db)
def predict(model, embeddings):
predictions = model.predict(embeddings)
maxProbs = predictions.max(-1)
predicted_indices = np.argmax(predictions, axis=-1)
predicted_categories = [idx_to_labels[idx] for idx in predicted_indices]
return predicted_categories, maxProbs
# Load rules
rules = pd.read_csv(RULES_PATH)
case_sensitive_rules = pd.read_csv(CASE_SENSITIVE_RULES_PATH)
# Predict request schema
class PredictItems(BaseModel):
descriptions: List[str]
modelType: str
isAuth0User: bool
uid: str
# Predict request schema
class PredictRequest(BaseModel):
predict_data: PredictItems
# Response schema
class Prediction(BaseModel):
predicted_category: str
confidence: float
# Train request schema
class TrainItems(BaseModel):
descriptions: List[str]
categories: List[str]
modelType: str
isAuth0User: bool
uid: str
# Train request schema
class TrainRequest(BaseModel):
train_data: TrainItems
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173"], # frontend URL
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/predict", response_model=List[Prediction])
def predict_route(request: PredictRequest, ml_api_secret: Optional[str] = Header(None, alias="ml_api_secret")):
if ml_api_secret != ML_API_SECRET:
raise HTTPException(status_code=401, detail="Unauthorized")
descriptions = request.predict_data.descriptions
model_type = request.predict_data.modelType
is_auth0_user = request.predict_data.isAuth0User
uid = request.predict_data.uid
# Create embeddings on formatted lowercased descriptions
embeddings = create_embeddings([format_description(description) for description in descriptions])
model = MLP_MODEL
if model_type == "clientModel":
retrieved_model = get_user_model(user_models_db, uid, is_auth0_user)
if not retrieved_model:
print("failed to retrieve client model")
save_user_model(MLP_MODEL, user_models_db, uid, is_auth0_user)
else:
print("client model successfully retrieved")
model = retrieved_model
# Predict
preds, probabilities = predict(model, embeddings)
predicted_categories = []
for i, d in enumerate(descriptions):
matches = case_sensitive_rules[
case_sensitive_rules["company_name"].apply(lambda rule: rule in d)
]
if not matches.empty:
predicted_categories.append(matches.iloc[0]["category"])
probabilities[i] = 1.0
continue
matches = rules[rules["company_name"].str.lower().apply(lambda rule: rule in d.lower())]
if not matches.empty:
predicted_categories.append(matches.iloc[0]["category"])
probabilities[i] = 1.0
continue
predicted_categories.append(preds[i])
results = [
Prediction(predicted_category=p, confidence=c)
for p, c in zip(predicted_categories, probabilities)
]
return results
@app.post("/train")
def train_route(request: TrainRequest, ml_api_secret: Optional[str] = Header(None, alias="ml_api_secret")):
if ml_api_secret != ML_API_SECRET:
raise HTTPException(status_code=401, detail="Unauthorized")
uid = request.train_data.uid
model_type = request.train_data.modelType
is_auth0_user = request.train_data.isAuth0User
global_model = MLP_MODEL
client_model = None
if model_type == "clientModel":
retrieved_model = get_user_model(user_models_db, uid, is_auth0_user)
if not retrieved_model:
print("failed to retrieve client model")
else:
print("client model successfully retrieved")
client_model = retrieved_model
global_model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
if client_model:
client_model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
embeddings = create_embeddings(request.train_data.descriptions)
categories = request.train_data.categories
labels = np.array([labels_to_idx[label] for label in categories])
global_model.fit(
embeddings,
labels,
epochs=1,
batch_size=16,
)
if client_model:
client_model.fit(
embeddings,
labels,
epochs=1,
batch_size=16,
)
if client_model:
save_user_model(client_model, user_models_db, uid, is_auth0_user)
save_global_model(global_model, global_model_db)
else:
save_user_model(global_model, user_models_db, uid, is_auth0_user)
save_global_model(global_model, global_model_db)
return { "status": "model updated" }
# Lambda handler
handler = Mangum(app)