FastAPI, Docker, and Hugging Face Transformers
This API provides text classification capabilities using a pre-trained model for sentiment analysis. It allows users to analyze the sentiment of text inputs and obtain the corresponding sentiment labels.
- The API has been built using the Hugging Face
transformers
library. - It uses the following pre-trained transformer model from Hugging Face:
cardiffnlp/twitter-roberta-base-sentiment-latest
- It classifies the text as
positive
,negative
, orneutral
.
This API is built using FastAPI and leverages a pre-trained sentiment analysis model from the Hugging Face model hub. It preprocesses the input text and passes it through the model to classify the sentiment as positive, negative, or neutral.
To install and run the API locally, follow these steps:
- Clone this repository to your local machine.
- Ensure you have Docker installed.
- Build the Docker container using the provided Dockerfile.
- Run the Docker container.
To use the API, send HTTP requests to the appropriate endpoints. The API provides the following endpoints:
GET /
: Welcome endpoint, returns a greeting message.POST /analyze/{text}
: Analyze endpoint, classifies the sentiment of the provided text.
The API is documented using FastAPI's automatic documentation features. You can access the API documentation using the Swagger UI or ReDoc interface. Simply navigate to the appropriate URL after starting the API server.
- Swagger UI
http://localhost:8000/docs
- ReDoc
http://localhost:8000/redoc
To build and run the Docker container, follow these steps:
- Navigate to the folder in which your FastAPI app resides.
- Build a Docker image using the following command
docker build -t text-classification-api .
- Containerize the application by creating a Docker container from the built image
docker run -p 8000:8000 text-classification-api
- The API will be available at
http://localhost:8000
- The API documentaion will be avaialable at
http://localhost:8000/docs
orhttp://localhost:8000/redoc
Test the API using the following command:
pytest
It will automatically run the predefined test cases.
Once the API is running, you can interact with it using HTTP requests through Swagger UI.
This API was built with inspiration from various open-source projects and libraries. Special thanks to the developers and contributors of FastAPI, Hugging Face Transformers, and NLTK.
This project is licensed under the Apache license version 2.0.