Creating a Machine Learning API using FastAPI

Posted by

Building a Machine Learning API with FastAPI

Building a Machine Learning API with FastAPI

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints. It’s fast to code and fast to run. In this article, we’ll take a look at how to build a Machine Learning API with FastAPI.

Step 1: Setting up FastAPI

First, you need to install FastAPI by running the following command:


pip install fastapi

Next, create a new file for your FastAPI application. For example, create a file called app.py:


touch app.py

Step 2: Create a FastAPI Application

Inside your app.py file, import FastAPI and create an instance of the FastAPI class:


from fastapi import FastAPI

app = FastAPI()

Step 3: Create Machine Learning Endpoints

Now, you can create endpoints for your machine learning API. For example, let’s create a simple endpoint that takes a list of numbers and returns their sum:


@app.get("/sum/")
def sum_numbers(numbers: List[int]):
return {"result": sum(numbers)}

Step 4: Start the FastAPI Application

Finally, start the FastAPI application by running the following command:


uvicorn app:app --reload

Your FastAPI application should now be running on http://127.0.0.1:8000.

Conclusion

FastAPI is a powerful and easy-to-use tool for building machine learning APIs. With its support for type hints and automatic documentation generation, it makes the process of building and deploying APIs a breeze. Give FastAPI a try for your next machine learning project!

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@_stevejunior
7 months ago

Yes please host the API

@GLITZTECH
7 months ago

Nice video bro keep it on

@techupdates2
7 months ago

Great tutorial 💪