• Docs >
  • 4. Putting everything together
Shortcuts

4. Putting everything together

In the code below, we put together the TrainWork, the MLServer and the Locust components in an app.py file.

from locust_component import Locust
from model_server import MLServer
from train import TrainModel

from lightning import LightningApp, LightningFlow


class TrainAndServe(LightningFlow):
    def __init__(self):
        super().__init__()
        self.train_model = TrainModel()
        self.model_server = MLServer(
            name="mnist-svm",
            implementation="mlserver_sklearn.SKLearnModel",
            workers=8,
        )
        self.performance_tester = Locust(num_users=100)

    def run(self):
        self.train_model.run()
        self.model_server.run(self.train_model.best_model_path)
        if self.model_server.alive():
            # The performance tester needs the model server to be up
            # and running to be started, so the URL is added in the UI.
            self.performance_tester.run(self.model_server.url)

    def configure_layout(self):
        return [
            {"name": "Server", "content": self.model_server.url + "/docs"},
            {"name": "Server Testing", "content": self.performance_tester},
        ]


app = LightningApp(TrainAndServe())

Run the App

To run the app, simply open a terminal and execute this command:

lightning run app docs/source/examples/model_deploy_app/app.py

Here is a gif of the UI.

../../_images/ml_server_2.gif

Congrats, you have finished the Build a Model Server example !


Find more examples