Instructions to use Ahmed766/text-classification-demo with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Ahmed766/text-classification-demo with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="Ahmed766/text-classification-demo")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Ahmed766/text-classification-demo", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download text_classifier.py from Ahmed766/text-classification-demo: direct link, hf CLI and curl.
- Browser
- Download file 3.02 kB
-
https://huggingface.co/Ahmed766/text-classification-demo/resolve/main/text_classifier.py
- Command line
-
hf download hf://Ahmed766/text-classification-demo/text_classifier.py
-
curl -L -o text_classifier.py https://huggingface.co/Ahmed766/text-classification-demo/resolve/main/text_classifier.py
3.02 kB
| """ | |
| Simplified Text Classification Model | |
| A lightweight sentiment analysis model for demonstration | |
| """ | |
| import json | |
| import re | |
| from collections import Counter | |
| class TextClassifier: | |
| def __init__(self): | |
| """ | |
| Initialize the text classifier with simple heuristics | |
| In a real implementation, this would load a pre-trained model | |
| """ | |
| # Simple sentiment word lists for demonstration | |
| self.positive_words = { | |
| 'good', 'great', 'excellent', 'amazing', 'love', 'wonderful', | |
| 'fantastic', 'awesome', 'brilliant', 'perfect', 'nice', 'best', | |
| 'superb', 'outstanding', 'delightful', 'pleasure', 'happy', | |
| 'joy', 'pleased', 'satisfied', 'impressed' | |
| } | |
| self.negative_words = { | |
| 'bad', 'terrible', 'awful', 'horrible', 'hate', 'worst', | |
| 'disgusting', 'disappointing', 'annoying', 'frustrating', | |
| 'sad', 'angry', 'upset', 'displeased', 'regret', 'miserable', | |
| 'pathetic', 'useless', 'broken', 'wrong' | |
| } | |
| def predict(self, text): | |
| """ | |
| Predict the sentiment of the input text using simple heuristics | |
| """ | |
| # Convert to lowercase and split into words | |
| words = re.findall(r'\b\w+\b', text.lower()) | |
| # Count positive and negative words | |
| pos_count = sum(1 for word in words if word in self.positive_words) | |
| neg_count = sum(1 for word in words if word in self.negative_words) | |
| # Determine sentiment based on counts | |
| if pos_count > neg_count: | |
| sentiment = "positive" | |
| confidence = min(0.95, 0.5 + (pos_count / (pos_count + neg_count + 1))) | |
| elif neg_count > pos_count: | |
| sentiment = "negative" | |
| confidence = min(0.95, 0.5 + (neg_count / (pos_count + neg_count + 1))) | |
| else: | |
| # If equal, look for sentiment intensifiers or just return neutral | |
| sentiment = "neutral" | |
| confidence = 0.5 | |
| return { | |
| "label": sentiment, | |
| "confidence": round(confidence, 4), | |
| "text": text, | |
| "positive_words_found": pos_count, | |
| "negative_words_found": neg_count | |
| } | |
| def main(): | |
| # Example usage | |
| classifier = TextClassifier() | |
| examples = [ | |
| "I love this product! It's amazing.", | |
| "This is terrible. I hate it.", | |
| "It's okay, nothing special.", | |
| "The service was excellent and the staff was wonderful!", | |
| "Completely disappointed with this purchase." | |
| ] | |
| print("Text Classification Demo:") | |
| print("=" * 50) | |
| for text in examples: | |
| result = classifier.predict(text) | |
| print(f"Text: {result['text']}") | |
| print(f"Label: {result['label']}, Confidence: {result['confidence']}") | |
| print(f"Positive words: {result['positive_words_found']}, Negative words: {result['negative_words_found']}") | |
| print("-" * 30) | |
| if __name__ == "__main__": | |
| main() |