Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e45db84

Browse files
cleaned up and added docs
1 parent c879a08 commit e45db84

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

‎.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ dmypy.json
165165
# Pyre type checker
166166
.pyre/
167167

168-
.vscode/*
168+
# VS Code & Pycharm
169169
.vscode/
170-
.idea/*
171-
.idea
170+
.idea/

‎app/data/predictions_handler.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,33 @@
33
import numpy as np
44
import joblib
55
import os
6+
from loguru import logger
67

78

9+
@logger.catch
810
def get_predictions(data):
9-
print(os.getcwd())
10-
new_data = {k:[v] for k, v in data.items()}
11+
"""
12+
A function that reshapes the incoming JSON data, loads the saved model objects
13+
and returns predicted class and probability.
14+
15+
:param data: Dict with keys representing features and values representing the associated value
16+
:return: Dict with keys 'predicted_class' (class predicted) and 'predicted_prob' (probability of prediction)
17+
"""
18+
new_data = {k: [v] for k, v in data.items()}
1119

1220
new_data_df = pd.DataFrame.from_dict(new_data)
1321
new_data_df = new_data_df[['variance_of_wavelet', 'skewness_of_wavelet',
1422
'curtosis_of_wavelet', 'entropy_of_wavelet']]
1523

1624
scaler = joblib.load('app/data/scaler.joblib')
25+
logger.debug('Saved standardising object successfully loaded')
26+
1727
model = tf.keras.models.load_model('app/data/banknote_authentication_model.h5')
28+
logger.debug('Saved ANN model loaded successfully')
1829

1930
X = scaler.transform(new_data_df.values)
31+
logger.debug('Incoming data successfully standardised with saved object')
32+
2033
preds = model.predict(X)
2134

2235
return preds

‎app/main.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from data.predictions_handler import get_predictions
88
from loguru import logger
99

10+
1011
# Initiate app instance
1112
app = FastAPI(title='Forged Or Not Forged', version='1.0',
1213
description='A simple Neural Network that classifies a banknote as either forged or not')
@@ -19,26 +20,42 @@ class Features(BaseModel):
1920
curtosis_of_wavelet: float
2021
entropy_of_wavelet: float
2122

23+
2224
# Initiate logging
23-
log_format = ""
24-
logger.add(sink='/data/log_files/log.log', format=log_format, level='DEBUG')
25+
log_format = "{time} | {level} | {message} | {file} | {line} | {function} | {exception}"
26+
logger.add(sink='app/data/log_files/logs.log', format=log_format, level='DEBUG', compression='zip')
2527

2628

27-
# Api root/home
29+
# Api root or home endpoint
2830
@app.get('/')
2931
@app.get('/home')
3032
def read_home():
33+
"""
34+
Home endpoint which can be used to test the availability of the application.
35+
36+
:return: Dict with key 'message' and value 'Fake or Not API live!'
37+
"""
38+
logger.debug('User checked the root page')
3139
return {'message': 'Fake or Not API live!'}
3240

3341

3442
# Prediction endpoint
3543
@app.post('/predict')
44+
@logger.catch() # catch any unexpected breaks
3645
def get_prediction(incoming_data: Features):
46+
"""
47+
This endpoint serves the predictions based on the values received from a user and the saved model.
48+
49+
:param incoming_data: JSON with keys representing features and values representing the associated values.
50+
:return: Dict with keys 'predicted_class' (class predicted) and 'predicted_prob' (probability of prediction)
51+
"""
3752
# retrieve incoming json data as a dictionary
3853
new_data = incoming_data.dict()
54+
logger.info('User sent some data for predictions')
3955

4056
# Make predictions based on the incoming data and saved neural net
4157
preds = get_predictions(new_data)
58+
logger.debug('Predictions successfully generated for the user')
4259

4360
# Return the predicted class and the predicted probability
4461
return {'predicted_class': round(float(preds.flatten())), 'predicted_prob': float(preds.flatten())}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /