This repository contains a FastAPI-based sentiment analysis API that uses a BERT model for sentiment classification. The API is containerized with Docker and supports NVIDIA GPU acceleration.
- Docker installed on your system
- For GPU support: NVIDIA GPU with appropriate drivers
- NVIDIA Container Toolkit (nvidia-docker2) installed
To build the Docker image, run the following command from the project root:
docker build -t sentiment-analysis-api .
To run the container with GPU support:
docker run --rm --runtime=nvidia --gpus all -p 9005:9005 sentiment-analysis-api
If you don't have a GPU or don't want to use it:
docker run -p 9005:9005 sentiment-analysis-api
Once the container is running, the API will be available at http://localhost:9005
GET /health
- Health check endpointPOST /predict
- Predict sentiment for a single textPOST /predict/batch
- Predict sentiment for multiple texts (up to 100)
curl -X POST "http://localhost:9005/predict" \
-H "Content-Type: application/json" \
-d '{"text": "This movie was fantastic and I really enjoyed it!"}'
curl -X POST "http://localhost:9005/predict/batch" \
-H "Content-Type: application/json" \
-d '{"texts": ["I loved this product", "The service was terrible"]}'
- The application will automatically detect if a GPU is available and use it
- You can verify GPU usage by checking the logs when the container starts
- The Dockerfile is configured with CUDA 12.1, which should be compatible with recent NVIDIA GPUs