Spaces:
Sleeping
Sleeping
| 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 | |
| ry: | |
| 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_thresholds.json"): | |
| with open("fine_thresholds.json", "r") as f: | |
| THRESHOLDS = json.load(f) | |
| else: | |
| THRESHOLDS = DEFAULT_THRESHOLDS | |
| # --- 3. LOGIC --- | |
| def analyze(text): | |
| if not text or not text.strip(): | |
| return {}, "⚠️ Please type something first!" | |
| if model is None: | |
| return {}, "⚠️ Model files missing in this folder." | |
| 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 = [] | |
| for i, label in enumerate(LABELS): | |
| score = float(probs[i]) | |
| results[label] = score | |
| if score > THRESHOLDS.get(label, 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(""" | |
| <div class="form-header"> | |
| <h1>🧠 Sentiment Check</h1> | |
| <p>Advanced Neural Emotion Detection System</p> | |
| </div> | |
| """) | |
| # Section 1: Input | |
| gr.Markdown("### 1. Source Text") | |
| txt_input = gr.Textbox( | |
| lines=4, | |
| placeholder="Type your text here...", | |
| show_label=False, | |
| container=False # Makes it look cleaner | |
| ) | |
| # Action Button | |
| btn_run = gr.Button("GENERATE REPORT", variant="primary", size="lg") | |
| # Divider | |
| gr.HTML("<hr style='margin: 20px 0; border: 0; border-top: 1px solid #e5e7eb;'>") | |
| # Section 2: Results | |
| gr.Markdown("### 2. Analysis Results") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| lbl_verdict = gr.Markdown("**Status:** Waiting for data...", label="Prediction") | |
| with gr.Column(scale=1): | |
| lbl_chart = gr.Label(num_top_classes=5, label="Confidence Spectrum") | |
| # Footer | |
| gr.Markdown( | |
| "<div style='text-align:center; color:#9ca3af; margin-top:15px; font-size:0.8em;'>Secure Local Deployment • Powered by RoBERTa</div>" | |
| ) | |
| # Logic | |
| btn_run.click(fn=analyze, inputs=txt_input, outputs=[lbl_chart, lbl_verdict]) | |
| app.launch(theme=theme, css=custom_css,share=False)#,auth=("admin", "pass123")) |