https://github.com/sebiwtt/flaskriver
This is a repository for the open-source project flaskriver which will make it easier to combine the lightweight web-framework flask with the online-ML library river.
https://github.com/sebiwtt/flaskriver
data-science flask machine-learning online-learning python river streaming
Last synced: 3 months ago
JSON representation
This is a repository for the open-source project flaskriver which will make it easier to combine the lightweight web-framework flask with the online-ML library river.
- Host: GitHub
- URL: https://github.com/sebiwtt/flaskriver
- Owner: sebiwtt
- License: mit
- Created: 2022-09-13T06:36:35.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-09-20T11:00:14.000Z (about 3 years ago)
- Last Synced: 2025-04-24T11:03:05.978Z (6 months ago)
- Topics: data-science, flask, machine-learning, online-learning, python, river, streaming
- Language: Python
- Homepage: https://flaskriver.ml
- Size: 66.4 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![]()
This is a repository for the open-source project Flaskriver. It combines the lightweight web-framework Flask with the online-ML library River. With this project, I want to make deploying online-ML models to the web easier and quicker.## Introduction
### Installation
First, you will have to install the package via pip:```sh
pip3 install flaskriver
```### Hosting a model
The following code will spin up a development server which is providing a logistic regression model. You can reach its endpoints at:
- http://localhost/predict:5000
- http://localhost/train:5000
- http://localhost/metric:5000```python
from flaskriver import ClassificationInterface
from flask import Flask
from river import linear_model, metricsmodel = linear_model.LogisticRegression()
mae = metrics.MAE()
accuracy = metrics.Accuracy()
metrics = [mae, accuracy]interface = ClassificationInterface(model, metrics)
app = Flask(__name__)
interface.registerToApp(app)if __name__ == "__main__":
app.run(host="localhost", debug=True)
```At these endpoints, the app will await the training data as the JSON payload of the request (since river models work with dictionaries). The payload for training the model could look something like this:
```json
{
"features":{
"x1":300,
"x2":210
},
"target":false
}
```The most important thing about the payload for training is the keys "features" and "target". Without these, the model won't know what to learn. Training a model will not return anything but a 201 response (no payload).
The Payload for predicting a value would then look like this:
```json
{
"x1":100,
"x2":150
}
```A request to the prediction endpoint along with a payload like the one shown above would result in a response containing the predicted value under the key "y_pred".
### Sending data to the model
With the following code, you can set up a small client which goes through an entire dataset (the "Phishing" dataset which comes with River) and incrementally trains the model. While training the model the specified metrics will be updated constantly. These metrics can be queried using the /metric endpoint. A request to this endpoint will result in a response containing all the metrics under an identically named key. So in this example, the response payload would contain the two keys "MAE" and "Accuracy" along with their values at this point in time.```python
import requests
from river import datasetsdataset = datasets.Phishing()
train_url = f"http://localhost:5000/train"
metric_url = f"http://localhost:5000/metric"for x, y in dataset:
payload = {"features": x, "target": y}
response = requests.post(train_url, json=payload)response = requests.get(metric_url)
print(response.json())
```## More information
### Documentation
You can find more detailed documentation for Flaskriver at flaskriver.ml### Package
The Package source and build is available on PyPI so that it can be insalled via pip.### Repository
If you're wondering why there is a .gitlab-ci.yml file in the repository, don't worry. I did not mix up GitLab-CI and GitHub Actions ;) Since I am far more familiar with GitLab-CI I decided to create a repository there for running the automated CI-Pipeline. But with each push/merge to the main branch, all the code will be uploaded to this public GitHub repo as well. There is no extra code in the GitLab repository.## Contributing
If you would like to contribute something to the project feel free to share your ideas in form of an issue. You can also reach out to me directly via e-mail.