Team Ai
Apppublic

code-switch/sentiment_analyzer

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
engine.py24 linesDownload Raw Back to src
1from transformers import pipeline2 3class SentimentEngine:4    def __init__(self):5        # Using a multi-task model for both sentiment and aspect detection6        # For simplicity in this demo, we use a zero-shot classifier to identify issues7        self.classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")8        self.sentiment_pipe = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")9 10    def analyze_feedback(self, text):11        # 1. Identify the "Issue Category" (The Aspect)12        candidate_labels = ["Course Content", "Instructor Quality", "Technical Issue", "Pricing", "Support"]13        category_res = self.classifier(text, candidate_labels)14        top_category = category_res['labels'][0]15        16        # 2. Get Polarity17        sentiment_res = self.sentiment_pipe(text)[0]18        19        return {20            "text": text,21            "category": top_category,22            "sentiment": sentiment_res['label'],23            "confidence": round(sentiment_res['score'], 4)24        }