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

In one of the previous articles on AI solutions, the creation of a convolutional neural network for Determining the degree of diabetic retinopathy was shown. This article will show you how to deploy an ML model as API.

Introduction

Machine learning (ML) has become a cornerstone for building intelligent applications in today’s data-driven world. However, the true power of ML models is realized when they are deployed as services that can be accessed and utilized by other applications or users. This is where creating an ML solution as an API and deploying it in the cloud comes into play.

This article will guide you through  transforming your ML model into a robust API service and deploying it on a cloud platform. By leveraging cloud infrastructure, you can ensure scalability, reliability, and accessibility for your ML service, making it available to a broader audience. 

Wrapping a machine learning model in an API and deploying it in the cloud enables organizations to leverage the power of advanced analytics without the complexities of managing infrastructure. This approach not only enhances accessibility but also streamlines the process of creating a cloud machine learning API that can serve predictions in real-time. 

As we explore this topic, we will delve into the benefits and steps involved in creating a machine learning cloud API, making it easier for developers to harness the full potential of their models in a scalable and efficient manner.

Prerequisites

In this article, the choice fell on the following technology stack – FastAPI, Uvicorn, and Google Cloud Platform (GCP). First of all, due to ease of use. For example, below is the ASGI server startup code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
uvicorn app:app --host 127.0.0.1 --port 8080
uvicorn app:app --host 127.0.0.1 --port 8080
uvicorn app:app --host 127.0.0.1 --port 8080

Google Cloud Platform (GCP) was chosen due to its leading position in the market, a large number of manuals, as well as the ease of building docker images and their subsequent deployment using the convenient Google Cloud Run tool.

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

FastAPI is specifically designed for creating machine learning cloud APIs quickly and efficiently. It allows you to define endpoints, handle requests, and return responses with minimal code. Key Purposes of FastAPI:

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 APIs?

Uvicorn is a high-performance ASGI server used to run asynchronous web applications in Python. It’s designed to serve frameworks like FastAPI and Starlette, providing fast and efficient handling of HTTP requests. Uvicorn has some benefits:

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 

Cloud Shell is a web-based embeddable functionality at Google Cloud Platform that you can access from any browser. It features an online terminal equipped with tools like the docker, gcloud command-line tool, kubectl, and other utilities for managing your resources. For this purpose, it’s necessary to create a docker file. Where each part of it will be explained in detail below:

  1. Use a lightweight version of Python 3.9 as the base image:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
FROM python:3.9-slim
FROM python:3.9-slim
FROM python:3.9-slim

2. Set environment variables to prevent Python from writing .pyc files and to ensure unbuffered output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1 ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
RUN pip install --upgrade pip
RUN pip install tensorflow==2.12 opencv-python fastapi==0.110.0 uvicorn==0.22.0
RUN pip install efficientnet --no-dependencies
RUN pip install scikit-image --no-dependencies
RUN pip install lazy_loader --no-dependencies
RUN pip install keras_applications --no-dependencies
RUN pip install click
RUN pip install opencv-python-headless
RUN pip install python-multipart
RUN pip install --upgrade pip RUN pip install tensorflow==2.12 opencv-python fastapi==0.110.0 uvicorn==0.22.0 RUN pip install efficientnet --no-dependencies RUN pip install scikit-image --no-dependencies RUN pip install lazy_loader --no-dependencies RUN pip install keras_applications --no-dependencies RUN pip install click RUN pip install opencv-python-headless RUN pip install python-multipart
RUN pip install --upgrade pip
RUN pip install tensorflow==2.12 opencv-python fastapi==0.110.0 uvicorn==0.22.0
RUN pip install efficientnet --no-dependencies
RUN pip install scikit-image --no-dependencies
RUN pip install lazy_loader --no-dependencies
RUN pip install keras_applications --no-dependencies
RUN pip install click
RUN pip install opencv-python-headless
RUN pip install python-multipart

       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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
WORKDIR /app
COPY app.py /app
COPY folds /app/folds
RUN apt-get update
RUN apt install -y libgl1-mesa-glx
WORKDIR /app COPY app.py /app COPY folds /app/folds RUN apt-get update RUN apt install -y libgl1-mesa-glx
WORKDIR /app
COPY app.py /app
COPY folds /app/folds
RUN apt-get update
RUN apt install -y libgl1-mesa-glx

       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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
EXPOSE 8080
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
EXPOSE 8080 CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]
EXPOSE 8080
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"]

Deploying machine learning cloud API  on the Google Cloud Platform

By following these steps, it’s possible to successfully deploy your Docker image on GCP and create cloud machine learning API service using Cloud Shell:

Step 1: Set Up Google Cloud Platform

  1. Create a GCP Account: If you don’t have one, create a Google Cloud Platform account.
  2. Create a Project: Go to the GCP Console and create a new project.

Step 2: Open Cloud Shell

  1. Access Cloud Shell: 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

  1. 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

  1. Navigate to your Project Directory: Use the cd command to navigate to the directory containing your
  2. 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).:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
gcloud builds submit --tag gcr.io/PROJECT-ID/IMAGE-NAME:TAG .
gcloud builds submit --tag gcr.io/PROJECT-ID/IMAGE-NAME:TAG .
gcloud builds submit --tag gcr.io/PROJECT-ID/IMAGE-NAME:TAG .

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
gcloud run deploy SERVICE-NAME --image gcr.io/PROJECT-ID/IMAGE-NAME:TAG --platform managed
gcloud run deploy SERVICE-NAME --image gcr.io/PROJECT-ID/IMAGE-NAME:TAG --platform managed
gcloud run deploy SERVICE-NAME --image gcr.io/PROJECT-ID/IMAGE-NAME:TAG --platform managed

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

Step 6: Final results

After finishing the job for deploying a service on the cloud, the user gives the final path for using. In our case this path:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Please enter numeric choice or text value (must exactly match list item): 14
To make this the default region, run `gcloud config set run/region europe-north1`.
Allow unauthenticated invocations to [fastapi-app] (y/N)? y
Deploying container to Cloud Run service [fastapi-app] in project [nifty-realm-430309-n7] region [europe-north1]...
OK Deploying new service... Done.
OK Creating Revision...
OK Routing traffic...
OK Setting IAM Policy...
Done.
Service [fastapi-app], revision [fastapi-app-00001-xhn] has been deployed and is serving 100 percent of traffic.
Service URL: https://fastapi-app-zs1l6bqkqa-oa.a.run.app
Please enter numeric choice or text value (must exactly match list item): 14 To make this the default region, run `gcloud config set run/region europe-north1`. Allow unauthenticated invocations to [fastapi-app] (y/N)? y Deploying container to Cloud Run service [fastapi-app] in project [nifty-realm-430309-n7] region [europe-north1]... OK Deploying new service... Done. OK Creating Revision... OK Routing traffic... OK Setting IAM Policy... Done. Service [fastapi-app], revision [fastapi-app-00001-xhn] has been deployed and is serving 100 percent of traffic. Service URL: https://fastapi-app-zs1l6bqkqa-oa.a.run.app
Please enter numeric choice or text value (must exactly match list item): 14

To make this the default region, run `gcloud config set run/region europe-north1`.

Allow unauthenticated invocations to [fastapi-app] (y/N)? y

Deploying container to Cloud Run service [fastapi-app] in project [nifty-realm-430309-n7] region [europe-north1]...
OK Deploying new service... Done.
  OK Creating Revision...
  OK Routing traffic...
  OK Setting IAM Policy...
Done.

Service [fastapi-app], revision [fastapi-app-00001-xhn] has been deployed and is serving 100 percent of traffic.
Service URL: https://fastapi-app-zs1l6bqkqa-oa.a.run.app

As we can see, service is available by URL.

Testing

After finishing the deployment, let’s visit the generated service by providing the URL from the previous step. FastAPI has a built-in integration with Swagger, which is a powerful tool for API documentation. Swagger provides an ability to test API endpoints directly from the documentation interface and helps in quickly identifying and fixing issues during development. The home page is shown in the figure:

fastapi

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:

FastAPI 200-status

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"message": "Successfully uploaded file - Elena Genadjvna10.01.11.jpg",
"file": "File Elena Genadjvna10.01.11.jpg",
"severity": "Severity 4 with probability 53.15%",
"probability_distribution": {
"0": "0.057859645%",
"1": "0.018798877%",
"2": "0.25461438%",
"3": "0.13719666%",
"4": "0.531530445%"
}
}
{ "message": "Successfully uploaded file - Elena Genadjvna10.01.11.jpg", "file": "File Elena Genadjvna10.01.11.jpg", "severity": "Severity 4 with probability 53.15%", "probability_distribution": { "0": "0.057859645%", "1": "0.018798877%", "2": "0.25461438%", "3": "0.13719666%", "4": "0.531530445%" } }
{
  "message": "Successfully uploaded file - Elena Genadjvna10.01.11.jpg",
  "file": "File Elena Genadjvna10.01.11.jpg",
  "severity": "Severity 4 with probability 53.15%",
  "probability_distribution": {
    "0": "0.057859645%",
    "1": "0.018798877%",
    "2": "0.25461438%",
    "3": "0.13719666%",
    "4": "0.531530445%"
  }
}

Conclusion

In this article, we explored the process of delivering a machine learning (ML) model into a robust API service and deploying it in the cloud. We started by setting up the development environment and building the ML model. Using FastAPI, we created a high-performance, easy-to-use API to serve the model. We then containerized the application with Docker, ensuring consistency and portability across different environments. Next, we deployed the Docker image to Google Cloud Platform (GCP), leveraging its scalability, security, and global reach. We also tested the deployed service to ensure it operates efficiently.

By understanding and applying the concepts discussed, readers can enhance their technical skills and make their ML models accessible to a broader audience.

In conclusion, wrapping a machine learning model in an API and deploying it in the cloud represents a significant advancement in the accessibility and scalability of machine learning applications. By leveraging a machine learning cloud API, organizations can streamline their workflow, enabling seamless integration and real-time predictions. 

Furthermore, the implementation of a cloud machine learning API not only enhances performance but also optimizes resource utilization, allowing businesses to focus on innovation rather than infrastructure management. As the demand for intelligent solutions continues to rise, adopting a machine learning cloud API will be crucial for staying competitive in the rapidly evolving tech landscape. By understanding and applying the concepts discussed, you can enhance their technical skills and make their ML models accessible to a broader audience through a cloud machine learning API.

Contact Us
Contact Us

    • United States+1
    • United Kingdom+44
    • Afghanistan (‫افغانستان‬‎)+93
    • Albania (Shqipëri)+355
    • Algeria (‫الجزائر‬‎)+213
    • American Samoa+1
    • Andorra+376
    • Angola+244
    • Anguilla+1
    • Antigua and Barbuda+1
    • Argentina+54
    • Armenia (Հայաստան)+374
    • Aruba+297
    • Ascension Island+247
    • Australia+61
    • Austria (Österreich)+43
    • Azerbaijan (Azərbaycan)+994
    • Bahamas+1
    • Bahrain (‫البحرين‬‎)+973
    • Bangladesh (বাংলাদেশ)+880
    • Barbados+1
    • Belarus (Беларусь)+375
    • Belgium (België)+32
    • Belize+501
    • Benin (Bénin)+229
    • Bermuda+1
    • Bhutan (འབྲུག)+975
    • Bolivia+591
    • Bosnia and Herzegovina (Босна и Херцеговина)+387
    • Botswana+267
    • Brazil (Brasil)+55
    • British Indian Ocean Territory+246
    • British Virgin Islands+1
    • Brunei+673
    • Bulgaria (България)+359
    • Burkina Faso+226
    • Burundi (Uburundi)+257
    • Cambodia (កម្ពុជា)+855
    • Cameroon (Cameroun)+237
    • Canada+1
    • Cape Verde (Kabu Verdi)+238
    • Caribbean Netherlands+599
    • Cayman Islands+1
    • Central African Republic (République centrafricaine)+236
    • Chad (Tchad)+235
    • Chile+56
    • China (中国)+86
    • Christmas Island+61
    • Cocos (Keeling) Islands+61
    • Colombia+57
    • Comoros (‫جزر القمر‬‎)+269
    • Congo (DRC) (Jamhuri ya Kidemokrasia ya Kongo)+243
    • Congo (Republic) (Congo-Brazzaville)+242
    • Cook Islands+682
    • Costa Rica+506
    • Côte d’Ivoire+225
    • Croatia (Hrvatska)+385
    • Cuba+53
    • Curaçao+599
    • Cyprus (Κύπρος)+357
    • Czech Republic (Česká republika)+420
    • Denmark (Danmark)+45
    • Djibouti+253
    • Dominica+1
    • Dominican Republic (República Dominicana)+1
    • Ecuador+593
    • Egypt (‫مصر‬‎)+20
    • El Salvador+503
    • Equatorial Guinea (Guinea Ecuatorial)+240
    • Eritrea+291
    • Estonia (Eesti)+372
    • Eswatini+268
    • Ethiopia+251
    • Falkland Islands (Islas Malvinas)+500
    • Faroe Islands (Føroyar)+298
    • Fiji+679
    • Finland (Suomi)+358
    • France+33
    • French Guiana (Guyane française)+594
    • French Polynesia (Polynésie française)+689
    • Gabon+241
    • Gambia+220
    • Georgia (საქართველო)+995
    • Germany (Deutschland)+49
    • Ghana (Gaana)+233
    • Gibraltar+350
    • Greece (Ελλάδα)+30
    • Greenland (Kalaallit Nunaat)+299
    • Grenada+1
    • Guadeloupe+590
    • Guam+1
    • Guatemala+502
    • Guernsey+44
    • Guinea (Guinée)+224
    • Guinea-Bissau (Guiné Bissau)+245
    • Guyana+592
    • Haiti+509
    • Honduras+504
    • Hong Kong (香港)+852
    • Hungary (Magyarország)+36
    • Iceland (Ísland)+354
    • India (भारत)+91
    • Indonesia+62
    • Iran (‫ایران‬‎)+98
    • Iraq (‫العراق‬‎)+964
    • Ireland+353
    • Isle of Man+44
    • Israel (‫ישראל‬‎)+972
    • Italy (Italia)+39
    • Jamaica+1
    • Japan (日本)+81
    • Jersey+44
    • Jordan (‫الأردن‬‎)+962
    • Kazakhstan (Казахстан)+7
    • Kenya+254
    • Kiribati+686
    • Kosovo+383
    • Kuwait (‫الكويت‬‎)+965
    • Kyrgyzstan (Кыргызстан)+996
    • Laos (ລາວ)+856
    • Latvia (Latvija)+371
    • Lebanon (‫لبنان‬‎)+961
    • Lesotho+266
    • Liberia+231
    • Libya (‫ليبيا‬‎)+218
    • Liechtenstein+423
    • Lithuania (Lietuva)+370
    • Luxembourg+352
    • Macau (澳門)+853
    • Macedonia (FYROM) (Македонија)+389
    • Madagascar (Madagasikara)+261
    • Malawi+265
    • Malaysia+60
    • Maldives+960
    • Mali+223
    • Malta+356
    • Marshall Islands+692
    • Martinique+596
    • Mauritania (‫موريتانيا‬‎)+222
    • Mauritius (Moris)+230
    • Mayotte+262
    • Mexico (México)+52
    • Micronesia+691
    • Moldova (Republica Moldova)+373
    • Monaco+377
    • Mongolia (Монгол)+976
    • Montenegro (Crna Gora)+382
    • Montserrat+1
    • Morocco (‫المغرب‬‎)+212
    • Mozambique (Moçambique)+258
    • Myanmar (Burma) (မြန်မာ)+95
    • Namibia (Namibië)+264
    • Nauru+674
    • Nepal (नेपाल)+977
    • Netherlands (Nederland)+31
    • New Caledonia (Nouvelle-Calédonie)+687
    • New Zealand+64
    • Nicaragua+505
    • Niger (Nijar)+227
    • Nigeria+234
    • Niue+683
    • Norfolk Island+672
    • North Korea (조선 민주주의 인민 공화국)+850
    • Northern Mariana Islands+1
    • Norway (Norge)+47
    • Oman (‫عُمان‬‎)+968
    • Pakistan (‫پاکستان‬‎)+92
    • Palau+680
    • Palestine (‫فلسطين‬‎)+970
    • Panama (Panamá)+507
    • Papua New Guinea+675
    • Paraguay+595
    • Peru (Perú)+51
    • Philippines+63
    • Poland (Polska)+48
    • Portugal+351
    • Puerto Rico+1
    • Qatar (‫قطر‬‎)+974
    • Réunion (La Réunion)+262
    • Romania (România)+40
    • Russia (Россия)+7
    • Rwanda+250
    • Saint Barthélemy+590
    • Saint Helena+290
    • Saint Kitts and Nevis+1
    • Saint Lucia+1
    • Saint Martin (Saint-Martin (partie française))+590
    • Saint Pierre and Miquelon (Saint-Pierre-et-Miquelon)+508
    • Saint Vincent and the Grenadines+1
    • Samoa+685
    • San Marino+378
    • São Tomé and Príncipe (São Tomé e Príncipe)+239
    • Saudi Arabia (‫المملكة العربية السعودية‬‎)+966
    • Senegal (Sénégal)+221
    • Serbia (Србија)+381
    • Seychelles+248
    • Sierra Leone+232
    • Singapore+65
    • Sint Maarten+1
    • Slovakia (Slovensko)+421
    • Slovenia (Slovenija)+386
    • Solomon Islands+677
    • Somalia (Soomaaliya)+252
    • South Africa+27
    • South Korea (대한민국)+82
    • South Sudan (‫جنوب السودان‬‎)+211
    • Spain (España)+34
    • Sri Lanka (ශ්‍රී ලංකාව)+94
    • Sudan (‫السودان‬‎)+249
    • Suriname+597
    • Svalbard and Jan Mayen+47
    • Sweden (Sverige)+46
    • Switzerland (Schweiz)+41
    • Syria (‫سوريا‬‎)+963
    • Taiwan (台灣)+886
    • Tajikistan+992
    • Tanzania+255
    • Thailand (ไทย)+66
    • Timor-Leste+670
    • Togo+228
    • Tokelau+690
    • Tonga+676
    • Trinidad and Tobago+1
    • Tunisia (‫تونس‬‎)+216
    • Turkey (Türkiye)+90
    • Turkmenistan+993
    • Turks and Caicos Islands+1
    • Tuvalu+688
    • U.S. Virgin Islands+1
    • Uganda+256
    • Ukraine (Україна)+380
    • United Arab Emirates (‫الإمارات العربية المتحدة‬‎)+971
    • United Kingdom+44
    • United States+1
    • Uruguay+598
    • Uzbekistan (Oʻzbekiston)+998
    • Vanuatu+678
    • Vatican City (Città del Vaticano)+39
    • Venezuela+58
    • Vietnam (Việt Nam)+84
    • Wallis and Futuna (Wallis-et-Futuna)+681
    • Western Sahara (‫الصحراء الغربية‬‎)+212
    • Yemen (‫اليمن‬‎)+967
    • Zambia+260
    • Zimbabwe+263
    • Åland Islands+358

    Insert math as
    Block
    Inline
    Additional settings
    Formula color
    Text color
    #333333
    Type math using LaTeX
    Preview
    {}
    Nothing to preview
    Insert