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 |
+
import torch
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# --- 1. CONFIGURATION ---
|
| 8 |
+
MODEL_PATH = "."
|
| 9 |
+
LABELS = ["anger", "fear", "joy", "sadness", "surprise"]
|
| 10 |
+
DEFAULT_THRESHOLDS = {"anger": 0.5, "fear": 0.5, "joy": 0.5, "sadness": 0.5, "surprise": 0.5}
|
| 11 |
+
|
| 12 |
+
# --- 2. LOAD RESOURCES ---
|
| 13 |
+
# Load Model
|
| 14 |
+
try:
|
| 15 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 16 |
+
# Check if safe tensors actually exist
|
| 17 |
+
if not os.path.exists(os.path.join(MODEL_PATH, "model.safetensors")):
|
| 18 |
+
raise FileNotFoundError
|
| 19 |
+
except:
|
| 20 |
+
model = None # Fallback
|
| 21 |
+
|
| 22 |
+
# Load Tokenizer
|
| 23 |
+
try:
|
| 24 |
+
tokenizer = AutoTokenizer.from_pretrained("roberta-large")
|
| 25 |
+
except:
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained("roberta-base")
|
| 27 |
+
|
| 28 |
+
# Load Thresholds
|
| 29 |
+
if os.path.exists("fine_thresholds.json"):
|
| 30 |
+
with open("fine_thresholds.json", "r") as f:
|
| 31 |
+
THRESHOLDS = json.load(f)
|
| 32 |
+
else:
|
| 33 |
+
THRESHOLDS = DEFAULT_THRESHOLDS
|
| 34 |
+
|
| 35 |
+
# --- 3. LOGIC ---
|
| 36 |
+
def analyze(text):
|
| 37 |
+
if not text or not text.strip():
|
| 38 |
+
return {}, "⚠️ Please type something first!"
|
| 39 |
+
if model is None:
|
| 40 |
+
return {}, "⚠️ Model files missing in this folder."
|
| 41 |
+
|
| 42 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 43 |
+
model.eval()
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
outputs = model(**inputs)
|
| 46 |
+
probs = torch.sigmoid(outputs.logits).numpy()[0]
|
| 47 |
+
|
| 48 |
+
results = {}
|
| 49 |
+
detected_emotions = []
|
| 50 |
+
|
| 51 |
+
for i, label in enumerate(LABELS):
|
| 52 |
+
score = float(probs[i])
|
| 53 |
+
results[label] = score
|
| 54 |
+
if score > THRESHOLDS.get(label, 0.5):
|
| 55 |
+
detected_emotions.append(label.upper())
|
| 56 |
+
|
| 57 |
+
if not detected_emotions:
|
| 58 |
+
final_verdict = "😐 No strong emotion detected."
|
| 59 |
+
else:
|
| 60 |
+
final_verdict = " ".join([f"**{e}**" for e in detected_emotions])
|
| 61 |
+
|
| 62 |
+
return results, final_verdict
|
| 63 |
+
|
| 64 |
+
# --- 4. PROFESSIONAL CSS (The "Form" Look) ---
|
| 65 |
+
custom_css = """
|
| 66 |
+
/* 1. Background Gradient */
|
| 67 |
+
.gradio-container {
|
| 68 |
+
background: linear-gradient(135deg, #e0e7ff 0%, #f3f4f6 100%) !important;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/* 2. The White Card Container */
|
| 72 |
+
.form-card {
|
| 73 |
+
max-width:600px;
|
| 74 |
+
margin:auto;
|
| 75 |
+
background: white !important;
|
| 76 |
+
border-radius: 12px !important;
|
| 77 |
+
padding: 30px !important;
|
| 78 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.1) !important;
|
| 79 |
+
border-top: 6px solid #6366f1 !important; /* Indigo Top Bar */
|
| 80 |
+
border: 1px solid #e5e7eb !important;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
/* 3. Header Text */
|
| 84 |
+
.form-header h1 {
|
| 85 |
+
color: #4338ca;
|
| 86 |
+
text-align: center;
|
| 87 |
+
font-weight: 800;
|
| 88 |
+
margin-bottom: 5px;
|
| 89 |
+
}
|
| 90 |
+
.form-header p {
|
| 91 |
+
color: #6b7280;
|
| 92 |
+
text-align: center;
|
| 93 |
+
margin-bottom: 20px;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
/* 4. Button Styling */
|
| 97 |
+
button.primary {
|
| 98 |
+
background: #4f46e5 !important;
|
| 99 |
+
font-size: 1.1em !important;
|
| 100 |
+
}
|
| 101 |
+
"""
|
| 102 |
+
|
| 103 |
+
# --- 5. UI LAYOUT ---
|
| 104 |
+
theme = gr.themes.Base(primary_hue="indigo", font=[gr.themes.GoogleFont("Inter"), "system-ui"])
|
| 105 |
+
|
| 106 |
+
with gr.Blocks( title="Sentiment Check") as app:
|
| 107 |
+
|
| 108 |
+
# We wrap everything in a Column with our custom 'form-card' class
|
| 109 |
+
with gr.Column(elem_classes="form-card"):
|
| 110 |
+
|
| 111 |
+
# Header
|
| 112 |
+
gr.HTML("""
|
| 113 |
+
<div class="form-header">
|
| 114 |
+
<h1>🧠 Sentiment Check</h1>
|
| 115 |
+
<p>Advanced Neural Emotion Detection System</p>
|
| 116 |
+
</div>
|
| 117 |
+
""")
|
| 118 |
+
|
| 119 |
+
# Section 1: Input
|
| 120 |
+
gr.Markdown("### 1. Source Text")
|
| 121 |
+
txt_input = gr.Textbox(
|
| 122 |
+
lines=4,
|
| 123 |
+
placeholder="Type your text here...",
|
| 124 |
+
show_label=False,
|
| 125 |
+
container=False # Makes it look cleaner
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Action Button
|
| 129 |
+
btn_run = gr.Button("GENERATE REPORT", variant="primary", size="lg")
|
| 130 |
+
|
| 131 |
+
# Divider
|
| 132 |
+
gr.HTML("<hr style='margin: 20px 0; border: 0; border-top: 1px solid #e5e7eb;'>")
|
| 133 |
+
|
| 134 |
+
# Section 2: Results
|
| 135 |
+
gr.Markdown("### 2. Analysis Results")
|
| 136 |
+
with gr.Row():
|
| 137 |
+
with gr.Column(scale=1):
|
| 138 |
+
lbl_verdict = gr.Markdown("**Status:** Waiting for data...", label="Prediction")
|
| 139 |
+
with gr.Column(scale=1):
|
| 140 |
+
lbl_chart = gr.Label(num_top_classes=5, label="Confidence Spectrum")
|
| 141 |
+
|
| 142 |
+
# Footer
|
| 143 |
+
gr.Markdown(
|
| 144 |
+
"<div style='text-align:center; color:#9ca3af; margin-top:15px; font-size:0.8em;'>Secure Local Deployment • Powered by RoBERTa</div>"
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# Logic
|
| 148 |
+
btn_run.click(fn=analyze, inputs=txt_input, outputs=[lbl_chart, lbl_verdict])
|
| 149 |
+
|
| 150 |
+
app.launch(theme=theme, css=custom_css,share=False)#,auth=("admin", "pass123"))
|