{"id":21455456,"url":"https://github.com/alvertogit/deeplearning_flask","last_synced_at":"2026-03-15T21:08:19.206Z","repository":{"id":38385640,"uuid":"161637988","full_name":"alvertogit/deeplearning_flask","owner":"alvertogit","description":"Data Science AI Artificial Intelligence Deep Learning Python Keras TensorFlow TensorFlow2 Flask Flask3 Docker NGINX Gunicorn microservices REST API Jupyter Lab Notebook GitHub Actions Ruff","archived":false,"fork":false,"pushed_at":"2024-03-31T19:34:39.000Z","size":5906,"stargazers_count":17,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-03-31T20:29:12.191Z","etag":null,"topics":["artificial-intelligence","data-science","deep-learning","docker","flask","github-actions","jupyter-lab","jupyter-notebook","keras","python","ruff","tensorflow","tensorflow2"],"latest_commit_sha":null,"homepage":"","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/alvertogit.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}},"created_at":"2018-12-13T12:45:13.000Z","updated_at":"2024-04-14T22:27:56.145Z","dependencies_parsed_at":"2024-02-18T12:25:52.791Z","dependency_job_id":"99998595-61cc-46a4-8da5-4bce294b421f","html_url":"https://github.com/alvertogit/deeplearning_flask","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/alvertogit%2Fdeeplearning_flask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvertogit%2Fdeeplearning_flask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvertogit%2Fdeeplearning_flask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alvertogit%2Fdeeplearning_flask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alvertogit","download_url":"https://codeload.github.com/alvertogit/deeplearning_flask/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226002997,"owners_count":17558157,"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":["artificial-intelligence","data-science","deep-learning","docker","flask","github-actions","jupyter-lab","jupyter-notebook","keras","python","ruff","tensorflow","tensorflow2"],"created_at":"2024-11-23T05:12:13.828Z","updated_at":"2026-03-15T21:08:19.201Z","avatar_url":"https://github.com/alvertogit.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DEEP LEARNING ON FLASK \u003c!-- omit in toc --\u003e\n\nThis repository stores a test to demonstrate skills mainly with [Python], [Keras], [Flask], [Docker], [Jupyter Notebook], [microservices], [REST API] and [GitHub Actions].\n\n- [PURPOSE](#purpose)\n- [DEPENDENCIES](#dependencies)\n  - [PYTHON VIRTUAL ENVIRONMENT](#python-virtual-environment)\n- [REPOSITORY CONTENT](#repository-content)\n- [ARCHITECTURE](#architecture)\n- [DEEP LEARNING MODEL](#deep-learning-model)\n- [HOW TO RUN DEEP LEARNING ON FLASK WITH DOCKER COMPOSE](#how-to-run-deep-learning-on-flask-with-docker-compose)\n- [TEST SERVER \\\u0026 REST API](#test-server--rest-api)\n- [CREDITS](#credits)\n\n## PURPOSE\n\nThe goal is to deploy on [Flask] a [Deep Learning] model as a microservice. The model is used to predict handwritten digits and it has been previously trained on a [Jupyter Notebook]. [REST API] are utilized to communicate with the deployed model. e.g. send image to be analized and return the generated predictions to the client. [GitHub Actions] are employed to implement CI/CD workflows in the project.\n\n## DEPENDENCIES\n\nThe code has been tested using:\n\n- [Python] (3.12): an interpreted high-level programming language for general-purpose programming.\n- [Jupyter Lab] (4.5): a web-based interactive development environment for [Jupyter Notebooks], code and data.\n- [Flask] (3.1): a microframework for [Python] based on Werkzeug, Jinja 2 and good intentions.\n- [Gunicorn] (25.1): a [Python] [WSGI] HTTP Server for UNIX.\n- [NGINX] (1.29): a free, open-source, high-performance HTTP server, reverse proxy, and IMAP/POP3 proxy server.\n- [Docker] (28.5): an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.\n- [Docker Compose] (2.40): a tool for defining and running multi-container [Docker] applications.\n- [Keras] ([TensorFlow] built-in): a high-level neural networks API, written in [Python] and capable of running on top of [TensorFlow].\n- [TensorFlow] (2.21): an open source software [Deep Learning] library for high performance numerical computation using data flow graphs.\n- [Matplotlib] (3.10): a plotting library for [Python] and its numerical mathematics extension [NumPy].\n- [NumPy] (2.4): a library for [Python], adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.\n- [Ruff] (0.15): An extremely fast Python linter and code formatter, written in Rust.\n- [scikit-image] (0.26): a collection of algorithms for image processing with [Python].\n\n### PYTHON VIRTUAL ENVIRONMENT\n\nVirtual environment (\u003cenv_name\u003e=**.venv**) can be generated from **requirements_dev.txt** file located in the repository.\n\nCommand to configure virtual environment with [venv]:\n\n```bash\n~/deeplearning_flask$ python3 -m venv .venv\n~/deeplearning_flask$ source .venv/bin/activate\n(.venv)~/deeplearning_flask$ python3 -m pip install pip==26.0.1\n(.venv)~/deeplearning_flask$ python3 -m pip install setuptools==82.0.1\n(.venv)~/deeplearning_flask$ python3 -m pip install -r requirements_dev.txt\n(.venv)~/deeplearning_flask$ pre-commit install\n```\n\n## REPOSITORY CONTENT\n\nThe repository main folder contains:\n\n```bash\ndeeplearning_flask\n├── .env.example\n├── .env.test\n├── .github\n│   └── workflows\n│       └── ci_tests.yml\n├── .gitignore\n├── .pre-commit-config.yaml\n├── app\n│   ├── app\n│   │   ├── __init__.py\n│   │   ├── api.py\n│   │   ├── model.py\n│   │   ├── static\n│   │   │   └── 4.jpg\n│   │   └── templates\n│   │       └── dlflask.html\n│   ├── config.py\n│   ├── Makefile\n│   ├── mnist_model.keras\n│   ├── server.py\n│   └── tests\n│       ├── __init__.py\n│       ├── conftest.py\n│       └── test_app.py\n├── Deep Learning MNIST prediction model with Keras.ipynb\n├── docker-compose.yml\n├── Dockerfile\n├── nginx\n│   └── conf.d\n│       └── local.conf\n├── pyproject.toml\n├── README.md\n├── requirements.txt\n└── requirements_dev.txt\n```\n\n## ARCHITECTURE\n\nThe architecture created with [Docker Compose] uses two different [Docker] containers for:\n\n- [NGINX].\n- [Flask] and [Gunicorn].\n\nThe following diagram illustrates the architecture in blocks:\n\n```mermaid\nflowchart LR;\n  Client\u003c--\u003eNGINX;\n  NGINX\u003c--brigde--\u003eGunicorn;\n  subgraph web;\n  Gunicorn\u003c--\u003eFlask;\n  end;\n```\n\n## DEEP LEARNING MODEL\n\nThe definition and training of the [Deep Learning] MNIST model was done through a notebook in [Jupyter Lab]. The employed notebook is stored in the main folder, to run it use the command shown below:\n\n```bash\n(.venv)~/deeplearning_flask$ jupyter lab Deep\\ Learning\\ MNIST\\ prediction\\ model\\ with\\ Keras.ipynb\n```\n\n## HOW TO RUN DEEP LEARNING ON FLASK WITH DOCKER COMPOSE\n\nThe steps and commands to run the [Deep Learning] model on the [Flask] server with [Docker Compose] are described below.\n\nBefore executing [Docker Compose] is strongly recommended to close other applications to free up resources and ports to avoid potential issues. Then [Docker Compose] can be executed to build services.\n\n```bash\n~/deeplearning_flask$ docker compose build\n```\n\nNext step consists in executing [Docker Compose] up command.\n\n```bash\n~/deeplearning_flask$ docker compose up\n```\n\nIf everything goes fine at the end it should appear something similar to:\n\n```bash\n...\n...\nweb_1    | 2020-06-04 19:30:17.818273: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version\n```\n\n## TEST SERVER \u0026 REST API\n\nThere are different ways to check that the server is running properly. One is opening a web browser such as Chrome or Firefox and paste the following URL:\n\n```bash\nhttp://127.0.0.1/\n```\n\nThe web browser should show the text \"Deep Learning on Flask\".\n\n[REST API] can be tested with [pytest] or [curl].\n\nIt is possible to execute tests of [Flask] microservice created with [pytest] from inside the [Flask] [Docker] container using [Makefile]:\n\n```bash\n~/deeplearning_flask$ docker exec -it deeplearning_flask-web-1 /bin/bash\n~/app# make test\n...\n============================= test session starts ==============================platform linux -- Python 3.12.12, pytest-9.0.2, pluggy-1.6.0\nrootdir: /app/tests\ncollected 2 items\n\ntest_app.py ..                                                           [100%]\n```\n\nThose tests are also automatically executed with CI/CD workflows implemented with [GitHub Actions] for every push and pull request in the project repository.\n\nA POST example using [curl] from outside [Docker] container is shown below:\n\n```bash\n~/deeplearning_flask$ curl -F file=@app/app/static/4.jpg -X POST 'http://127.0.0.1/api/predictlabel' | json_pp\n  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n100 11650  100   489  100 11161    321   7347  0:00:01  0:00:01 --:--:--  7664\n{\n   \"most_probable_label\" : \"4\",\n   \"predictions\" : [\n      {\n         \"label\" : \"0\",\n         \"probability\" : \"8.270098e-08\"\n      },\n      {\n         \"label\" : \"1\",\n         \"probability\" : \"0.00016669065\"\n      },\n      {\n         \"label\" : \"2\",\n         \"probability\" : \"4.821898e-05\"\n      },\n      {\n         \"label\" : \"3\",\n         \"probability\" : \"2.3290573e-05\"\n      },\n      {\n         \"label\" : \"4\",\n         \"probability\" : \"0.99914443\"\n      },\n      {\n         \"label\" : \"5\",\n         \"probability\" : \"1.4074722e-06\"\n      },\n      {\n         \"label\" : \"6\",\n         \"probability\" : \"2.4940262e-05\"\n      },\n      {\n         \"label\" : \"7\",\n         \"probability\" : \"0.0004908524\"\n      },\n      {\n         \"label\" : \"8\",\n         \"probability\" : \"4.4384862e-05\"\n      },\n      {\n         \"label\" : \"9\",\n         \"probability\" : \"5.569217e-05\"\n      }\n   ],\n   \"success\" : true\n}\n```\n\n## CREDITS\n\nauthor: alvertogit\ncopyright: 2018-2026\n\n[Python]: https://www.python.org/\n[Flask]: https://flask.palletsprojects.com/en/1.1.x/\n[Gunicorn]: https://gunicorn.org/\n[WSGI]: https://en.wikipedia.org/wiki/Web_Server_Gateway_Interface\n[NGINX]: https://www.nginx.com/\n[Docker]: https://www.docker.com/\n[microservices]: https://en.wikipedia.org/wiki/Microservices\n[REST API]: https://en.wikipedia.org/wiki/Representational_state_transfer\n[GitHub Actions]: https://github.com/features/actions\n[Docker Compose]: https://github.com/docker/compose\n[venv]: https://docs.python.org/3/library/venv.html\n[Jupyter Lab]: https://jupyter.org/\n[Jupyter Notebook]: https://jupyter.org/\n[Jupyter Notebooks]: https://jupyter.org/\n[Deep Learning]: https://en.wikipedia.org/wiki/Deep_learning\n[Keras]: https://keras.io/\n[TensorFlow]: https://www.tensorflow.org/\n[Matplotlib]: https://matplotlib.org/\n[NumPy]: https://numpy.org/\n[scikit-image]: https://scikit-image.org/\n[curl]: https://curl.haxx.se/\n[pytest]: https://docs.pytest.org/en/latest/\n[Makefile]: https://en.wikipedia.org/wiki/Makefile\n[Ruff]: https://docs.astral.sh/ruff/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvertogit%2Fdeeplearning_flask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falvertogit%2Fdeeplearning_flask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falvertogit%2Fdeeplearning_flask/lists"}