Skip to main content

How to deploy your ML model as Fast API?

Another post starts with you beautiful people!
Thank you all for showing so much interests in my last posts about object detection and recognition using YOLOv4. I was very happy to see many aspiring data scientists have learnt from my past three posts about using YOLOv4. Today I am going to share you all a new skill to learn. Most of you have seen my post about deploying and consuming ML models as Flask API where we have learnt to deploy and consume a keras model with Flask API . In this post you are going to learn a new framework- FastAPI to deploy your model as Rest API. After completing this post you will have a new industry standard skill.

What is FastAPI?
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It is easy to learn, fast to code and ready for production. Yes, you heard it right! Flask is not meant to be used in production but with FastAPI you can use your model in production with minimal code. Along with this advantage FastAPI also have inbuilt OpenAPI (previously known as Swagger) support. With this OpenAPI support you don't need to use Postman or any other tool for testing your API. You will have a UI where you can easily test your endpoints.

How to install FastAPI?
We can install FastAPI using pip. For this installation open your Anaconda Prompt with admin rights, activate your virtual environment and run the following two commands one by one-

  1. pip install fastapi
  2. pip install uvicorn
Here uvicorn is a lightning-fast ASGI server and enables us to use FastAPI for all our asynchronous calls without any worry. Once you installed this framework, you can confirm the installation by following code snippet-

See the similarity with Flask API. Here in the same manner we have initialized the FastAPI as app variable. Save the above file and to run this file use the command: uvicorn main:app --reload where main is your Python file name and app is the variable we used for the initialization of FastAPI() function. Here --reload parameter is used to take any changes in Python file immediately. After making any changes you will not need to rerun the server. Once your server is up and running you will see following look like message in console-

Now open the url  http://127.0.0.1:8000/items/5?q=somequery in a browser window. You will see a json response. Now open the url  http://127.0.0.1:8000/docs and you will see screen like below-
See, this is the OpenAPI UI where you can easily test your API endpoints. Now you are aware of basic of FastAPI, we will move to the next step.

Serving my YOLO model as FastAPI?
In this post I am going to deploy my YOLO model as FastAPI. If you still have not tried YOLO then you can learn from previous post and trained your custom model. The API deployment process will be the same for any kind of ML model. Here I am using my custom YOLOv3 models which detects price tags in a given shelf image and then extract the price from those tags. Basically aim of these models are to extract the price of a brand and send in response to front end. So in this example input will be the complete path of an image and response will be extracted price.

Here is my code snippet of main Python file-
As you can see code is similar as we saw earlier. Here I have defined my get_price() function as asynchronous using 'async' keyword. Function is taking one input arguement- image which is the complete path of the image. I am reading the image using OpenCV. The HTTP method of this call is 'GET'. The complete logic for price tag detection and recognition is written in another Python file tag_detection.py which I have imported and called the required function. This tag_detection() function will return the extracted prices. You can replace this function with your own classification/regression or NLP model logic.

I have saved this file as price_extraction.py. So I ran following command to run the server: uvicorn price_extraction:app --reload and after server started I open the OpenAPI UI with command http://127.0.0.1:8000/docs-
Now to test the endpoint click on the GET or your HTTP method button. In my case it is looking like below-
Once you expand your HTTP method, you need to click on Try it out button to enable the input field. Based on your ML model input fields may be more than one. In my case it is one. After clicking on this button, enter the values of the input field and click on the Execute button. This button will trigger your endpoint and return the response.
In my case response screen looks like below-
And my input image was this-

Quite interesting right! With minimal code we are able to deploy and consume our model as Rest API. We also don't need to use any external tool for testing the endpoints. With OpenAPI inbuilt support no wonder companies like Microsoft, Netflix and Uber are using the FastAPI in production. Now you don't need to think too much; just take your existing ML model, wrap it in FastAPI code you learned today from this post and start experimenting with it because we learn by doing experiments in Data Science. 

Once you are done with this go ahead and deploy your FastAPI in Heroku by following this link to showcase your model with others. In my next post I will share you my new learning till then Go chase your dreams, have an awesome day, make every second count and see you later in my next post.

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice Article.Thank you for sharing useful information.
    For Online MBA check below.
    Innomatics Research Labs is collaborated with JAIN (Deemed-to-be University) and offering the Online MBA in Business Intelligence,Business Analytics Program. This two-year program from JAIN (deemed-to-be) University offers foundation courses, core courses, Specialization courses, and a comprehensive master thesis intermediary, apart from an option to pursue a cross-functional and open elective.
    Online MBA in Business Analytics
    Online MBA in Business Intelligence

    ReplyDelete
  3. I really enjoyed reading your blog. I think fast API is the best web framework. I will definitely try it. But do you know anything on Informatica Read Rest Api? Please share if you have. Thanks!

    ReplyDelete
  4. Our DevOps Training in Noida incorporates *online classes, classroom sessions, real-life case studies, develop and deploy software using DevOps* and lots more.

    ReplyDelete
  5. Get the best Python Training in Noida, ✓ state-of-the-art infrastructure, ✓ interactive classes, ✓ self-paced studies, and ✓ studies through real-life case studies.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. "Nice Blog! such a informative things you are sharing ,I really liked your content. If you wanna know about ""Skillslash | Training with live industry experience that gets you hired""
    go to Data Science Course"

    ReplyDelete
  8. i read your article it is very informative and attractive I hope you will share some more content. learn more from Machine Learning With Data Science Course

    ReplyDelete
  9. Thanks for sharing the valuable information and helpful content . Machine Learning course

    ReplyDelete
  10. It is extremely nice to see the greatest details presented in an easy and understanding manner.
    cyber security course

    ReplyDelete
  11. You completed certain reliable points there. I did a search on the subject and found nearly all persons will agree with your blog. 360DigiTMG data science course in jaipur

    ReplyDelete
  12. Thank you for sharing an amazing & wonderful blog. This content is very useful, informative and valuable in order to enhance knowledge. Keep sharing this type of content with us & keep updating us with new blogs. Apart from this, if anyone who wants to join the Python Base training institute in Delhi, can contact 9311002620 or visit our website-
    https://www.htsindia.com/Courses/python/python-training-institute-in-delhi

    ReplyDelete
  13. Excellent work! You have a nice blog here. You wrote something that everyone could understand and made the topic appealing. This article is very interesting and effective because it explains everything in detail. Amazon Lex was used to building a chatbot, integrated with a web page and a web page deployed on the AWS cloud. Thank you for this very important article!. For more information Visit Clinical Data Science

    ReplyDelete
  14. Take my money!!! It is great and helpful article! congratulations!

    ReplyDelete
  15. E4A251F9F1Kali57EED5404AOctober 13, 2024 at 5:27 PM

    6A5056CB1D
    skype show

    ReplyDelete

Post a Comment

Popular posts from this blog

How to install and compile YOLO v4 with GPU enable settings in Windows 10?

Another post starts with you beautiful people! Last year I had shared a post about  installing and compiling Darknet YOLOv3   in your Windows machine and also how to detect an object using  YOLOv3 with Keras . This year on April' 2020 the fourth generation of YOLO has arrived and since then I was curious to use this as soon as possible. Due to my project (built on YOLOv3 :)) work I could not find a chance to check this latest release. Today I got some relief and successfully able to install and compile YOLOv4 in my machine. In this post I am going to share a single shot way to do the same in your Windows 10 machine. If your machine does not have GPU then you can follow my  previous post  by just replacing YOLOv3 related files with YOLOv4 files. For GPU having Windows machine, follow my steps to avoid any issue while building the Darknet repository. My machine has following configurations: Windows 10 64 bit Intel Core i7 16 GB RAM NVIDIA GeForce GTX 1660 Ti Version 445.87

How to use opencv-python with Darknet's YOLOv4?

Another post starts with you beautiful people 😊 Thank you all for messaging me your doubts about Darknet's YOLOv4. I am very happy to see in a very short amount of time my lovely aspiring data scientists have learned a state of the art object detection and recognition technique. If you are new to my blog and to computer vision then please check my following blog posts one by one- Setup Darknet's YOLOv4 Train custom dataset with YOLOv4 Create production-ready API of YOLOv4 model Create a web app for your YOLOv4 model Since now we have learned to use YOLOv4 built on Darknet's framework. In this post, I am going to share with you how can you use your trained YOLOv4 model with another awesome computer vision and machine learning software library-  OpenCV  and of course with Python 🐍. Yes, the Python wrapper of OpenCV library has just released it's latest version with support of YOLOv4 which you can install in your system using below command- pip install opencv-python --up

How can I make a simple ChatBot?

Another post starts with you beautiful people! It has been a long time of posting a new post. But my friends in this period I was not sitting  where I got a chance to work with chatbot and classification related machine learning problem. So in this post I am going to share all about chatbot- from where I have learned? What I have learned? And how can you build your first bot? Quite interesting right! Chatbot is a program that can conduct an intelligent conversation based on user's input. Since chatbot is a new thing to me also, I first searched- is there any Python library available to start with this? And like always Python has helped me this time also. There is a Python library available with name as  ChatterBot   which is nothing but a machine learning conversational dialog engine. And yes that is all I want to start my learning because I always prefer inbuilt Python library to start my learning journey and once I learn this then only I move ahead for another sources.