Containerized sentiment analysis API using HuggingFace’s DistilBERT model, powered by FastAPI and Uvicorn. Includes parallel request demonstration via Jupyter Notebook.
-
Project Overview This project deploys a sentiment analysis model using Hugging Face's transformers library, served via a FastAPI application. The system is containerized with Docker and supports concurrent inference requests. A Jupyter Notebook demonstrates parallel POST requests to the endpoint.
-
Model Used distilbert-base-uncased-finetuned-sst-2-english
https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english#how-to-get-started-with-the-model -
Tech Stack
• Python 3.10+
• FastAPI
• HuggingFace Transformers
• Docker
• Uvicorn (ASGI server)
• Jupyter Notebook
• aiohttp and asyncio (for parallel requests demo) -
Project Structure
main.py # FastAPI app with /predict endpoint
Dockerfile # Instructions for containerizing the app
requirements.txt # Python dependencies
.dockerignore # Files to exclude from Docker builds
parallel_requests_demo.ipynb # Notebook to test multiple requests
README.md # Project documentation -
How to Run the Server
Using Docker:
docker build -t sentiment-api .
docker run -p 8000:8000 sentiment-api
Access the API at http://localhost:8000/docs. -
API Usage
Endpoint: POST /predict
Payload example:
{
"text": "This product is amazing!"
}
Response example:
{
"label": "POSITIVE",
"score": 0.9993
} -
Parallel Request Demo The parallel_requests_demo.ipynb file shows how to simulate multiple users hitting the endpoint in parallel using aiohttp and asyncio.
-
Future Enhancements
• Add support for batch inference.
• Deploy on cloud (AWS/GCP) with autoscaling.
• Integrate logging and monitoring tools.