{"id":19647070,"url":"https://github.com/idsia/mlprod","last_synced_at":"2025-10-18T08:11:12.114Z","repository":{"id":79448579,"uuid":"599531164","full_name":"IDSIA/MLprod","owner":"IDSIA","description":"Machine Learning in Production","archived":false,"fork":false,"pushed_at":"2023-02-10T08:59:28.000Z","size":1116,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-05T09:23:14.635Z","etag":null,"topics":["distributed-systems","machine-learning","mlops"],"latest_commit_sha":null,"homepage":"https://machine-learning-in-production.readthedocs.io/en/latest/index.html","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/IDSIA.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-02-09T10:38:52.000Z","updated_at":"2024-10-21T14:14:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"a56dd26a-0377-4f6f-b8ae-0d2aafa9cd57","html_url":"https://github.com/IDSIA/MLprod","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IDSIA%2FMLprod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IDSIA%2FMLprod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IDSIA%2FMLprod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IDSIA%2FMLprod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IDSIA","download_url":"https://codeload.github.com/IDSIA/MLprod/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251338612,"owners_count":21573584,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distributed-systems","machine-learning","mlops"],"created_at":"2024-11-11T14:42:15.031Z","updated_at":"2025-10-18T08:11:07.068Z","avatar_url":"https://github.com/IDSIA.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Machine Learning in Production \n\n\n## Run the dockers\n\n\n### Setup\n\nIn order to work as intended, the docker-compose stack requires some setup:\n\n* A docker network named `www`. Use the following command to create it:\n\n  ```\n  docker network create www\n  ```\n\n* A [Traefik](https://doc.traefik.io/traefik/) service working on the `www` network.\n\n  _Traefik_ is a service that is capable of routing requests to web sub-domain to services built using docker. We are using it just for this purpose, although it can also perform other tasks.\n\n  To create this service, check the file `extra/docker-compose.traefik.yaml`.\n\n* A `.env` file need to be created first. This file is not included in the repository since it is server-dependant.\n\n  The content is the following:\n\n  ```\n  DOMAIN=\u003cdomain of the machine (used only for traefik labels)\u003e\n\n  CELERY_BROKER_URL=pyamqp://rabbitmq/\n  CELERY_BACKEND_URL=redis://redis/\n  CELERY_QUEUE=\n\n  DATABASE_SCHEMA=mlpdb\n  DATABASE_USER=mlp\n  DATABASE_PASS=mlp\n  DATABASE_HOST=database\n\n  DATABASE_URL=postgresql://${DATABASE_USER}:${DATABASE_PASS}@${DATABASE_HOST}/${DATABASE_SCHEMA}\n  \n  GRAFANA_ADMIN_PASS=grafana\n  ```\n\n  Remember that these password are written in a non-encripted way. This is **not** a safe solution.\n\n\n### Execute the docker\n\nThen launch the docker through the docker compose, execute the following command from the root directory of this repository:\n\n```\ndocker-compose up -d\n```\n\n\n## Generate data\n\nThis proof-of-concept software use synthetic data generated by sampling some distributions. To generate these data, just rund the following command and it will populate the `/dataset` folder with TSV (Tab Separated Value) files.\n\n```\npython dataset_generator.py\n```\n\n\n## Generate traffic\n\nIn order to simulate the use the application from of external users, the script `traffic_generator.py` can be used. \n\nBasic command to execute with default parameters is\n\n```\npython traffic_generator.py\n```\n\nSome parameters can be used to control the behavior of the users:\n\n* `--config \u003cpath\u003e` is a path to a configuration file. A configuration file is a `.tsv` (Tab Separated Value) file that contains all the parameters for the `UserData` and `UserLabeller` behavior. See the files `config/user.tsv` and `config/user_noise.tsv` for some examples.\n\n* `-p` number of parallel thread to run. Each thread will contact the application independently.\n\n* `-d` probability to have a response. If set to 1.0, it is certain that there will always be a response. If set to 0.0, the user will never set a response.\n\n* To control the waiting time use the `-tmin` and `-tmax` parameters. The number is expressed in seconds. For less than a second use decimals (i.e. 100ms is written as 0.1).\n\n  `-tmin` is the minimum amount of time to wait after a request to the application.\n\n  `-tmax` maximum amount of time to wait after a request to the application. The wait is randomly choosed between the `-tmin` and `-tmax` values. Higher values mean a slow generation of new cdata. Bigger is the difference between these two parameters and higher is the variance in the waiting time.\n\n\n## Development\n\nTo develop this application, a [Python virutal environmnet](https://docs.python.org/3/tutorial/venv.html) is highly recommended. If a development machine with Docker is not available, it is possible to use the three `requirements.txt` file to create a fully working environment:\n\n* `requirements.api.txt` contains all the packages for the API service,\n* `requirements.worker.txt` contains all the packages for the Celery worker service,\n* `requirements.txt` contains extra packages and utilities required by scripts or for the development. \n\nTo create a virtual environment using the `python-venv` package, use the following command:\n\n```\npython -m venv MLPenv\n```\n\nThen remember to **activate** the environment before launching the scripts:\n\n```\nsource ./MLPenv/bin/activate\n```\n\n\n## References\n\n### FastAPI and database interaction\n* [SQL (Relational) Databases](https://fastapi.tiangolo.com/tutorial/sql-databases/)\n* [Python ML in Production - Part 1: FastAPI + Celery with Docker](https://denisbrogg.hashnode.dev/python-ml-in-production-part-1-fastapi-celery-with-docker)\n* [First Steps with Celery](https://docs.celeryq.dev/en/stable/getting-started/first-steps-with-celery.html)\n* [Next Steps](https://docs.celeryq.dev/en/stable/getting-started/next-steps.html)\n* [Serving ML Models in Production with FastAPI and Celery](https://towardsdatascience.com/deploying-ml-models-in-production-with-fastapi-and-celery-7063e539a5db)\n* [Multi-stage builds #2: Python specifics](https://pythonspeed.com/articles/multi-stage-docker-python/#solution2-virtualenv)\n* [SQLAlchemy ORM — a more “Pythonic” way of interacting with your database](https://medium.com/dataexplorations/sqlalchemy-orm-a-more-pythonic-way-of-interacting-with-your-database-935b57fd2d4d)\n* [Events: startup - shutdown](https://fastapi.tiangolo.com/advanced/events/)\n\n### Metrics with Prometheus\n* [Overview | Prometheus](https://prometheus.io/docs/introduction/overview/)\n* [Instrumentation | Prometheus](https://prometheus.io/docs/practices/instrumentation/#counter-vs-gauge-summary-vs-histogram)\n* [prometheus/client_python | GitHub](https://github.com/prometheus/client_python)\n* [kozhushman/prometheusrock | GitHub](https://github.com/kozhushman/prometheusrock)\n\n### Grafana\n* [Provision Grafana](https://grafana.com/docs/grafana/latest/administration/provisioning/)\n* [Data Source on Startup](https://community.grafana.com/t/data-source-on-startup/8618/2)\n* [Authentication API](https://grafana.com/docs/grafana/latest/developers/http_api/auth/)\n\n## Disclaimer\n\nThis software was build as proof-of-concept and as a support material for the course _Machine Learning in Production_.\n\nIt is not intended to be used in a real production system, although some state-of-the-art best practice has been followed to implement it.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidsia%2Fmlprod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fidsia%2Fmlprod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fidsia%2Fmlprod/lists"}