|
| 1 | +from .encryptor import DummyEncryptor |
| 2 | +from .compressor import DummyCompressor |
| 3 | +from ..pymilo_obj import Export, Import |
| 4 | +from .communicator import RESTClientCommunicator |
| 5 | +from ..transporters.general_data_structure_transporter import GeneralDataStructureTransporter |
| 6 | +class PymiloClient: |
| 7 | + |
| 8 | + def __init__( |
| 9 | + self, |
| 10 | + model=None, |
| 11 | + mode="LOCAL", |
| 12 | + server="http://127.0.0.1", |
| 13 | + port= 8000 |
| 14 | + ): |
| 15 | + self._client_id = "0x_client_id" |
| 16 | + self._model_id = "0x_model_id" |
| 17 | + self._model = model |
| 18 | + self._mode = mode |
| 19 | + self._compressor = DummyCompressor() |
| 20 | + self._encryptor = DummyEncryptor() |
| 21 | + self._communicator = RESTClientCommunicator( |
| 22 | + server_url="{}:{}".format(server, port) |
| 23 | + ) |
| 24 | + |
| 25 | + def toggle_mode(self, mode="LOCAL"): |
| 26 | + mode = mode.upper() |
| 27 | + if mode not in ["LOCAL", "DELEGATE"]: |
| 28 | + raise Exception("Invalid mode, the given mode should be either `LOCAL`[default] or `DELEGATE`.") |
| 29 | + self._mode = mode |
| 30 | + |
| 31 | + def download(self): |
| 32 | + response = self._communicator.download({ |
| 33 | + "client_id": self._client_id, |
| 34 | + "model_id": self._model_id |
| 35 | + }) |
| 36 | + if response.status_code != 200: |
| 37 | + print("Remote model download failed.") |
| 38 | + print("Remote model downloaded successfully.") |
| 39 | + serialized_model = response.json()["payload"] |
| 40 | + self._model = Import(file_adr=None, json_dump=serialized_model).to_model() |
| 41 | + print("Local model updated successfully.") |
| 42 | + |
| 43 | + def upload(self): |
| 44 | + response = self._communicator.upload({ |
| 45 | + "client_id": self._client_id, |
| 46 | + "model_id": self._model_id, |
| 47 | + "model": Export(self._model).to_json(), |
| 48 | + }) |
| 49 | + if response.status_code == 200: |
| 50 | + print("Local model uploaded successfully.") |
| 51 | + else: |
| 52 | + print("Local model upload failed.") |
| 53 | + |
| 54 | + def __getattr__(self, attribute): |
| 55 | + if self._mode == "LOCAL": |
| 56 | + if attribute in dir(self._model): |
| 57 | + return getattr(self._model, attribute) |
| 58 | + else: |
| 59 | + raise AttributeError("This attribute doesn't exist either in PymiloClient or the inner ML model.") |
| 60 | + elif self._mode == "DELEGATE": |
| 61 | + gdst = GeneralDataStructureTransporter() |
| 62 | + def relayer(*args, **kwargs): |
| 63 | + print(f"Method '{attribute}' called with args: {args} and kwargs: {kwargs}") |
| 64 | + payload = { |
| 65 | + "client_id": self._client_id, |
| 66 | + "model_id": self._model_id, |
| 67 | + 'attribute': attribute, |
| 68 | + 'args': args, |
| 69 | + 'kwargs': kwargs, |
| 70 | + } |
| 71 | + payload["args"] = gdst.serialize(payload, "args", None) |
| 72 | + payload["kwargs"] = gdst.serialize(payload, "kwargs", None) |
| 73 | + result = self._communicator.attribute_call( |
| 74 | + self._encryptor.encrypt( |
| 75 | + self._compressor.compress( |
| 76 | + payload |
| 77 | + ) |
| 78 | + ) |
| 79 | + ).json() |
| 80 | + return gdst.deserialize(result, "payload", None) |
| 81 | + relayer.__doc__ = getattr(self._model.__class__, attribute).__doc__ |
| 82 | + return relayer |
0 commit comments