Skip to content

FastAPI service for text sentiment classification (positive, negative, neutral) using OpenAI LLMs.

Notifications You must be signed in to change notification settings

IreHurtado/text-classifier-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

🤖 Text Classifier API

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.


✨ Features

  • 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.

📂 Project Structure

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


🛠️ Installation

1. Clone the repo

git clone https://github.com/IreHurtado/text-classifier-api.git
cd text-classifier-api

2. Create and activate a virtual environment

Ubuntu/macOS

python3 -m venv .venv
source .venv/bin/activate

Windows (PowerShell)

python -m venv .venv
.venv\Scripts\Activate.ps1

3. Install dependencies

pip install -r requirements.txt

4. Configure environment variables

Create a .env file in the root directory:

OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxx

▶️ Run the server

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

📌 Usage Examples

Classify a text (Swagger UI)

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 Request

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"
}

🧰 Tech Stack

  • FastAPI
  • Uvicorn
  • OpenAI Python SDK
  • pydantic-settings: for environment management

👩‍💻 Author

[Irene Hurtado] (https://github.com/IreHurtado)

📄 License

This project is licensed under the MIT License - you are free to use, copy, modify, and distribute this software with attribution.

About

FastAPI service for text sentiment classification (positive, negative, neutral) using OpenAI LLMs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages