A backend service built with FastAPI that classifies text sentiment into one of three categories: positive, negative, or neutral, using OpenAI LLMs.
This project is structured in a clean and scalable way, separating configuration, services, models, and routes.
POST /classify→ classify any text as positive, negative, or neutral.GET /health→ health check endpoint (confirms API is running and OpenAI key is loaded).- Automatic interactive documentation with Swagger UI (
/docs) and ReDoc (/redoc). - Modular architecture (services, models, routes) for scalability and testability.
text-classifier-api/ ├─ app/ │ ├─ main.py # FastAPI entry point │ ├─ api/classify.py # Endpoints (routes) │ ├─ core/config.py # Settings and environment variables │ ├─ models/schemas.py # Request/response models (Pydantic) │ ├─ services/openai_client.py # OpenAI client loader │ ├─ services/classifier.py # Classification logic │ └─ init.py ├─ requirements.txt ├─ .env # API key (not committed) ├─ .gitignore └─ README.md
git clone https://github.com/IreHurtado/text-classifier-api.git
cd text-classifier-api
python3 -m venv .venv
source .venv/bin/activate
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
Create a .env file in the root directory:
OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx
Start the FastAPI app with Uvicorn:
uvicorn app.main:app --reload
The server will be available at:
Swagger docs → http://127.0.0.1:8000/docs
ReDoc docs → http://127.0.0.1:8000/redoc
Go to /docs, open POST /classify, click Try it out, and send:
{
"text": "The service was excellent!"
}Response:
{
"text": "The service was excellent!",
"category": "positive"
}curl -X POST "http://127.0.0.1:8000/classify" \
-H "Content-Type: application/json" \
-d '{"text":"The product broke after 2 days"}'Response:
{
"text": "The product broke after 2 days",
"category": "negative"
}- FastAPI
- Uvicorn
- OpenAI Python SDK
- pydantic-settings: for environment management
[Irene Hurtado] (https://github.com/IreHurtado)
This project is licensed under the MIT License - you are free to use, copy, modify, and distribute this software with attribution.