ML Consuming Blueprints¶
What are ML Consuming Blueprints?¶
ML Consuming Blueprints are templates for a retrieving predictions from a model, that is served by a Model-Serving-Service. It showcases how to consume the model predictions in a microservice architecture via the FastIoT framework.
Pytorch Regression Consuming Blueprint¶
Pytorch Regression Consuming 1
2import asyncio
3import logging
4import pprint
5import random
6
7from fastiot.core import FastIoTService, loop
8from datetime import datetime
9
10from blueprint_dev_v2.ml_lifecycle_utils.ml_lifecycle_broker_facade import request_get_prediction
11
12
13class MlConsumerService(FastIoTService):
14
15 @staticmethod
16 def _get_random_raw_datapoint() -> dict:
17 return {
18 'laborant': ["TK", "HANS", "AN", "SO"][random.randint(0, 3)],
19 'material_id': ["00000000", "11111111", "22222222", "33333333"][random.randint(0, 3)],
20 'datum': datetime.now().strftime("%d.%m.%Y, %H:%M:%S"),
21 'rohwert_1_labormessung': random.uniform(0, 30),
22 'rohwert_2_labormessung': random.uniform(0, 30),
23 'rohwert_3_labormessung': random.uniform(0, 2),
24 'aufbereiteter_wert''': 0.1,
25 }
26
27 @loop
28 async def request_prediction(self):
29 self._logger.info("Requesting prediction")
30 raw_unlabeled_datapoints = [self._get_random_raw_datapoint() for _ in range(2)]
31 self._logger.info(f"Requesting predictions for: \n{pprint.pformat(raw_unlabeled_datapoints)}")
32 predictions = await request_get_prediction(fiot_service=self, data=raw_unlabeled_datapoints)
33 self._logger.info(f"Received predictions: \n{pprint.pformat(predictions)}")
34 return asyncio.sleep(5)
35
36
37if __name__ == '__main__':
38 # Change this to reduce verbosity or remove completely to use `FASTIOT_LOG_LEVEL` environment variable to configure
39 # logging.
40 logging.basicConfig(level=logging.DEBUG)
41 MlConsumerService.main()