A complete machine learning model deployment project using FastAPI and Docker. This project demonstrates how to train a sentiment analysis model, create a REST API, and containerize it for easy deployment.
Try the API now: https://ml-docker-deployment.onrender.com/docs Quick Test in Terminal bash# Health Check curl https://ml-docker-deployment.onrender.com/health
curl -X POST "https://ml-docker-deployment.onrender.com/predict"
-H "Content-Type: application/json"
-d '{"text": "This product is amazing!"}'
Quick Test in Python
pythonimport requests
response = requests.post( "https://ml-docker-deployment.onrender.com/predict", json={"text": "I love this product!"} ) print(response.json())
- Machine Learning Model: Sentiment analysis using Scikit-learn
- REST API: FastAPI with automatic documentation
- Containerization: Docker for easy deployment
- Batch Processing: Support for single and batch predictions
- Health Checks: Built-in health monitoring
- API Documentation: Auto-generated Swagger UI
- Cloud Deployed: Live on Render.com with 99.9% uptime
You can test all endpoints directly from the browser at http://localhost:8000/docs
ml-docker-deployment/
├── app/
│ ├── __init__.py
│ └── main.py # FastAPI application
├── models/
│ └── sentiment_model.pkl # Trained model
├── images/ # Screenshots
│ ├── api-documentation.png
│ ├── health.png
│ ├── prediction-commands.png
│ └── render.png
├── train_model.py # Model training script
├── requirements.txt # Python dependencies
├── Dockerfile # Docker configuration
└── README.md # This file
## Quick Start
### Prerequisites
- Python 3.11+
- Docker (optional, for containerization)
- Git
### 1. Clone the Repository
```bash
git clone https://github.com/YOUR_USERNAME/ml-docker-deployment.git
cd ml-docker-deployment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txtpython train_model.pyThis will create a sentiment_model.pkl file in the models/ directory.
uvicorn app.main:app --reloadThe API will be available at http://localhost:8000
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
docker build -t ml-sentiment-api .docker run -d -p 8000:8000 --name ml-api ml-sentiment-apidocker ps
docker logs ml-apidocker stop ml-api
docker rm ml-apicurl -X POST "http://localhost:8000/predict" \
-H "Content-Type: application/json" \
-d '{"text": "This product is amazing!"}'Response:
{
"text": "This product is amazing!",
"sentiment": "Positive",
"confidence": 0.8542,
"timestamp": "2025-11-08T10:30:00"
}curl -X POST "http://localhost:8000/predict/batch" \
-H "Content-Type: application/json" \
-d '{
"texts": [
"I love this!",
"This is terrible",
"Pretty good overall"
]
}'curl http://localhost:8000/healthimport requests
# Single prediction
response = requests.post(
"http://localhost:8000/predict",
json={"text": "This is fantastic!"}
)
print(response.json())
# Batch prediction
response = requests.post(
"http://localhost:8000/predict/batch",
json={"texts": ["Great!", "Terrible!", "Not bad"]}
)
print(response.json())// Single prediction
fetch('http://localhost:8000/predict', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({text: 'This is amazing!'})
})
.then(res => res.json())
.then(data => console.log(data));| Method | Endpoint | Description |
|---|---|---|
| GET | / |
API information |
| GET | /health |
Health check |
| GET | /docs |
Swagger UI documentation |
| POST | /predict |
Single text prediction |
| POST | /predict/batch |
Batch text prediction |
- Python 3.11: Programming language
- Scikit-learn: Machine learning library
- FastAPI: Modern web framework
- Uvicorn: ASGI server
- Docker: Containerization platform
- Pydantic: Data validation
- Type: Sentiment Analysis (Binary Classification)
- Algorithm: Naive Bayes with TF-IDF vectorization
- Classes: Positive, Negative
- Features: TF-IDF with unigrams and bigrams
You can configure the application using environment variables:
export MODEL_PATH=models/sentiment_model.pkl
export PORT=8000Create a docker-compose.yml:
version: '3.8'
services:
ml-api:
build: .
ports:
- "8000:8000"
volumes:
- ./models:/app/models
environment:
- MODEL_PATH=models/sentiment_model.pklRun with: docker-compose up -d
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License.
Sakshi
- GitHub: Sakshi3027
- FastAPI documentation
- Scikit-learn community
- Docker documentation
Through this project, I gained hands-on experience with:
- Training and deploying ML models in production
- Building RESTful APIs with FastAPI
- Containerization with Docker
- API documentation and testing
- Git version control and GitHub workflows




