help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
text-classification
/
scripts
RSK World
text-classification
Text Classification Dataset - NLP + Multi-Class Classification + Machine Learning
scripts
  • __init__.py2.3 KB
  • active_learning.py26.8 KB
  • api_server.py12.7 KB
  • batch_processor.py16.4 KB
  • data_augmentation.py18.2 KB
  • data_quality.py20 KB
  • deep_learning.py24.2 KB
  • hyperparameter_tuning.py22.5 KB
  • model_explainability.py17.9 KB
  • preprocessing.py8.7 KB
  • train_classifier.py13.8 KB
  • train_transformers.py12.5 KB
  • visualizations.py19 KB
api_server.py
scripts/api_server.py
Raw Download
Find: Go to:
"""
================================================================================
Text Classification Dataset - REST API Server
================================================================================
Project: Text Classification Dataset
Category: Text Data / NLP

Author: Molla Samser
Designer & Tester: Rima Khatun
Website: https://rskworld.in
Email: help@rskworld.in | support@rskworld.in
Phone: +91 93305 39277

Copyright (c) 2026 RSK World - All Rights Reserved
Content used for educational purposes only.

Features:
- Single text classification
- Batch classification
- Model info endpoint
- Category listing
- Health check

Usage:
    python api_server.py --model model.joblib --port 5000

API Endpoints:
    GET  /                  - API info
    GET  /health            - Health check
    GET  /categories        - List categories
    POST /predict           - Classify single text
    POST /predict/batch     - Classify multiple texts

Created: December 2026
================================================================================
"""

import os
import json
import argparse
from datetime import datetime
from typing import Dict, List, Optional

try:
    from flask import Flask, request, jsonify
    from flask_cors import CORS
except ImportError:
    print("Please install Flask: pip install flask flask-cors")
    exit(1)

import joblib
import numpy as np

# Project information
__author__ = "Molla Samser"
__website__ = "https://rskworld.in"
__email__ = "help@rskworld.in"
__version__ = "1.0.0"

# Initialize Flask app
app = Flask(__name__)
CORS(app)

# Global model storage
model_data = None
classifier = None
vectorizer = None

# Category mapping
CATEGORIES = {
    0: 'Technology',
    1: 'Sports',
    2: 'Politics',
    3: 'Entertainment',
    4: 'Business',
    5: 'Science'
}

CATEGORY_DESCRIPTIONS = {
    'Technology': 'Tech news, gadgets, software, AI, innovation',
    'Sports': 'Athletics, competitions, leagues, tournaments',
    'Politics': 'Government, policy, elections, international relations',
    'Entertainment': 'Movies, music, TV shows, celebrities, pop culture',
    'Business': 'Finance, markets, economy, corporate news',
    'Science': 'Research, discoveries, space, health, environment'
}


def load_model(model_path: str):
    """Load the trained model and vectorizer."""
    global model_data, classifier, vectorizer
    
    if not os.path.exists(model_path):
        raise FileNotFoundError(f"Model not found: {model_path}")
    
    model_data = joblib.load(model_path)
    
    # Extract pipeline components
    pipeline = model_data.get('pipeline')
    if pipeline:
        vectorizer = pipeline.named_steps.get('vectorizer')
        classifier = pipeline.named_steps.get('classifier')
    
    print(f"Model loaded successfully from: {model_path}")
    print(f"Algorithm: {model_data.get('algorithm', 'unknown')}")


def preprocess_text(text: str) -> str:
    """Basic text preprocessing."""
    import re
    import string
    
    # Lowercase
    text = text.lower()
    # Remove URLs
    text = re.sub(r'https?://\S+|www\.\S+', '', text)
    # Remove punctuation
    text = text.translate(str.maketrans('', '', string.punctuation))
    # Normalize whitespace
    text = ' '.join(text.split())
    
    return text


def predict_text(text: str) -> Dict:
    """
    Classify a single text.
    
    Args:
        text: Input text to classify
        
    Returns:
        Dictionary with prediction results
    """
    if model_data is None:
        return {'error': 'Model not loaded'}
    
    # Preprocess
    processed_text = preprocess_text(text)
    
    # Get prediction
    pipeline = model_data['pipeline']
    prediction = pipeline.predict([processed_text])[0]
    
    # Get probabilities if available
    probabilities = None
    if hasattr(pipeline, 'predict_proba'):
        try:
            probs = pipeline.predict_proba([processed_text])[0]
            probabilities = {CATEGORIES[i]: float(p) for i, p in enumerate(probs)}
        except:
            pass
    
    result = {
        'text': text[:200] + '...' if len(text) > 200 else text,
        'predicted_label': int(prediction),
        'predicted_category': CATEGORIES[prediction],
        'category_description': CATEGORY_DESCRIPTIONS[CATEGORIES[prediction]],
        'confidence': max(probabilities.values()) if probabilities else None,
        'probabilities': probabilities
    }
    
    return result


# ===== API Routes =====

@app.route('/')
def home():
    """API information endpoint."""
    return jsonify({
        'name': 'Text Classification API',
        'version': __version__,
        'author': __author__,
        'website': __website__,
        'email': __email__,
        'description': 'Multi-class text classification API for news categorization',
        'endpoints': {
            'GET /': 'API information',
            'GET /health': 'Health check',
            'GET /categories': 'List all categories',
            'GET /model/info': 'Model information',
            'POST /predict': 'Classify single text',
            'POST /predict/batch': 'Classify multiple texts'
        },
        'copyright': 'Copyright (c) 2026 RSK World - All Rights Reserved'
    })


@app.route('/health')
def health():
    """Health check endpoint."""
    return jsonify({
        'status': 'healthy',
        'model_loaded': model_data is not None,
        'timestamp': datetime.now().isoformat(),
        'version': __version__
    })


@app.route('/categories')
def categories():
    """List all categories."""
    return jsonify({
        'categories': [
            {
                'id': label,
                'name': name,
                'description': CATEGORY_DESCRIPTIONS[name]
            }
            for label, name in CATEGORIES.items()
        ],
        'total': len(CATEGORIES)
    })


@app.route('/model/info')
def model_info():
    """Get model information."""
    if model_data is None:
        return jsonify({'error': 'Model not loaded'}), 500
    
    return jsonify({
        'algorithm': model_data.get('algorithm', 'unknown'),
        'max_features': model_data.get('max_features', 'unknown'),
        'ngram_range': model_data.get('ngram_range', 'unknown'),
        'training_history': model_data.get('training_history', {}),
        'metadata': model_data.get('metadata', {})
    })


@app.route('/predict', methods=['POST'])
def predict():
    """
    Classify a single text.
    
    Request body:
        {"text": "Your text here"}
        
    Returns:
        Classification result with category and confidence
    """
    if model_data is None:
        return jsonify({'error': 'Model not loaded'}), 500
    
    data = request.get_json()
    
    if not data or 'text' not in data:
        return jsonify({
            'error': 'Missing required field: text',
            'usage': {'text': 'Your text to classify'}
        }), 400
    
    text = data['text']
    
    if not text or len(text.strip()) == 0:
        return jsonify({'error': 'Text cannot be empty'}), 400
    
    result = predict_text(text)
    result['request_timestamp'] = datetime.now().isoformat()
    
    return jsonify(result)


@app.route('/predict/batch', methods=['POST'])
def predict_batch():
    """
    Classify multiple texts.
    
    Request body:
        {"texts": ["Text 1", "Text 2", ...]}
        
    Returns:
        List of classification results
    """
    if model_data is None:
        return jsonify({'error': 'Model not loaded'}), 500
    
    data = request.get_json()
    
    if not data or 'texts' not in data:
        return jsonify({
            'error': 'Missing required field: texts',
            'usage': {'texts': ['Text 1', 'Text 2']}
        }), 400
    
    texts = data['texts']
    
    if not isinstance(texts, list):
        return jsonify({'error': 'texts must be a list'}), 400
    
    if len(texts) == 0:
        return jsonify({'error': 'texts list cannot be empty'}), 400
    
    if len(texts) > 100:
        return jsonify({'error': 'Maximum 100 texts per batch'}), 400
    
    results = [predict_text(text) for text in texts]
    
    return jsonify({
        'results': results,
        'total': len(results),
        'request_timestamp': datetime.now().isoformat()
    })


@app.route('/analyze', methods=['POST'])
def analyze():
    """
    Detailed text analysis with statistics.
    
    Request body:
        {"text": "Your text here"}
        
    Returns:
        Classification + text statistics
    """
    if model_data is None:
        return jsonify({'error': 'Model not loaded'}), 500
    
    data = request.get_json()
    
    if not data or 'text' not in data:
        return jsonify({'error': 'Missing required field: text'}), 400
    
    text = data['text']
    
    # Get prediction
    result = predict_text(text)
    
    # Add text statistics
    words = text.split()
    result['statistics'] = {
        'character_count': len(text),
        'word_count': len(words),
        'sentence_count': text.count('.') + text.count('!') + text.count('?'),
        'average_word_length': sum(len(w) for w in words) / len(words) if words else 0,
        'unique_words': len(set(w.lower() for w in words))
    }
    
    result['request_timestamp'] = datetime.now().isoformat()
    
    return jsonify(result)


@app.errorhandler(404)
def not_found(error):
    """Handle 404 errors."""
    return jsonify({
        'error': 'Endpoint not found',
        'message': 'Please check the API documentation at /',
        'website': __website__
    }), 404


@app.errorhandler(500)
def server_error(error):
    """Handle 500 errors."""
    return jsonify({
        'error': 'Internal server error',
        'message': 'Please try again or contact support',
        'email': __email__
    }), 500


def create_demo_model():
    """Create a demo model for testing without training."""
    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.linear_model import LogisticRegression
    from sklearn.pipeline import Pipeline
    
    # Simple demo data
    texts = [
        "Apple launches new iPhone with AI features",
        "Manchester United wins Premier League",
        "Congress passes new legislation",
        "Taylor Swift breaks streaming records",
        "Stock market reaches all-time high",
        "NASA discovers new exoplanet"
    ]
    labels = [0, 1, 2, 3, 4, 5]
    
    # Create pipeline
    pipeline = Pipeline([
        ('vectorizer', TfidfVectorizer(max_features=1000)),
        ('classifier', LogisticRegression(max_iter=1000))
    ])
    
    pipeline.fit(texts, labels)
    
    return {
        'pipeline': pipeline,
        'algorithm': 'logistic_regression',
        'max_features': 1000,
        'ngram_range': (1, 1),
        'metadata': {
            'author': __author__,
            'website': __website__,
            'demo': True
        }
    }


def main():
    """Main entry point."""
    parser = argparse.ArgumentParser(
        description='Text Classification API - RSK World'
    )
    parser.add_argument(
        '--model', type=str, default='model.joblib',
        help='Path to trained model file'
    )
    parser.add_argument(
        '--port', type=int, default=5000,
        help='Port to run the server on'
    )
    parser.add_argument(
        '--host', type=str, default='0.0.0.0',
        help='Host to bind to'
    )
    parser.add_argument(
        '--debug', action='store_true',
        help='Run in debug mode'
    )
    parser.add_argument(
        '--demo', action='store_true',
        help='Run with demo model (no training required)'
    )
    
    args = parser.parse_args()
    
    print(f"\n{'='*60}")
    print("Text Classification API - RSK World")
    print(f"Author: {__author__}")
    print(f"Website: {__website__}")
    print(f"Email: {__email__}")
    print(f"{'='*60}\n")
    
    global model_data
    
    if args.demo:
        print("Creating demo model...")
        model_data = create_demo_model()
        print("Demo model created successfully!")
    else:
        try:
            load_model(args.model)
        except FileNotFoundError:
            print(f"Model not found at: {args.model}")
            print("Creating demo model instead...")
            model_data = create_demo_model()
    
    print(f"\nStarting server on http://{args.host}:{args.port}")
    print("Press Ctrl+C to stop\n")
    
    app.run(host=args.host, port=args.port, debug=args.debug)


if __name__ == '__main__':
    main()

462 lines•12.7 KB
python

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer