Skip to content

Quick Start

Get up and running with the Text Classification API in minutes.

Prerequisites

  • Python 3.11+
  • Model files (final_best_model.pkl, tfidf_vectorizer.pkl)
  • 2GB+ RAM

Installation

# Clone or download the project
cd text-classifier-api

# Install dependencies
pip install -r api/api_requirements.txt

# Download NLTK data
python -c "import nltk; nltk.download('stopwords'); nltk.download('punkt'); nltk.download('wordnet')"

Running the API

Local Development

# Start the API server
cd api
python main.py

# The API will be available at http://localhost:8000

Using Docker

# Build and run with Docker
docker build -t text-classifier-api ./api
docker run -p 8000:8000 text-classifier-api

Using Docker Compose

# Run with docker-compose
docker-compose -f api/docker-compose.yml up --build

Testing the API

Health Check

curl http://localhost:8000/health

Response:

{
  "status": "healthy",
  "models_loaded": true,
  "vectorizer_loaded": true,
  "memory_usage_mb": 45.2,
  "uptime_seconds": 5.5
}

Single Prediction

curl -X POST "http://localhost:8000/predict" \
  -H "Content-Type: application/json" \
  -d '{"text": "This product is amazing!"}'

Response:

{
  "sentiment": "positive",
  "confidence": 0.8923,
  "model_used": "Ensemble Best Model",
  "processing_time": 0.0234,
  "request_id": "req-1"
}

Batch Prediction

curl -X POST "http://localhost:8000/batch_predict" \
  -H "Content-Type: application/json" \
  -d '{
    "texts": [
      "This is great!",
      "Not good at all",
      "It is okay"
    ]
  }'

Python Client Example

import requests

class TextClassifierClient:
    def __init__(self, base_url="http://localhost:8000"):
        self.base_url = base_url

    def predict(self, text):
        response = requests.post(
            f"{self.base_url}/predict",
            json={"text": text}
        )
        return response.json()

    def batch_predict(self, texts):
        response = requests.post(
            f"{self.base_url}/batch_predict",
            json={"texts": texts}
        )
        return response.json()

    def health_check(self):
        response = requests.get(f"{self.base_url}/health")
        return response.json()

# Usage
client = TextClassifierClient()
result = client.predict("This movie was fantastic!")
print(f"Sentiment: {result['sentiment']}")
print(f"Confidence: {result['confidence']}")

Next Steps