Skip to content

Deploy a Hugging Face sentiment analysis model via FastAPI — simple, production-aligned ML API setup.

License

Notifications You must be signed in to change notification settings

MeelahMe/fastapi-ml-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deploying a Pretrained Sentiment Analysis Model with FastAPI

This tutorial demonstrates how to deploy a pretrained Hugging Face sentiment analysis model using FastAPI. It is intended for developers, machine learning (ML) engineers, and data scientists seeking to expose ML models through a RESTful API using Python-based tools.

This guide follows best practices in documentation as outlined in the Google Developer Style Guide, prioritizing clarity, precision, and reader-first communication.


Objectives

By the end of this guide, you will learn how to:

  • Load a pretrained model using Hugging Face Transformers.
  • Serve model predictions through an HTTP endpoint using FastAPI.
  • Test the endpoint using curl, Postman, or Swagger UI.
  • Understand the structure of a production-friendly ML API deployment.

Project structure

fastapi-ml-api/ 
├── app.py # Core application file with model and endpoint 
├── requirements.txt # Python package dependencies 
├── README.md # Project documentation (this file) 
├── LICENSE # MIT License for project use 
└── .github/ 
    └── ISSUE_TEMPLATE/ 
    └── bug_report.md # Template for structured bug reporting

Prerequisites

  • Python 3.8 or later
  • Familiarity with Python, REST APIs, and the command line
  • A virtual environment (recommended)

Setup instructions

git clone https://github.com/MeelahMe/fastapi-ml-api.git
cd fastapi-ml-api

Create and activate a virtual environment (recommended)

python -m venv venv

Activate the environment:

  • On macOS/Linux
source venv/bin/activate 
  • On Windows:
venv\\Scripts\\activate

You should now see your terminal prompt prefixed with (venv).

Install required packages

pip install -r requirements.txt

This will install:

  • FastAPI for creating the web application.
  • Uvicorn as the ASGI server.
  • Transformers for accessing pretrained models.
  • Torch as the backend deep learning framework.

Verify files are present

  • app.py
  • requirements.txt
  • README.md
  • .github/ISSUE_TEMPLATE/bug_report.md
  • LINCENSE

Run the application server

uvicorn app:app --reload

The --reload flag enables automatic server restart on code changes.

Expected output:

INFO:     Uvicorn running on http://127.0.0.1:8000

Open the API docs

Usage instructions

Once your application server is running, you can interact with the API using the Swagger UI, command-line tools like curl, or API clients such as Postman.

Usage Swagger UI (recommended for exploration)

  1. Open your browser and navigate to:
http://127.0.0.1:8000/docs
  1. Locate the POST /predict endpoint in the list.
  2. Click Try it out.
  3. In the request body input, enter sample text:
{
  "text": "This is a fantastic project!"
}
  1. Click Execute.
  2. The response section below will display the prediction result.

This is ideal for quickly verifying functionality and exploring the API.

Using curl (Command Line)

To send a request directly from the terminal:

curl -X POST http://127.0.0.1:8000/predict \
  -H "Content-Type: application/json" \
  -d '{"text": "Deploying models is easier than ever."}'

Expected output:

{
  "result": [
    {
      "label": "POSITIVE",
      "score": 0.9997
    }
  ]
}

This method is useful for scripting and quick tests.

Using Postman (GUI-based API Testing Tool)

  1. Open Postman and create a new POST request.
  2. Set the request URL to:
http://127.0.0.1:8000/predict
  1. Go to the Headers tab and add:

    • Key : Content-Type
    • Value : application/json
  2. Navigate to the Body tab.

    • Select raw and choose JSON as the format.
    • Paste the following:
{
  "text": "FastAPI is great for ML deployment."
}
  1. Click Send to see the response.

Postman is especially helpful for testing with varying inputs or headers.

Alternate documentation UI (ReDoc)

If you prefer a different layout, FastAPI also auto-generates a ReDoc UI:

http://127.0.0.1:8000/redoc

This is a read-only reference for exploring endpoints and data structures.

License

This project is licensed under the MIT License.

About

Deploy a Hugging Face sentiment analysis model via FastAPI — simple, production-aligned ML API setup.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages