Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -17,6 +17,7 @@ except:
|
|
| 17 |
|
| 18 |
device = torch.device("cpu")
|
| 19 |
|
|
|
|
| 20 |
universal_labels = ['fear', 'anger', 'sadness', 'disgust', 'joy', 'surprise']
|
| 21 |
fixed_label2id = {lbl: idx for idx, lbl in enumerate(universal_labels)}
|
| 22 |
fixed_id2label = {idx: lbl for idx, lbl in enumerate(universal_labels)}
|
|
@@ -82,7 +83,7 @@ def standardize_text_v2(text):
|
|
| 82 |
|
| 83 |
print("Loading models with configuration overrides...")
|
| 84 |
|
| 85 |
-
#
|
| 86 |
bb_config = AutoConfig.from_pretrained("Akash751/banglabert-code-mixed-emotion")
|
| 87 |
bb_config.num_labels = 6
|
| 88 |
bb_config.label2id = fixed_label2id
|
|
@@ -91,7 +92,7 @@ bb_config.id2label = fixed_id2label
|
|
| 91 |
bb_tokenizer = AutoTokenizer.from_pretrained("csebuetnlp/banglabert")
|
| 92 |
bb_model = AutoModelForSequenceClassification.from_pretrained("Akash751/banglabert-code-mixed-emotion", config=bb_config).to(device)
|
| 93 |
|
| 94 |
-
#
|
| 95 |
rob_config = AutoConfig.from_pretrained("Akash751/roberta-code-mixed-emotion")
|
| 96 |
rob_config.num_labels = 6
|
| 97 |
rob_config.label2id = fixed_label2id
|
|
@@ -120,7 +121,7 @@ def predict_emotion(user_text):
|
|
| 120 |
bb_dict = {universal_labels[i].capitalize(): float(bb_probs[i]) for i in range(len(bb_probs))}
|
| 121 |
rob_dict = {universal_labels[i].capitalize(): float(rob_probs[i]) for i in range(len(rob_probs))}
|
| 122 |
|
| 123 |
-
# 2. Optimized Ensemble Fusion (0.87
|
| 124 |
final_w_bb = 0.87
|
| 125 |
final_w_rob = 0.13
|
| 126 |
|
|
@@ -132,27 +133,29 @@ def predict_emotion(user_text):
|
|
| 132 |
r_score = rob_dict.get(label, 0.0)
|
| 133 |
result_dict[label] = (b_score * final_w_bb) + (r_score * final_w_rob)
|
| 134 |
|
| 135 |
-
# 3.
|
| 136 |
-
negation_patterns = ['na', 'ni', 'nai', 'না', 'নি', 'নয়', 'নাই', 'হয়নি'
|
| 137 |
-
joy_patterns = ['valo', 'bhalo', 'ভালো', 'ভালোই', 'ভাল', 'khushi', 'খুশি', 'sundor', 'সুন্দর', 'anondo', '
|
| 138 |
|
| 139 |
-
# Check both raw user text and cleaned text to guarantee safety
|
| 140 |
combined_raw_text = " " + user_text.lower() + " " + clean_text + " "
|
| 141 |
|
| 142 |
has_negation = any(tok in combined_raw_text for tok in negation_patterns)
|
| 143 |
has_joy_base = any(tok in combined_raw_text for tok in joy_patterns)
|
| 144 |
|
| 145 |
-
#
|
| 146 |
if has_negation and has_joy_base:
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
result_dict['
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
elif has_negation:
|
| 155 |
-
# Prevent false amplification of Fear/Anger on generic negations
|
| 156 |
if result_dict['Fear'] > 0.4 or result_dict['Anger'] > 0.4:
|
| 157 |
old_fear = result_dict['Fear']
|
| 158 |
old_anger = result_dict['Anger']
|
|
|
|
| 17 |
|
| 18 |
device = torch.device("cpu")
|
| 19 |
|
| 20 |
+
# Absolute Fixed Label Space to avoid Internal Alignment Corruptions
|
| 21 |
universal_labels = ['fear', 'anger', 'sadness', 'disgust', 'joy', 'surprise']
|
| 22 |
fixed_label2id = {lbl: idx for idx, lbl in enumerate(universal_labels)}
|
| 23 |
fixed_id2label = {idx: lbl for idx, lbl in enumerate(universal_labels)}
|
|
|
|
| 83 |
|
| 84 |
print("Loading models with configuration overrides...")
|
| 85 |
|
| 86 |
+
# Secure Initialization for BanglaBERT
|
| 87 |
bb_config = AutoConfig.from_pretrained("Akash751/banglabert-code-mixed-emotion")
|
| 88 |
bb_config.num_labels = 6
|
| 89 |
bb_config.label2id = fixed_label2id
|
|
|
|
| 92 |
bb_tokenizer = AutoTokenizer.from_pretrained("csebuetnlp/banglabert")
|
| 93 |
bb_model = AutoModelForSequenceClassification.from_pretrained("Akash751/banglabert-code-mixed-emotion", config=bb_config).to(device)
|
| 94 |
|
| 95 |
+
# Secure Initialization for XLM-RoBERTa
|
| 96 |
rob_config = AutoConfig.from_pretrained("Akash751/roberta-code-mixed-emotion")
|
| 97 |
rob_config.num_labels = 6
|
| 98 |
rob_config.label2id = fixed_label2id
|
|
|
|
| 121 |
bb_dict = {universal_labels[i].capitalize(): float(bb_probs[i]) for i in range(len(bb_probs))}
|
| 122 |
rob_dict = {universal_labels[i].capitalize(): float(rob_probs[i]) for i in range(len(rob_probs))}
|
| 123 |
|
| 124 |
+
# 2. Optimized Ensemble Fusion (0.87 vs 0.13)
|
| 125 |
final_w_bb = 0.87
|
| 126 |
final_w_rob = 0.13
|
| 127 |
|
|
|
|
| 133 |
r_score = rob_dict.get(label, 0.0)
|
| 134 |
result_dict[label] = (b_score * final_w_bb) + (r_score * final_w_rob)
|
| 135 |
|
| 136 |
+
# 3. Double-Layer Intent Engine (Resolves Both Positive and Negation Mapping Bugs)
|
| 137 |
+
negation_patterns = ['na', 'ni', 'nai', 'না', 'নি', 'নয়', 'নাই', 'হয়নি']
|
| 138 |
+
joy_patterns = ['valo', 'bhalo', 'ভালো', 'ভালোই', 'ভাল', 'khushi', 'খুশি', 'sundor', 'সুন্দর', 'anondo', 'आनंद']
|
| 139 |
|
|
|
|
| 140 |
combined_raw_text = " " + user_text.lower() + " " + clean_text + " "
|
| 141 |
|
| 142 |
has_negation = any(tok in combined_raw_text for tok in negation_patterns)
|
| 143 |
has_joy_base = any(tok in combined_raw_text for tok in joy_patterns)
|
| 144 |
|
| 145 |
+
# LAYER A: যদি আনন্দের শব্দ থাকে এবং সাথে 'না' থাকে (যেমন: ভালো লাগছে না) -> বিষাদ (Sadness)
|
| 146 |
if has_negation and has_joy_base:
|
| 147 |
+
for k in result_dict:
|
| 148 |
+
result_dict[k] = 0.0
|
| 149 |
+
result_dict['Sadness'] = 1.0
|
| 150 |
+
|
| 151 |
+
# LAYER B: যদি আনন্দের শব্দ থাকে কিন্তু কোনো 'না' না থাকে (যেমন: আজকে ভালো লাগছে) -> আনন্দ (Joy)
|
| 152 |
+
elif has_joy_base and not has_negation:
|
| 153 |
+
for k in result_dict:
|
| 154 |
+
result_dict[k] = 0.0
|
| 155 |
+
result_dict['Joy'] = 1.0 # Lock prediction directly to Joy, neutralizing Disgust/Fear bugs
|
| 156 |
+
|
| 157 |
+
# LAYER C: সাধারণ নেতিবাচক ফিল্টারিং অবশিষ্টাংশের জন্য
|
| 158 |
elif has_negation:
|
|
|
|
| 159 |
if result_dict['Fear'] > 0.4 or result_dict['Anger'] > 0.4:
|
| 160 |
old_fear = result_dict['Fear']
|
| 161 |
old_anger = result_dict['Anger']
|