This project demonstrates how to set up a FastAPI endpoint for AI model inference, specifically using a pre-trained sentiment analysis model from the Hugging Face Transformers library.
- FastAPI Integration: Easily create API endpoints using FastAPI.
- Local Model Loading: Load and use pre-trained models stored locally on your machine.
- Sentiment Analysis: Analyze text sentiment using state-of-the-art transformer models.
- Extensible and Modular: Designed to be easily extendable for other types of models and tasks.
app/
: Contains the FastAPI application code.main.py
: Entry point for the FastAPI application.models/
: Directory for model-related code.sentiment_model.py
: Contains the sentiment analysis model class.
routers/
: Directory for route definitions.inference.py
: Contains the route for sentiment analysis inference.
schemas/
: Directory for request/response schemas.text_data.py
: Contains the schema for text data.
Dockerfile
: Docker configuration file.requirements.txt
: List of Python dependencies..dockerignore
: List of files and directories to ignore in the Docker image.README.md
: Project documentation.
- Docker
- Docker Compose (optional, for more complex setups)
-
Build the Docker image:
docker build -t ai-model-inference .
-
Run the Docker container:
docker run -d -p 8000:8000 ai-model-inference
-
Access the API documentation at
http://localhost:8000/docs
.
POST /predict/
: Predict the sentiment of a given text.
--visit `https://huggingface.co/distilbert/distilbert-base-uncased-finetuned-sst-2-english/tree/main
curl -X POST "http://localhost:8000/predict/" -H "Content-Type: application/json" -d '{"text": "I love FastAPI!"}'