Sentiment Analysis Model

A simple sentiment analysis model that classifies text as positive, negative, or neutral using TF-IDF vectorization and Multinomial Naive Bayes.

Model Details

  • Model Type: Sentiment Classifier
  • Algorithm: TF-IDF + Multinomial Naive Bayes
  • Classes: Positive, Negative, Neutral
  • Framework: Scikit-learn

Usage

Using Hugging Face Inference API

import requests

API_URL = "https://api-inference.huggingface.co/models/GunjanSingh/sentiment-analysis-model"
headers = {"Authorization": f"Bearer {YOUR_TOKEN}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()

output = query({
    "inputs": "I love this product!"
})

Using Transformers Pipeline

from transformers import pipeline

classifier = pipeline("text-classification", model="GunjanSingh/sentiment-analysis-model")
result = classifier("I love this product!")

Using Direct API Call

curl -X POST "https://api-inference.huggingface.co/models/GunjanSingh/sentiment-analysis-model" \
     -H "Authorization: Bearer YOUR_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"inputs": "I love this product!"}'

Example Output

{
    "text": "I love this product!",
    "prediction": "positive",
    "confidence": 0.85,
    "probabilities": {
        "negative": 0.05,
        "neutral": 0.10,
        "positive": 0.85
    }
}

Training

This model is trained on sample data using:

  • TF-IDF vectorization with 1000 features
  • English stop words removal
  • 1-2 gram combinations
  • Multinomial Naive Bayes classifier

Performance

  • Training Time: < 1 second
  • Inference Time: < 10ms per prediction
  • Memory Usage: ~10MB
  • Accuracy: ~85% on sample data

Alternative: Custom Space API

For more advanced features, you can also use the custom Space API:

import requests

# Custom Space API (with more features)
space_url = "https://GunjanSingh-sentiment-analysis-demo.hf.space"

response = requests.post(f"{space_url}/predict", 
                        json={"text": "I love this product!"})
result = response.json()

Model Files

  • model.pkl: Trained scikit-learn model
  • config.json: Model configuration
  • model.py: Inference pipeline
  • requirements.txt: Dependencies

Testing

You can test the model locally:

from model import pipeline

# Create pipeline
classifier = pipeline("text-classification")

# Test prediction
result = classifier("I love this product!")
print(result)

License

MIT License - feel free to use this model for your projects!

Downloads last month
2
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support