A FastAPI application that provides sentiment analysis using a fine-tuned BERT model.
- Sentiment analysis endpoint with confidence scores
- Input validation and error handling
- Health check endpoint
- Containerized with Docker
- Ready for cloud deployment
Analyze sentiment of input text.
Request Body:
{
"text": "I love this product!"
}Response:
{
"label": "positive",
"confidence": 0.9876
}Check API health and model status.
Response:
{
"status": "healthy",
"model_loaded": true
}- Install dependencies:
pip install -r requirements.txt- Run the API:
uvicorn main:app --reload- Visit http://localhost:8000/docs for interactive API documentation.
- Build the image:
docker build -t bert-sentiment-api .- Run the container:
docker run -p 8000:8000 bert-sentiment-api- Connect your GitHub repository to Render
- Create a new Web Service
- Set the following:
- Runtime: Docker
- Build Command: (leave empty, uses Dockerfile)
- Start Command: (leave empty, uses CMD from Dockerfile)
- Add environment variable if needed (none required)
- Deploy
- Build and push Docker image to ECR:
aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin your-account.dkr.ecr.your-region.amazonaws.com
docker tag bert-sentiment-api:latest your-account.dkr.ecr.your-region.amazonaws.com/bert-sentiment-api:latest
docker push your-account.dkr.ecr.your-region.amazonaws.com/bert-sentiment-api:latest- Create ECS cluster, task definition, and service
- Configure load balancer and security groups
- Build and push to GCR:
gcloud builds submit --tag gcr.io/your-project/bert-sentiment-api- Deploy to Cloud Run:
gcloud run deploy --image gcr.io/your-project/bert-sentiment-api --platform managed- Build and push to ACR:
az acr build --registry your-registry --image bert-sentiment-api .- Deploy to ACI:
az container create --resource-group your-rg --name bert-sentiment-api --image your-registry.azurecr.io/bert-sentiment-api --dns-name-label bert-sentiment-api --ports 8000- Python 3.11+
- Fine-tuned BERT model in
./model/directory - Sufficient RAM for model loading (4GB+ recommended)
The API expects a fine-tuned BERT model for sentiment analysis saved in the ./model/ directory with the standard transformers format (config.json, pytorch_model.bin or model.safetensors, tokenizer files).# FastAPI