| 2 | |
| 3 | |
| 4 | class RegressionPredictor(object): |
| 5 | __model = None |
| 6 | |
| 7 | # ensure predictor be initialized only once by using Singleton Pattern |
| 8 | def __new__(cls, *args, **kwargs): |
| 9 | if not hasattr(cls, '__instance'): |
| 10 | cls.__instance = super().__new__(cls) |
| 11 | cls.__model = load_model('app/models/regression.h5') |
| 12 | return cls.__instance |
| 13 | |
| 14 | @staticmethod |
| 15 | def predict(input_data): |
| 16 | assert RegressionPredictor.__model, \ |
| 17 | "Use 'RegressionPredictor()' to initialize before using 'RegressionPredictor.predict()'" |
| 18 | |
| 19 | x = input_data.reshape(1, 28, 28) |
| 20 | return RegressionPredictor.__model.predict(x).flatten().tolist() |
| 21 | |
| 22 | |
| 23 | class CNNPredictor(object): |