tsangcharles/docker-deployment-demo

Packaging a Hugging Face model with Docker

Python

1

1 commits

updated Oct 1, 2025

See the code

README

Multilingual Sentiment Analysis API

A FastAPI application that provides multilingual sentiment analysis across 23 languages using a fine-tuned DistilBERT model. This project is designed for educational purposes to demonstrate Docker containerization and ML model deployment.

🚀 Features

  • RESTful API built with FastAPI
  • Multilingual Sentiment Analysis across 23 languages
  • Docker containerization for easy deployment
  • Health checks and monitoring endpoints
  • Interactive API documentation with Swagger UI

📊 Model Information

This application uses the Multilingual Sentiment Analysis Model from Hugging Face.

Model Details

  • Base Model: distilbert/distilbert-base-multilingual-cased
  • Languages: Supports 23 languages including English, Chinese, Spanish, Hindi, Arabic, Bengali, Portuguese, Russian, Japanese, German, Malay, Telugu, Vietnamese, Korean, French, Turkish, Italian, Polish, Ukrainian, Tagalog, Dutch, Swiss German, and Swahili
  • Labels: Very Negative, Negative, Neutral, Positive, Very Positive (5 classes)
  • Training: 3.5 epochs, synthetic multilingual data
  • Performance: ~93% accuracy on validation dataset

Citation

@misc{tabularisai_2025,
    author       = { tabularisai and Samuel Gyamfi and Vadim Borisov and Richard H. Schreiber },
    title        = { multilingual-sentiment-analysis (Revision 69afb83) },
    year         = 2025,
    url          = { https://huggingface.co/tabularisai/multilingual-sentiment-analysis },
    doi          = { 10.57967/hf/5968 },
    publisher    = { Hugging Face }
}

License: CC-BY-NC-4.0 - For commercial use, contact [email protected]. See the model page for full license details.

🛠️ Quick Start

Prerequisites

  • Docker
  • Docker Compose

Running with Docker Compose

  1. Clone or download this repository

    git clone <repository-url>
    cd deploying_ml_model
    
  2. Start the application

    docker compose up
    
  3. Access the API

    • API Base URL: http://localhost:8000
    • Interactive Documentation: http://localhost:8000/docs
    • Health Check: http://localhost:8000/health

📡 API Endpoints

1. Root Endpoint

GET /

Returns API information and available endpoints.

2. Health Check

GET /health

Returns the health status of the API and model loading status.

3. Single Text Sentiment Analysis

POST /predict

Request Body:

{
  "text": "Tesla stock drops after disappointing delivery numbers."
}

Response:

{
  "text": "Tesla stock drops after disappointing delivery numbers.",
  "sentiment": "Negative",
  "confidence": 0.95
}

🧪 Testing the API

Using curl

Single prediction:

curl -X POST "http://localhost:8000/predict" \
     -H "Content-Type: application/json" \
     -d '{"text": "Tesla stock drops after disappointing delivery numbers."}'

Using Python requests

import requests

# Single prediction
response = requests.post(
    "http://localhost:8000/predict",
    json={"text": "Tesla stock drops after disappointing delivery numbers."}
)
print(response.json())

🐳 Docker Commands

Building the image manually

docker build -t sentiment-api .

Running the container manually

docker run -p 8000:8000 sentiment-api

Using Docker Compose

# Start the service
docker compose up

# Start in detached mode
docker compose up -d

# Stop the service
docker compose down

# View logs
docker compose logs

# Rebuild and start
docker compose up --build

📁 Project Structure

.
├── src/
│   └── app.py            # FastAPI application
├── requirements.txt       # Python dependencies
├── Dockerfile            # Docker configuration
├── docker-compose.yml    # Docker Compose configuration
└── README.md            # This file

🎓 Educational Use

This project is designed for teaching data science students about:

  1. Docker containerization of ML applications
  2. RESTful API design with FastAPI
  3. Model deployment best practices
  4. Hugging Face integration for pre-trained models
  5. Production-ready ML application structure

Learning Objectives

Students will learn:

  • How to containerize ML applications with Docker
  • How to create RESTful APIs for ML models
  • How to handle model loading and inference in production
  • How to implement proper error handling and validation
  • How to use Docker Compose for orchestration

🔧 Development

Local Development Setup

  1. Install dependencies:

    pip install -r requirements.txt
    
  2. Run the application:

    uvicorn src.app:app --host 0.0.0.0 --port 8000 --reload
    

Customization

  • Modify src/app.py to add new endpoints
  • Update requirements.txt for additional dependencies
  • Adjust Dockerfile for specific system requirements
  • Modify docker-compose.yml for different deployment scenarios

📝 Notes

  • The model will be downloaded automatically on first startup (may take a few minutes)
  • Supports sentiment analysis in 23 languages including English, Chinese, Spanish, Hindi, Arabic, and more
  • Returns 5 sentiment classes: Very Negative, Negative, Neutral, Positive, Very Positive
  • Health checks ensure the model is properly loaded before serving requests
  • The application runs on port 8000 by default

🤝 Contributing

This is an educational project. Feel free to fork and modify for your own teaching needs.

📄 License

This project uses the CC-BY-NC-4.0 licensed multilingual sentiment model from tabularisai/multilingual-sentiment-analysis on Hugging Face. For commercial use, please contact [email protected].

tsangcharles/docker-deployment-demo

Packaging a Hugging Face model with Docker

Python

1

1 commits

updated Oct 1, 2025

See the code

README

Multilingual Sentiment Analysis API

A FastAPI application that provides multilingual sentiment analysis across 23 languages using a fine-tuned DistilBERT model. This project is designed for educational purposes to demonstrate Docker containerization and ML model deployment.

🚀 Features

  • RESTful API built with FastAPI
  • Multilingual Sentiment Analysis across 23 languages
  • Docker containerization for easy deployment
  • Health checks and monitoring endpoints
  • Interactive API documentation with Swagger UI

📊 Model Information

This application uses the Multilingual Sentiment Analysis Model from Hugging Face.

Model Details

  • Base Model: distilbert/distilbert-base-multilingual-cased
  • Languages: Supports 23 languages including English, Chinese, Spanish, Hindi, Arabic, Bengali, Portuguese, Russian, Japanese, German, Malay, Telugu, Vietnamese, Korean, French, Turkish, Italian, Polish, Ukrainian, Tagalog, Dutch, Swiss German, and Swahili
  • Labels: Very Negative, Negative, Neutral, Positive, Very Positive (5 classes)
  • Training: 3.5 epochs, synthetic multilingual data
  • Performance: ~93% accuracy on validation dataset

Citation

@misc{tabularisai_2025,
    author       = { tabularisai and Samuel Gyamfi and Vadim Borisov and Richard H. Schreiber },
    title        = { multilingual-sentiment-analysis (Revision 69afb83) },
    year         = 2025,
    url          = { https://huggingface.co/tabularisai/multilingual-sentiment-analysis },
    doi          = { 10.57967/hf/5968 },
    publisher    = { Hugging Face }
}

License: CC-BY-NC-4.0 - For commercial use, contact [email protected]. See the model page for full license details.

🛠️ Quick Start

Prerequisites

  • Docker
  • Docker Compose

Running with Docker Compose

  1. Clone or download this repository

    git clone <repository-url>
    cd deploying_ml_model
    
  2. Start the application

    docker compose up
    
  3. Access the API

    • API Base URL: http://localhost:8000
    • Interactive Documentation: http://localhost:8000/docs
    • Health Check: http://localhost:8000/health

📡 API Endpoints

1. Root Endpoint

GET /

Returns API information and available endpoints.

2. Health Check

GET /health

Returns the health status of the API and model loading status.

3. Single Text Sentiment Analysis

POST /predict

Request Body:

{
  "text": "Tesla stock drops after disappointing delivery numbers."
}

Response:

{
  "text": "Tesla stock drops after disappointing delivery numbers.",
  "sentiment": "Negative",
  "confidence": 0.95
}

🧪 Testing the API

Using curl

Single prediction:

curl -X POST "http://localhost:8000/predict" \
     -H "Content-Type: application/json" \
     -d '{"text": "Tesla stock drops after disappointing delivery numbers."}'

Using Python requests

import requests

# Single prediction
response = requests.post(
    "http://localhost:8000/predict",
    json={"text": "Tesla stock drops after disappointing delivery numbers."}
)
print(response.json())

🐳 Docker Commands

Building the image manually

docker build -t sentiment-api .

Running the container manually

docker run -p 8000:8000 sentiment-api

Using Docker Compose

# Start the service
docker compose up

# Start in detached mode
docker compose up -d

# Stop the service
docker compose down

# View logs
docker compose logs

# Rebuild and start
docker compose up --build

📁 Project Structure

.
├── src/
│   └── app.py            # FastAPI application
├── requirements.txt       # Python dependencies
├── Dockerfile            # Docker configuration
├── docker-compose.yml    # Docker Compose configuration
└── README.md            # This file

🎓 Educational Use

This project is designed for teaching data science students about:

  1. Docker containerization of ML applications
  2. RESTful API design with FastAPI
  3. Model deployment best practices
  4. Hugging Face integration for pre-trained models
  5. Production-ready ML application structure

Learning Objectives

Students will learn:

  • How to containerize ML applications with Docker
  • How to create RESTful APIs for ML models
  • How to handle model loading and inference in production
  • How to implement proper error handling and validation
  • How to use Docker Compose for orchestration

🔧 Development

Local Development Setup

  1. Install dependencies:

    pip install -r requirements.txt
    
  2. Run the application:

    uvicorn src.app:app --host 0.0.0.0 --port 8000 --reload
    

Customization

  • Modify src/app.py to add new endpoints
  • Update requirements.txt for additional dependencies
  • Adjust Dockerfile for specific system requirements
  • Modify docker-compose.yml for different deployment scenarios

📝 Notes

  • The model will be downloaded automatically on first startup (may take a few minutes)
  • Supports sentiment analysis in 23 languages including English, Chinese, Spanish, Hindi, Arabic, and more
  • Returns 5 sentiment classes: Very Negative, Negative, Neutral, Positive, Very Positive
  • Health checks ensure the model is properly loaded before serving requests
  • The application runs on port 8000 by default

🤝 Contributing

This is an educational project. Feel free to fork and modify for your own teaching needs.

📄 License

This project uses the CC-BY-NC-4.0 licensed multilingual sentiment model from tabularisai/multilingual-sentiment-analysis on Hugging Face. For commercial use, please contact [email protected].