Update app.py
Browse files
app.py
CHANGED
|
@@ -1,463 +1,295 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import pickle
|
| 3 |
import pandas as pd
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
"""Initialize bias corrector with target distribution"""
|
| 10 |
-
if target_distribution is None:
|
| 11 |
-
self.target_distribution = {'negative': 0.33, 'neutral': 0.34, 'positive': 0.33}
|
| 12 |
-
else:
|
| 13 |
-
self.target_distribution = target_distribution
|
| 14 |
-
|
| 15 |
-
self.confidence_threshold = 0.7
|
| 16 |
-
self.bias_correction_factor = 0.15
|
| 17 |
-
|
| 18 |
-
def correct_prediction(self, prediction_result):
|
| 19 |
-
"""Apply bias correction to a prediction result"""
|
| 20 |
-
if not isinstance(prediction_result, dict):
|
| 21 |
-
return prediction_result
|
| 22 |
-
|
| 23 |
-
if 'scores' not in prediction_result:
|
| 24 |
-
return prediction_result
|
| 25 |
-
|
| 26 |
-
scores = prediction_result['scores']
|
| 27 |
-
original_sentiment = prediction_result['sentiment']
|
| 28 |
-
confidence = prediction_result['confidence']
|
| 29 |
-
|
| 30 |
-
if confidence < self.confidence_threshold:
|
| 31 |
-
corrected_scores = scores.copy()
|
| 32 |
-
|
| 33 |
-
if original_sentiment == 'negative' and confidence < 0.6:
|
| 34 |
-
corrected_scores['positive'] += self.bias_correction_factor
|
| 35 |
-
corrected_scores['neutral'] += self.bias_correction_factor * 0.5
|
| 36 |
-
corrected_scores['negative'] -= self.bias_correction_factor * 1.5
|
| 37 |
-
elif original_sentiment == 'positive' and confidence < 0.5:
|
| 38 |
-
corrected_scores['positive'] += self.bias_correction_factor * 0.5
|
| 39 |
-
|
| 40 |
-
total = sum(corrected_scores.values())
|
| 41 |
-
corrected_scores = {k: v/total for k, v in corrected_scores.items()}
|
| 42 |
-
|
| 43 |
-
new_sentiment = max(corrected_scores, key=corrected_scores.get)
|
| 44 |
-
new_confidence = corrected_scores[new_sentiment]
|
| 45 |
-
|
| 46 |
-
return {
|
| 47 |
-
'sentiment': new_sentiment,
|
| 48 |
-
'confidence': new_confidence,
|
| 49 |
-
'scores': corrected_scores,
|
| 50 |
-
'original_sentiment': original_sentiment,
|
| 51 |
-
'bias_corrected': True
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
prediction_result['bias_corrected'] = False
|
| 55 |
-
return prediction_result
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
'annoying', 'frustrating', 'disgusted', 'angry', 'upset'
|
| 69 |
-
]
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def predict(self, text):
|
| 74 |
-
"""Simple rule-based prediction with bias correction"""
|
| 75 |
-
text_lower = text.lower()
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
neg_ratio = negative_score / max(total_words, 1)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
sentiment = 'negative'
|
| 89 |
-
confidence = min(0.8, 0.5 + neg_ratio)
|
| 90 |
-
else:
|
| 91 |
-
sentiment = 'neutral'
|
| 92 |
-
confidence = 0.6
|
| 93 |
|
| 94 |
-
|
| 95 |
-
scores = {'positive': confidence, 'neutral': (1-confidence)*0.7, 'negative': (1-confidence)*0.3}
|
| 96 |
-
elif sentiment == 'negative':
|
| 97 |
-
scores = {'negative': confidence, 'neutral': (1-confidence)*0.7, 'positive': (1-confidence)*0.3}
|
| 98 |
-
else:
|
| 99 |
-
scores = {'neutral': confidence, 'positive': (1-confidence)*0.5, 'negative': (1-confidence)*0.5}
|
| 100 |
|
| 101 |
-
|
| 102 |
-
'
|
| 103 |
-
'confidence': confidence,
|
| 104 |
-
'scores': scores
|
| 105 |
-
}
|
| 106 |
|
| 107 |
-
return
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
def
|
| 114 |
-
"""
|
| 115 |
-
global loaded_model
|
| 116 |
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
try:
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
for
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
-
print("β No model files found")
|
| 143 |
-
return False
|
| 144 |
-
|
| 145 |
except Exception as e:
|
| 146 |
-
print(f"
|
| 147 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
def
|
| 150 |
-
"""
|
| 151 |
|
| 152 |
-
|
| 153 |
-
if loaded_model is None:
|
| 154 |
return (
|
| 155 |
-
"β **
|
| 156 |
pd.DataFrame(),
|
| 157 |
-
"Error
|
| 158 |
"Model not available"
|
| 159 |
)
|
| 160 |
|
| 161 |
-
# Check if text is provided
|
| 162 |
if not text or not text.strip():
|
| 163 |
return (
|
| 164 |
-
"β οΈ **Please enter
|
| 165 |
pd.DataFrame(),
|
| 166 |
"No input",
|
| 167 |
"Enter text above"
|
| 168 |
)
|
| 169 |
|
| 170 |
try:
|
| 171 |
-
|
| 172 |
-
clean_text = text.strip()
|
| 173 |
-
print(f"π Analyzing: {clean_text[:50]}{'...' if len(clean_text) > 50 else ''}")
|
| 174 |
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
if hasattr(model_obj, 'predict'):
|
| 183 |
-
result = model_obj.predict(clean_text)
|
| 184 |
-
else:
|
| 185 |
-
raise Exception("No prediction function available")
|
| 186 |
|
| 187 |
-
|
| 188 |
confidence = result['confidence']
|
| 189 |
-
scores = result
|
| 190 |
-
|
| 191 |
-
# Check if bias correction was applied
|
| 192 |
-
bias_corrected = result.get('bias_corrected', False)
|
| 193 |
-
original_sentiment = result.get('original_sentiment', predicted_sentiment)
|
| 194 |
|
| 195 |
-
# Create
|
| 196 |
-
|
| 197 |
'Sentiment': ['Negative', 'Neutral', 'Positive'],
|
| 198 |
-
'Confidence': [
|
| 199 |
-
scores.get('negative', 0),
|
| 200 |
-
scores.get('neutral', 0),
|
| 201 |
-
scores.get('positive', 0)
|
| 202 |
-
]
|
| 203 |
})
|
| 204 |
|
| 205 |
-
#
|
| 206 |
-
|
| 207 |
-
emoji = emoji_map.get(predicted_sentiment, 'π€')
|
| 208 |
|
| 209 |
-
#
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
bias_info = f"\nπ§ **Bias Correction Applied**\n Original prediction: {original_sentiment.title()}\n Adjusted to: {predicted_sentiment.title()}"
|
| 213 |
-
|
| 214 |
-
result_message = f"""
|
| 215 |
-
### {emoji} **{predicted_sentiment.title()}** Sentiment Detected
|
| 216 |
-
|
| 217 |
-
**Confidence Score:** {confidence:.1%}
|
| 218 |
|
| 219 |
-
**
|
| 220 |
|
| 221 |
-
**
|
| 222 |
-
- **Negative:** {scores.get('negative', 0):.1%}
|
| 223 |
-
- **Neutral:** {scores.get('neutral', 0):.1%}
|
| 224 |
-
- **Positive:** {scores.get('positive', 0):.1%}
|
| 225 |
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
-
|
| 229 |
"""
|
| 230 |
|
| 231 |
-
|
| 232 |
-
if bias_corrected:
|
| 233 |
-
status_message += " (bias corrected)"
|
| 234 |
-
|
| 235 |
-
return result_message, confidence_data, predicted_sentiment.title(), status_message
|
| 236 |
|
| 237 |
except Exception as e:
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
font-weight: bold;
|
| 253 |
-
}
|
| 254 |
-
.status-success {
|
| 255 |
-
background-color: #d4edda;
|
| 256 |
-
color: #155724;
|
| 257 |
-
border: 1px solid #c3e6cb;
|
| 258 |
-
}
|
| 259 |
-
.status-error {
|
| 260 |
-
background-color: #f8d7da;
|
| 261 |
-
color: #721c24;
|
| 262 |
-
border: 1px solid #f5c6cb;
|
| 263 |
-
}
|
| 264 |
-
.bias-correction {
|
| 265 |
-
background-color: #fff3cd;
|
| 266 |
-
color: #856404;
|
| 267 |
-
border: 1px solid #ffeaa7;
|
| 268 |
-
padding: 0.5rem;
|
| 269 |
-
border-radius: 5px;
|
| 270 |
-
margin: 0.5rem 0;
|
| 271 |
-
}
|
| 272 |
-
"""
|
| 273 |
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
gr.
|
| 278 |
-
|
| 279 |
-
<h1>π€ BERT Sentiment Classification</h1>
|
| 280 |
-
<p>Advanced AI-powered sentiment analysis with bias correction</p>
|
| 281 |
-
<p><strong>π§ Bias-Corrected Model - Fixed Negative Bias Issue</strong></p>
|
| 282 |
-
<p><strong>π Ready for permanent deployment</strong></p>
|
| 283 |
-
</div>
|
| 284 |
-
""")
|
| 285 |
-
|
| 286 |
-
# Model status indicator
|
| 287 |
-
model_status = gr.HTML()
|
| 288 |
-
|
| 289 |
-
with gr.Row():
|
| 290 |
-
with gr.Column(scale=3):
|
| 291 |
-
gr.Markdown("### π Enter Text for Sentiment Analysis")
|
| 292 |
-
|
| 293 |
-
text_input = gr.Textbox(
|
| 294 |
-
label="Input Text",
|
| 295 |
-
placeholder="Enter your text here... (e.g., 'This product is amazing! Great quality and fast delivery.')",
|
| 296 |
-
lines=6,
|
| 297 |
-
max_lines=20,
|
| 298 |
-
value=""
|
| 299 |
-
)
|
| 300 |
-
|
| 301 |
-
with gr.Row():
|
| 302 |
-
analyze_btn = gr.Button("π Analyze Sentiment", variant="primary", size="lg")
|
| 303 |
-
clear_btn = gr.Button("ποΈ Clear", size="sm")
|
| 304 |
-
|
| 305 |
-
gr.Markdown("### π‘ Example Texts to Try (Test Bias Correction):")
|
| 306 |
-
examples = gr.Examples(
|
| 307 |
-
examples=[
|
| 308 |
-
# Positive examples
|
| 309 |
-
["This product exceeded all my expectations! Outstanding quality and excellent customer service."],
|
| 310 |
-
["Best purchase I've made this year! Highly recommend to everyone."],
|
| 311 |
-
["The delivery was fast and the packaging was perfect!"],
|
| 312 |
-
|
| 313 |
-
# Negative examples
|
| 314 |
-
["I'm completely disappointed with this purchase. Poor quality and terrible customer support."],
|
| 315 |
-
["Absolutely horrible experience. Would never buy from this company again."],
|
| 316 |
-
["Customer service was unhelpful and rude."],
|
| 317 |
-
|
| 318 |
-
# Neutral/ambiguous examples (test bias correction)
|
| 319 |
-
["The product is decent. It works as described but nothing extraordinary."],
|
| 320 |
-
["It's okay, good value for the price but could be improved."],
|
| 321 |
-
["Not bad, not great. Just acceptable."],
|
| 322 |
-
|
| 323 |
-
# Edge cases (test bias correction)
|
| 324 |
-
["This is not bad at all"], # Double negative
|
| 325 |
-
["Could be better"], # Subtle negative
|
| 326 |
-
["Pretty good"], # Subtle positive
|
| 327 |
-
],
|
| 328 |
-
inputs=text_input,
|
| 329 |
-
label=None
|
| 330 |
-
)
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
)
|
| 338 |
-
|
| 339 |
-
# DataFrame-based BarPlot for compatibility
|
| 340 |
-
confidence_plot = gr.BarPlot(
|
| 341 |
-
x="Sentiment",
|
| 342 |
-
y="Confidence",
|
| 343 |
-
title="Confidence Scores by Sentiment Class",
|
| 344 |
-
x_title="Sentiment",
|
| 345 |
-
y_title="Confidence Score",
|
| 346 |
-
width=500,
|
| 347 |
-
height=300,
|
| 348 |
-
container=True
|
| 349 |
-
)
|
| 350 |
-
|
| 351 |
-
predicted_class = gr.Textbox(
|
| 352 |
-
label="Predicted Sentiment Class",
|
| 353 |
-
interactive=False,
|
| 354 |
-
value=""
|
| 355 |
-
)
|
| 356 |
-
|
| 357 |
-
status_display = gr.Textbox(
|
| 358 |
-
label="Analysis Status",
|
| 359 |
-
interactive=False,
|
| 360 |
-
value="Ready for analysis"
|
| 361 |
-
)
|
| 362 |
-
|
| 363 |
-
# Model Information Section
|
| 364 |
-
with gr.Accordion("π Model Information & Bias Correction Details", open=False):
|
| 365 |
-
gr.Markdown(f"""
|
| 366 |
-
### π§ Model Architecture
|
| 367 |
-
- **Base Model:** BERT-inspired with bias correction
|
| 368 |
-
- **Task:** Multi-class sentiment classification
|
| 369 |
-
- **Classes:** Negative π, Neutral π, Positive π
|
| 370 |
-
- **Device:** {model_device}
|
| 371 |
-
- **Bias Correction:** β
Enabled
|
| 372 |
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
- **Confidence Threshold:** Applies correction when confidence < 70%
|
| 377 |
-
- **Transparency:** Shows when bias correction is applied
|
| 378 |
|
| 379 |
-
###
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
|
| 385 |
-
|
| 386 |
-
1. **Standard Prediction:** Model makes initial sentiment prediction
|
| 387 |
-
2. **Confidence Check:** System checks if confidence is below threshold
|
| 388 |
-
3. **Bias Detection:** Identifies potential negative bias in low-confidence cases
|
| 389 |
-
4. **Score Adjustment:** Adjusts sentiment scores to reduce bias
|
| 390 |
-
5. **Re-evaluation:** Provides corrected prediction with transparency
|
| 391 |
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
|
|
|
| 398 |
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
inputs=text_input,
|
| 422 |
-
outputs=[result_output, confidence_plot, predicted_class, status_display]
|
| 423 |
-
)
|
| 424 |
-
|
| 425 |
-
clear_btn.click(
|
| 426 |
-
fn=clear_inputs,
|
| 427 |
-
outputs=[text_input, result_output, confidence_plot, predicted_class, status_display]
|
| 428 |
-
)
|
| 429 |
-
|
| 430 |
-
# Update model status on load
|
| 431 |
-
demo.load(
|
| 432 |
-
fn=update_model_status,
|
| 433 |
-
outputs=model_status
|
| 434 |
-
)
|
| 435 |
|
| 436 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
|
| 438 |
-
# Load model and launch interface
|
| 439 |
if __name__ == "__main__":
|
| 440 |
-
print("π Starting
|
| 441 |
print("=" * 60)
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
if model_loaded:
|
| 447 |
-
print("\nπ BIAS-CORRECTED MODEL READY FOR PREDICTIONS!")
|
| 448 |
-
print("β
Creating Gradio interface...")
|
| 449 |
-
|
| 450 |
-
# Create and launch interface
|
| 451 |
-
demo = create_gradio_interface()
|
| 452 |
-
|
| 453 |
-
print("π Launching web interface...")
|
| 454 |
-
print("π± The interface will open automatically")
|
| 455 |
-
print("π§ Bias correction enabled - negative bias issue fixed!")
|
| 456 |
-
print("=" * 60)
|
| 457 |
-
|
| 458 |
-
# Launch the interface
|
| 459 |
demo.launch()
|
| 460 |
else:
|
| 461 |
-
print("\nβ
|
| 462 |
-
print("
|
| 463 |
-
print(" python create_bias_corrected_model.py")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
import pickle
|
| 4 |
import pandas as pd
|
| 5 |
import os
|
| 6 |
+
import io
|
| 7 |
|
| 8 |
+
# Global variables
|
| 9 |
+
loaded_pipeline = None
|
| 10 |
+
model_device = 'cpu' # Force CPU for Hugging Face Spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def load_model():
|
| 13 |
+
"""Load the BERT sentiment model from pickle"""
|
| 14 |
+
global loaded_pipeline
|
| 15 |
+
|
| 16 |
+
print(f"π₯οΈ Using device: {model_device}")
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
model_file = 'sentiment_pipeline.pkl'
|
| 20 |
|
| 21 |
+
if not os.path.exists(model_file):
|
| 22 |
+
print(f"β Model file not found: {model_file}")
|
| 23 |
+
return False
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
print(f"π¦ Loading BERT model from {model_file}...")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Custom unpickler for CPU compatibility
|
| 28 |
+
class CPUUnpickler(pickle.Unpickler):
|
| 29 |
+
def find_class(self, module, name):
|
| 30 |
+
if module == 'torch.storage' and name == '_load_from_bytes':
|
| 31 |
+
return lambda b: torch.load(io.BytesIO(b), map_location='cpu')
|
| 32 |
+
else:
|
| 33 |
+
return super().find_class(module, name)
|
| 34 |
|
| 35 |
+
with open(model_file, 'rb') as f:
|
| 36 |
+
loaded_pipeline = pickle.load(f)
|
|
|
|
| 37 |
|
| 38 |
+
# Move model to CPU
|
| 39 |
+
if 'model' in loaded_pipeline:
|
| 40 |
+
loaded_pipeline['model'] = loaded_pipeline['model'].to('cpu')
|
| 41 |
+
loaded_pipeline['model'].eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
print(f"β
Successfully loaded BERT model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
if 'best_val_accuracy' in loaded_pipeline:
|
| 46 |
+
print(f"π― Validation Accuracy: {loaded_pipeline['best_val_accuracy']:.4f}")
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
return True
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"β Loading failed: {e}")
|
| 52 |
+
import traceback
|
| 53 |
+
traceback.print_exc()
|
| 54 |
+
return False
|
| 55 |
|
| 56 |
+
def predict_sentiment(text):
|
| 57 |
+
"""Predict sentiment using BERT model"""
|
|
|
|
| 58 |
|
| 59 |
+
if loaded_pipeline is None:
|
| 60 |
+
return {
|
| 61 |
+
'sentiment': 'error',
|
| 62 |
+
'confidence': 0.0,
|
| 63 |
+
'scores': {'negative': 0.0, 'neutral': 0.0, 'positive': 0.0},
|
| 64 |
+
'error': 'Model not loaded'
|
| 65 |
+
}
|
| 66 |
|
| 67 |
try:
|
| 68 |
+
model = loaded_pipeline['model']
|
| 69 |
+
tokenizer = loaded_pipeline['tokenizer']
|
| 70 |
+
max_length = loaded_pipeline.get('training_config', {}).get('max_length', 128)
|
| 71 |
+
|
| 72 |
+
# Tokenize
|
| 73 |
+
inputs = tokenizer(
|
| 74 |
+
text,
|
| 75 |
+
return_tensors='pt',
|
| 76 |
+
truncation=True,
|
| 77 |
+
padding=True,
|
| 78 |
+
max_length=max_length
|
| 79 |
+
)
|
| 80 |
|
| 81 |
+
inputs = {k: v.to('cpu') for k, v in inputs.items()}
|
| 82 |
+
|
| 83 |
+
# Predict
|
| 84 |
+
model.eval()
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
outputs = model(**inputs)
|
| 87 |
+
probabilities = torch.softmax(outputs.logits, dim=1)
|
| 88 |
+
prediction = torch.argmax(probabilities, dim=1).item()
|
| 89 |
+
confidence = probabilities.max().item()
|
| 90 |
+
|
| 91 |
+
sentiment_names = ['negative', 'neutral', 'positive']
|
| 92 |
+
|
| 93 |
+
return {
|
| 94 |
+
'sentiment': sentiment_names[prediction],
|
| 95 |
+
'confidence': confidence,
|
| 96 |
+
'scores': {
|
| 97 |
+
'negative': float(probabilities[0][0].item()),
|
| 98 |
+
'neutral': float(probabilities[0][1].item()),
|
| 99 |
+
'positive': float(probabilities[0][2].item())
|
| 100 |
+
}
|
| 101 |
+
}
|
| 102 |
|
|
|
|
|
|
|
|
|
|
| 103 |
except Exception as e:
|
| 104 |
+
print(f"Prediction error: {e}")
|
| 105 |
+
return {
|
| 106 |
+
'sentiment': 'error',
|
| 107 |
+
'confidence': 0.0,
|
| 108 |
+
'scores': {'negative': 0.0, 'neutral': 0.0, 'positive': 0.0},
|
| 109 |
+
'error': str(e)
|
| 110 |
+
}
|
| 111 |
|
| 112 |
+
def analyze_sentiment(text):
|
| 113 |
+
"""Analyze sentiment and return formatted results"""
|
| 114 |
|
| 115 |
+
if loaded_pipeline is None:
|
|
|
|
| 116 |
return (
|
| 117 |
+
"β **Model not loaded!** Please upload sentiment_pipeline.pkl",
|
| 118 |
pd.DataFrame(),
|
| 119 |
+
"Error",
|
| 120 |
"Model not available"
|
| 121 |
)
|
| 122 |
|
|
|
|
| 123 |
if not text or not text.strip():
|
| 124 |
return (
|
| 125 |
+
"β οΈ **Please enter text**",
|
| 126 |
pd.DataFrame(),
|
| 127 |
"No input",
|
| 128 |
"Enter text above"
|
| 129 |
)
|
| 130 |
|
| 131 |
try:
|
| 132 |
+
result = predict_sentiment(text.strip())
|
|
|
|
|
|
|
| 133 |
|
| 134 |
+
if 'error' in result:
|
| 135 |
+
return (
|
| 136 |
+
f"β **Error:** {result['error']}",
|
| 137 |
+
pd.DataFrame(),
|
| 138 |
+
"Error",
|
| 139 |
+
f"Error: {result['error']}"
|
| 140 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
sentiment = result['sentiment']
|
| 143 |
confidence = result['confidence']
|
| 144 |
+
scores = result['scores']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
# Create DataFrame for chart
|
| 147 |
+
chart_data = pd.DataFrame({
|
| 148 |
'Sentiment': ['Negative', 'Neutral', 'Positive'],
|
| 149 |
+
'Confidence': [scores['negative'], scores['neutral'], scores['positive']]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
})
|
| 151 |
|
| 152 |
+
# Emoji mapping
|
| 153 |
+
emoji = {'negative': 'π', 'neutral': 'π', 'positive': 'π'}[sentiment]
|
|
|
|
| 154 |
|
| 155 |
+
# Result message
|
| 156 |
+
message = f"""
|
| 157 |
+
### {emoji} **{sentiment.title()}** Sentiment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
**Confidence:** {confidence:.1%}
|
| 160 |
|
| 161 |
+
**Text:** *"{text[:100]}{'...' if len(text) > 100 else ''}"*
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
**Scores:**
|
| 164 |
+
- π Negative: {scores['negative']:.1%}
|
| 165 |
+
- π Neutral: {scores['neutral']:.1%}
|
| 166 |
+
- π Positive: {scores['positive']:.1%}
|
| 167 |
|
| 168 |
+
β
Bias-corrected BERT model
|
| 169 |
"""
|
| 170 |
|
| 171 |
+
return message, chart_data, sentiment.title(), f"β
{sentiment.title()} ({confidence:.1%})"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
except Exception as e:
|
| 174 |
+
return (
|
| 175 |
+
f"β **Error:** {str(e)}",
|
| 176 |
+
pd.DataFrame(),
|
| 177 |
+
"Error",
|
| 178 |
+
f"Error: {str(e)}"
|
| 179 |
+
)
|
| 180 |
|
| 181 |
+
# Create Gradio interface
|
| 182 |
+
with gr.Blocks(title="BERT Sentiment Analyzer", theme=gr.themes.Soft()) as demo:
|
| 183 |
|
| 184 |
+
gr.HTML("""
|
| 185 |
+
<div style="text-align: center; padding: 2rem; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 2rem;">
|
| 186 |
+
<h1>π€ BERT Sentiment Analyzer</h1>
|
| 187 |
+
<p style="font-size: 1.2em;">Bias-Corrected Sentiment Classification</p>
|
| 188 |
+
<p>β
Trained with balanced data β’ No negative bias</p>
|
| 189 |
+
</div>
|
| 190 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
+
model_status = gr.HTML()
|
| 193 |
+
|
| 194 |
+
with gr.Row():
|
| 195 |
+
with gr.Column(scale=3):
|
| 196 |
+
gr.Markdown("### π Input Text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
+
text_input = gr.Textbox(
|
| 199 |
+
label="Enter text to analyze",
|
| 200 |
+
placeholder="Example: 'This product is amazing! Great quality and excellent service.'",
|
| 201 |
+
lines=6
|
| 202 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
+
with gr.Row():
|
| 205 |
+
analyze_btn = gr.Button("π Analyze", variant="primary", size="lg")
|
| 206 |
+
clear_btn = gr.Button("ποΈ Clear", size="sm")
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
gr.Markdown("### π‘ Examples:")
|
| 209 |
+
gr.Examples(
|
| 210 |
+
examples=[
|
| 211 |
+
["This product is absolutely amazing! Best purchase ever!"],
|
| 212 |
+
["I love this so much! Outstanding quality!"],
|
| 213 |
+
["Excellent customer service and fast delivery!"],
|
| 214 |
+
["This is terrible! Worst product ever!"],
|
| 215 |
+
["Completely disappointed. Poor quality."],
|
| 216 |
+
["Awful experience. Would never buy again!"],
|
| 217 |
+
["The product is okay. Nothing special but works."],
|
| 218 |
+
["It's decent. Good value but could be better."],
|
| 219 |
+
["This is not bad at all"],
|
| 220 |
+
["Pretty good"],
|
| 221 |
+
],
|
| 222 |
+
inputs=text_input
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
with gr.Column(scale=2):
|
| 226 |
+
gr.Markdown("### π Results")
|
| 227 |
|
| 228 |
+
result_output = gr.Markdown("*Enter text to see results*")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
confidence_plot = gr.BarPlot(
|
| 231 |
+
x="Sentiment",
|
| 232 |
+
y="Confidence",
|
| 233 |
+
title="Confidence Scores",
|
| 234 |
+
width=500,
|
| 235 |
+
height=300
|
| 236 |
+
)
|
| 237 |
|
| 238 |
+
predicted_class = gr.Textbox(label="Prediction", interactive=False)
|
| 239 |
+
status_display = gr.Textbox(label="Status", interactive=False, value="Ready")
|
| 240 |
+
|
| 241 |
+
with gr.Accordion("βΉοΈ Model Info", open=False):
|
| 242 |
+
gr.Markdown("""
|
| 243 |
+
### π§ Model
|
| 244 |
+
- **Architecture:** BERT (bert-base-uncased)
|
| 245 |
+
- **Classes:** Negative π, Neutral π, Positive π
|
| 246 |
+
- **Training:** Balanced dataset with class weights
|
| 247 |
+
|
| 248 |
+
### π§ Features
|
| 249 |
+
- β
No negative bias
|
| 250 |
+
- β
Balanced training data
|
| 251 |
+
- β
Class-weighted loss
|
| 252 |
+
- β
CPU optimized
|
| 253 |
+
|
| 254 |
+
### π Configuration
|
| 255 |
+
- Epochs: 4
|
| 256 |
+
- Learning Rate: 1e-5
|
| 257 |
+
- Batch Size: 16
|
| 258 |
+
- Max Length: 128 tokens
|
| 259 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
+
def clear_all():
|
| 262 |
+
return "", "*Enter text*", pd.DataFrame(), "", "Ready"
|
| 263 |
+
|
| 264 |
+
def update_status():
|
| 265 |
+
if loaded_pipeline:
|
| 266 |
+
val_acc = loaded_pipeline.get('best_val_accuracy', 'N/A')
|
| 267 |
+
return f"""<div style="padding: 1rem; background: #d4edda; color: #155724; border-radius: 8px; text-align: center;">
|
| 268 |
+
β
Model Loaded | Accuracy: {val_acc if isinstance(val_acc, str) else f'{val_acc:.2%}'}</div>"""
|
| 269 |
+
return """<div style="padding: 1rem; background: #f8d7da; color: #721c24; border-radius: 8px; text-align: center;">
|
| 270 |
+
β Model Not Loaded</div>"""
|
| 271 |
+
|
| 272 |
+
analyze_btn.click(
|
| 273 |
+
fn=analyze_sentiment,
|
| 274 |
+
inputs=text_input,
|
| 275 |
+
outputs=[result_output, confidence_plot, predicted_class, status_display]
|
| 276 |
+
)
|
| 277 |
+
|
| 278 |
+
clear_btn.click(
|
| 279 |
+
fn=clear_all,
|
| 280 |
+
outputs=[text_input, result_output, confidence_plot, predicted_class, status_display]
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
demo.load(fn=update_status, outputs=model_status)
|
| 284 |
|
|
|
|
| 285 |
if __name__ == "__main__":
|
| 286 |
+
print("π Starting BERT Sentiment Analyzer...")
|
| 287 |
print("=" * 60)
|
| 288 |
|
| 289 |
+
if load_model():
|
| 290 |
+
print("\nβ
MODEL READY!")
|
| 291 |
+
print("π Launching interface...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
demo.launch()
|
| 293 |
else:
|
| 294 |
+
print("\nβ FAILED TO LOAD MODEL!")
|
| 295 |
+
print("π Ensure sentiment_pipeline.pkl exists")
|
|
|