Git Product home page Git Product logo

flaskriver's Introduction

logo

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:

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:

from flaskriver import ClassificationInterface
from flask import Flask
from river import linear_model, metrics

model = 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:

{
    "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:

{
    "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.

import requests
from river import datasets

dataset = 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.

flaskriver's People

Contributors

sebiwtt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

officialarijit

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.