Skip to content

Commit 4309d9a

Browse files
authored
Update/readme (#157)
* add ability to select different protocols for the communication medium in `README.md` * `CHANGELOG.md` updated * enhance code snippets * `CHANGELOG.md` updated * convert `pycon` code snippets to `python` snippets * merge with latest state of dev
1 parent d289ae3 commit 4309d9a

File tree

2 files changed

+47
-37
lines changed

2 files changed

+47
-37
lines changed

README.md

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,21 @@ PyMilo is an open source Python package that provides a simple, efficient, and s
8080
## Usage
8181
### Import/Export
8282
Imagine you want to train a `LinearRegression` model representing this equation: $y = x_0 + 2x_1 + 3$. You will create data points (`X`, `y`) and train your model as follows.
83-
```pycon
84-
>>> import numpy as np
85-
>>> from sklearn.linear_model import LinearRegression
86-
>>> X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
87-
>>> y = np.dot(X, np.array([1, 2])) + 3
88-
# y = 1 * x_0 + 2 * x_1 + 3
89-
>>> model = LinearRegression().fit(X, y)
90-
>>> pred = model.predict(np.array([[3, 5]]))
83+
```python
84+
import numpy as np
85+
from sklearn.linear_model import LinearRegression
86+
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
87+
y = np.dot(X, np.array([1, 2])) + 3
88+
# y = 1 * x_0 + 2 * x_1 + 3
89+
model = LinearRegression().fit(X, y)
90+
pred = model.predict(np.array([[3, 5]]))
9191
# pred = [16.] (=1 * 3 + 2 * 5 + 3)
9292
```
9393

9494
Using PyMilo `Export` class you can easily serialize and export your trained model into a JSON file.
95-
```pycon
96-
>>> from pymilo import Export
97-
>>> Export(model).save("model.json")
95+
```python
96+
from pymilo import Export
97+
Export(model).save("model.json")
9898
```
9999

100100
You can check out your model as a JSON file now.
@@ -142,10 +142,10 @@ You can check out your model as a JSON file now.
142142
You can see all the learned parameters of the model in this file and change them if you want. This JSON representation is a transparent version of your model.
143143

144144
Now let's load it back. You can do it easily by using PyMilo `Import` class.
145-
```pycon
146-
>>> from pymilo import Import
147-
>>> model = Import("model.json").to_model()
148-
>>> pred = model.predict(np.array([[3, 5]]))
145+
```python
146+
from pymilo import Import
147+
model = Import("model.json").to_model()
148+
pred = model.predict(np.array([[3, 5]]))
149149
# pred = [16.] (=1 * 3 + 2 * 5 + 3)
150150
```
151151
This loaded model is exactly the same as the original trained model.
@@ -157,44 +157,54 @@ You can easily serve your ML model from a remote server using `ML streaming` fea
157157

158158
⚠️ In order to use `ML streaming` feature, make sure you've installed the `streaming` mode of PyMilo
159159

160+
You can choose either `REST` or `WebSocket` as the communication medium protocol.
161+
160162
#### Server
161-
Let's assume you are in the remote server and you want to import the exported JSON file and start serving your model!
162-
```pycon
163-
>>> from pymilo import Import
164-
>>> from pymilo.streaming import PymiloServer
165-
>>> my_model = Import("model.json").to_model()
166-
>>> communicator = PymiloServer(model=my_model, port=8000).communicator
167-
>>> communicator.run()
163+
Let's assume you are in the remote server and you want to import the exported JSON file and start serving your model through `REST` protocol!
164+
```python
165+
from pymilo import Import
166+
from pymilo.streaming import PymiloServer, CommunicationProtocol
167+
my_model = Import("model.json").to_model()
168+
communicator = PymiloServer(
169+
model=my_model,
170+
port=8000,
171+
communication_protocol=CommunicationProtocol["REST"],
172+
).communicator
173+
communicator.run()
168174
```
169175
Now `PymiloServer` runs on port `8000` and exposes REST API to `upload`, `download` and retrieve **attributes** either **data attributes** like `model._coef` or **method attributes** like `model.predict(x_test)`.
170176

171177
#### Client
172178
By using `PymiloClient` you can easily connect to the remote `PymiloServer` and execute any functionalities that the given ML model has, let's say you want to run `predict` function on your remote ML model and get the result:
173-
```pycon
174-
>>> from pymilo.streaming import PymiloClient
175-
>>> pymilo_client = PymiloClient(mode=PymiloClient.Mode.LOCAL, server_url="SERVER_URL")
176-
>>> pymilo_client.toggle_mode(PymiloClient.Mode.DELEGATE)
177-
>>> result = pymilo_client.predict(x_test)
179+
```python
180+
from pymilo.streaming import PymiloClient, CommunicationProtocol
181+
pymilo_client = PymiloClient(
182+
mode=PymiloClient.Mode.LOCAL,
183+
server_url="SERVER_URL",
184+
communication_protocol=CommunicationProtocol["REST"],
185+
)
186+
pymilo_client.toggle_mode(PymiloClient.Mode.DELEGATE)
187+
result = pymilo_client.predict(x_test)
178188
```
179189

180-
ℹ️ If you've deployed `PymiloServer` locally (on port `8000` for instance), then `SERVER_URL` would be `http://127.0.0.1:8000`
190+
ℹ️ If you've deployed `PymiloServer` locally (on port `8000` for instance), then `SERVER_URL` would be `http://127.0.0.1:8000` or `ws://127.0.0.1:8000` based on the selected protocol for the communication medium.
181191

182192
You can also download the remote ML model into your local and execute functions locally on your model.
183193

184194
Calling `download` function on `PymiloClient` will sync the local model that `PymiloClient` wraps upon with the remote ML model, and it doesn't save model directly to a file.
185195

186-
```pycon
187-
>>> pymilo_client.download()
196+
```python
197+
pymilo_client.download()
188198
```
189199
If you want to save the ML model to a file in your local, you can use `Export` class.
190-
```pycon
191-
>>> from pymilo import Export
192-
>>> Export(pymilo_client.model).save("model.json")
200+
```python
201+
from pymilo import Export
202+
Export(pymilo_client.model).save("model.json")
193203
```
194204
Now that you've synced the remote model with your local model, you can run functions.
195-
```pycon
196-
>>> pymilo_client.toggle_mode(mode=PymiloClient.Mode.LOCAL)
197-
>>> result = pymilo_client.predict(x_test)
205+
```python
206+
pymilo_client.toggle_mode(mode=PymiloClient.Mode.LOCAL)
207+
result = pymilo_client.predict(x_test)
198208
```
199209
`PymiloClient` wraps around the ML model, either to the local ML model or the remote ML model, and you can work with `PymiloClient` in the exact same way that you did with the ML model, you can run exact same functions with same signature.
200210

dev-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ scipy>=0.19.1
44
uvicorn==0.32.0
55
fastapi==0.115.5
66
requests==2.32.3
7-
pydantic>=1.5.0
7+
pydantic==1.10
88
websockets==10.4
99
setuptools>=40.8.0
1010
vulture>=1.0

0 commit comments

Comments
 (0)