7
7
from data .predictions_handler import get_predictions
8
8
from loguru import logger
9
9
10
+
10
11
# Initiate app instance
11
12
app = FastAPI (title = 'Forged Or Not Forged' , version = '1.0' ,
12
13
description = 'A simple Neural Network that classifies a banknote as either forged or not' )
@@ -19,26 +20,42 @@ class Features(BaseModel):
19
20
curtosis_of_wavelet : float
20
21
entropy_of_wavelet : float
21
22
23
+
22
24
# 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 ' )
25
27
26
28
27
- # Api root/ home
29
+ # Api root or home endpoint
28
30
@app .get ('/' )
29
31
@app .get ('/home' )
30
32
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' )
31
39
return {'message' : 'Fake or Not API live!' }
32
40
33
41
34
42
# Prediction endpoint
35
43
@app .post ('/predict' )
44
+ @logger .catch () # catch any unexpected breaks
36
45
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
+ """
37
52
# retrieve incoming json data as a dictionary
38
53
new_data = incoming_data .dict ()
54
+ logger .info ('User sent some data for predictions' )
39
55
40
56
# Make predictions based on the incoming data and saved neural net
41
57
preds = get_predictions (new_data )
58
+ logger .debug ('Predictions successfully generated for the user' )
42
59
43
60
# Return the predicted class and the predicted probability
44
61
return {'predicted_class' : round (float (preds .flatten ())), 'predicted_prob' : float (preds .flatten ())}
0 commit comments