Blog

Wrapping an ml model in an API and deploying it in the cloud

Learn how to wrap machine learning models in APIs and deploy them in the cloud with Elinext's step-by-step guide for seamless integration and scalability.

ML model cloud deployment

Introduction

Prerequisites

What is FastAPI and its benefits for building cloud machine learning APIs ?

High Performance:

FastAPI ranks among the quickest Python frameworks currently available. This performance is crucial for handling high loads and ensuring responsive APIs.

Ease of Use:

FastAPI’s syntax is simple and intuitive, making it easy to learn and use. It leverages Python type hints, which improves code readability and reduces bugs.

Automatic Documentation:

FastAPI effortlessly creates interactive API documentation with the help of Swagger UI and ReDoc. This feature is invaluable for testing and understanding API endpoints.

Asynchronous Support:

FastAPI supports asynchronous programming, allowing for non-blocking code execution. This is particularly beneficial for I/O-bound operations, enhancing the overall performance of the API.

Type Safety and Validation:

Using Pydantic for data validation and serialization ensures that the data passed to and from the API is correct and well-formed, reducing runtime errors.

What is Uvicorn and its benefits for building cloud machine learning API s?

High Performance:

Uvicorn is a lightning-fast ASGI server, designed to serve asynchronous web applications . Its performance is on par with other high-performance servers like Node.js and Go.

Compatibility:

Uvicorn is fully compatible with FastAPI, making it an ideal choice for serving FastAPI applications. It supports HTTP/2 and WebSockets, providing a robust foundation for modern web applications .

Simplicity:

Uvicorn is easy to set up and use, with a straightforward command-line interface. This simplicity reduces the overhead of configuring and managing the server.

Use Google Cloud Platforms to deploy ML model as API

Security:

GCP offers advanced security features, including identity and access management, encryption, and compliance with industry standards, ensuring your application and data are secure.

Cost-Effectiveness:

GCP’s pricing model is flexible and competitive, with options for pay-as-you-go and sustained use discounts. This makes it a cost-effective choice for deploying and scaling applications.

Developer Tools:

GCP provides a suite of developer tools, including Cloud Shell, Cloud Build, and Cloud Source Repositories, which streamline the development and deployment process. In this article below we will deploy ML model as API using the Cloud Shell tool.

Creating docker-file

Use a lightweight version of Python 3.9 as the base image:

3. Upgrade pip and install necessary Python libraries, including TensorFlow, OpenCV, FastAPI, Uvicorn, and others:

4. Set the working directory inside the container to /app and copies the app.py file and folds directory from your local machine to the container:

5. Update the package lists and installs the libgl1-mesa-glx package for OpenCV and expose port 8080 and specifies the command to run the FastAPI application using Uvicorn:

Deploying machine learning cloud API on the Google Cloud Platform

Step 1: Set Up Google Cloud Platform

Create a GCP Account : If you don’t have one, create a Google Cloud Platform account.

Create a Project : Go to the GCP Console and create a new project.

Step 2: Open Cloud Shell

Access Cloud Shel l : Click on the Cloud Shell icon in the top-right corner of the GCP Console. This opens a terminal directly in your

Step 3: Authenticate

Cloud Shell will automatically authenticate you as a current user of GCP (this step just for your information).

Step 4: Build and Push Docker Image

Navigate to your Project Directory : Use the cd command to navigate to the directory containing your

Build the Docker Image : Run the following command to build your Docker image and replace PROJECT-ID with your GCP project ID, IMAGE-NAME with your desired image name, and TAG with the version tag (e.g., v1).:

Step 5: Deploy to Google Cloud Run

Deploy the Image : Run the following command to deploy your Docker image to Google Cloud Run and replace PROJECT-ID, IMAGE-NAME, SERVICE-NAME with your desired names:

Configure Deployment Settings: Follow the prompts to configure your deployment settings, such as region and authentication.

Step 6: Final results

Testing

As you can see, there are two APIs. The first is to test robotic ability. The second is just for determining the disease. To check, upload an image of the fundus. Well, let's look at the result of the experiment with 200-status. Which tells us that the image was successfully processed:

As can be seen from the results of the software, the fourth stage of the retinopathy was confirmed:

Conclusion

Contact

Discutons de votre prochain projet

Partagez le contexte et l'equipe Elinext reviendra avec les prochaines etapes.

+48

Max file size 10MB. Supported formats include DOC, DOCX, ODT, PDF, RTF, and TXT.

Required fields are marked with an asterisk.

What does our proposal include?

Join our team

Upload your CV

For public relations

PR@ELINEXT.COM