import gradio as gr from transformers import AutoTokenizer, AutoModelForSequenceClassification import torch import json import os # --- 1. CONFIGURATION --- MODEL_PATH = "rup69/Sentiment-Analysis" LABELS = ["anger", "fear", "joy", "sadness", "surprise"] DEFAULT_THRESHOLDS = {"anger": 0.5, "fear": 0.5, "joy": 0.5, "sadness": 0.5, "surprise": 0.5} # --- 2. LOAD RESOURCES --- # Load Model try: model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH) print("✅ Model loaded from Model Repo!") except Exception as e: model = None print(f"❌ Error loading model: {e}") # Load Tokenizer try: tokenizer = AutoTokenizer.from_pretrained("roberta-large") except: tokenizer = AutoTokenizer.from_pretrained("roberta-base") # Load Thresholds if os.path.exists("fine_tune_thresholds.json"): with open("fine_tune_thresholds.json", "r") as f: THRESHOLDS = json.load(f) else: THRESHOLDS = DEFAULT_THRESHOLDS # --- 3. LOGIC --- def analyze(text): # 1. Simple Check: Is the model variable loaded? if model is None: return {}, "⚠️ Model failed to load. Check logs." # 2. Check for Empty Text if not text or not text.strip(): return {}, "⚠️ Please type something first!" inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512) model.eval() with torch.no_grad(): outputs = model(**inputs) probs = torch.sigmoid(outputs.logits).numpy()[0] results = {} detected_emotions = [] # Penalties for "Sleep" bias penalties = {"sleep": {"fear": 0.50}, "tired": {"fear": 0.40}, "hungry": {"fear": 0.40}} text_lower = text.lower() for i, label in enumerate(LABELS): score = float(probs[i]) for word, penalty_map in penalties.items(): if word in text_lower and label in penalty_map: score = max(0.0, score - penalty_map[label]) results[label] = score # Use simple 0.5 threshold if score > 0.5: detected_emotions.append(label.upper()) if not detected_emotions: final_verdict = "😐 No strong emotion detected." else: final_verdict = " ".join([f"**{e}**" for e in detected_emotions]) return results, final_verdict # --- 4. PROFESSIONAL CSS (The "Form" Look) --- custom_css = """ /* 1. Background Gradient */ .gradio-container { background: linear-gradient(135deg, #e0e7ff 0%, #f3f4f6 100%) !important; } /* 2. The White Card Container */ .form-card { max-width:600px; margin:auto; background: white !important; border-radius: 12px !important; padding: 30px !important; box-shadow: 0 10px 30px rgba(0,0,0,0.1) !important; border-top: 6px solid #6366f1 !important; /* Indigo Top Bar */ border: 1px solid #e5e7eb !important; } /* 3. Header Text */ .form-header h1 { color: #4338ca; text-align: center; font-weight: 800; margin-bottom: 5px; } .form-header p { color: #6b7280; text-align: center; margin-bottom: 20px; } /* 4. Button Styling */ button.primary { background: #4f46e5 !important; font-size: 1.1em !important; } """ # --- 5. UI LAYOUT --- theme = gr.themes.Base(primary_hue="indigo", font=[gr.themes.GoogleFont("Inter"), "system-ui"]) with gr.Blocks( title="Sentiment Check") as app: # We wrap everything in a Column with our custom 'form-card' class with gr.Column(elem_classes="form-card"): # Header gr.HTML("""
Advanced Neural Emotion Detection System