Update sentiment_analyzer.py
Browse files- sentiment_analyzer.py +22 -10
sentiment_analyzer.py
CHANGED
|
@@ -3,13 +3,13 @@ import torch
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
class NewsAnalyzer:
|
| 6 |
-
def __init__(self, model_name=None):
|
| 7 |
"""
|
| 8 |
Initialize news analyzer with fast, CPU-friendly Zero-Shot pipelines
|
| 9 |
"""
|
| 10 |
print("Initializing Zero-Shot News Analyzer...")
|
| 11 |
|
| 12 |
-
self.device = 0 if torch.cuda.is_available() else -1
|
| 13 |
print(f"Using device: {'cuda' if self.device == 0 else 'cpu'}")
|
| 14 |
|
| 15 |
try:
|
|
@@ -23,11 +23,13 @@ class NewsAnalyzer:
|
|
| 23 |
|
| 24 |
# Pipeline 2: For Zero-Shot Classification (Theme & Impact)
|
| 25 |
print("Loading Zero-Shot model...")
|
|
|
|
| 26 |
self.classifier_pipeline = pipeline(
|
| 27 |
"zero-shot-classification",
|
| 28 |
-
model="
|
| 29 |
device=self.device
|
| 30 |
)
|
|
|
|
| 31 |
|
| 32 |
print("Models loaded successfully!")
|
| 33 |
|
|
@@ -47,15 +49,23 @@ class NewsAnalyzer:
|
|
| 47 |
"""
|
| 48 |
วิเคราะห์ข่าว (Sentiment, Theme, Impact) โดยใช้ Zero-Shot
|
| 49 |
"""
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
return {
|
| 52 |
"sentiment": "Neutral", "score": 0.5, "theme": "N/A",
|
| 53 |
-
"impact": "N/A", "explanation": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
try:
|
| 57 |
# 1. Analyze Sentiment
|
| 58 |
-
sentiment_result = self.sentiment_pipeline(text[:512])[0]
|
| 59 |
sentiment = sentiment_result['label'].capitalize()
|
| 60 |
score = sentiment_result['score']
|
| 61 |
|
|
@@ -76,8 +86,12 @@ class NewsAnalyzer:
|
|
| 76 |
# 4. Create an explanation
|
| 77 |
explanation = f"Classified as '{theme}' (Impact: {impact}) via zero-shot analysis."
|
| 78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
return {
|
| 80 |
-
"sentiment":
|
| 81 |
"score": score,
|
| 82 |
"theme": theme,
|
| 83 |
"impact": impact,
|
|
@@ -103,6 +117,4 @@ class NewsAnalyzer:
|
|
| 103 |
**news,
|
| 104 |
**sentiment_result
|
| 105 |
})
|
| 106 |
-
return results
|
| 107 |
-
|
| 108 |
-
# --- ไม่ต้องใช้ฟังก์ชัน _parse หรือ _fallback อีกต่อไป ---
|
|
|
|
| 3 |
import re
|
| 4 |
|
| 5 |
class NewsAnalyzer:
|
| 6 |
+
def __init__(self, model_name=None):
|
| 7 |
"""
|
| 8 |
Initialize news analyzer with fast, CPU-friendly Zero-Shot pipelines
|
| 9 |
"""
|
| 10 |
print("Initializing Zero-Shot News Analyzer...")
|
| 11 |
|
| 12 |
+
self.device = 0 if torch.cuda.is_available() else -1
|
| 13 |
print(f"Using device: {'cuda' if self.device == 0 else 'cpu'}")
|
| 14 |
|
| 15 |
try:
|
|
|
|
| 23 |
|
| 24 |
# Pipeline 2: For Zero-Shot Classification (Theme & Impact)
|
| 25 |
print("Loading Zero-Shot model...")
|
| 26 |
+
# --- MODIFIED: Corrected model name ---
|
| 27 |
self.classifier_pipeline = pipeline(
|
| 28 |
"zero-shot-classification",
|
| 29 |
+
model="facebook/bart-large-mnli", # นี่คือโมเดลมาตรฐานที่ถูกต้อง
|
| 30 |
device=self.device
|
| 31 |
)
|
| 32 |
+
# --- End of modification ---
|
| 33 |
|
| 34 |
print("Models loaded successfully!")
|
| 35 |
|
|
|
|
| 49 |
"""
|
| 50 |
วิเคราะห์ข่าว (Sentiment, Theme, Impact) โดยใช้ Zero-Shot
|
| 51 |
"""
|
| 52 |
+
# ตรวจสอบว่า pipeline โหลดสำเร็จหรือไม่
|
| 53 |
+
if not self.classifier_pipeline or not self.sentiment_pipeline:
|
| 54 |
+
print("Error: Pipelines are not loaded.")
|
| 55 |
return {
|
| 56 |
"sentiment": "Neutral", "score": 0.5, "theme": "N/A",
|
| 57 |
+
"impact": "N/A", "explanation": "Model loading failed"
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
if not text or len(text.strip()) == 0:
|
| 61 |
+
return {
|
| 62 |
+
"sentiment": "Neutral", "score": 0.5, "theme": "N/A",
|
| 63 |
+
"impact": "N/A", "explanation": "No text"
|
| 64 |
}
|
| 65 |
|
| 66 |
try:
|
| 67 |
# 1. Analyze Sentiment
|
| 68 |
+
sentiment_result = self.sentiment_pipeline(text[:512])[0]
|
| 69 |
sentiment = sentiment_result['label'].capitalize()
|
| 70 |
score = sentiment_result['score']
|
| 71 |
|
|
|
|
| 86 |
# 4. Create an explanation
|
| 87 |
explanation = f"Classified as '{theme}' (Impact: {impact}) via zero-shot analysis."
|
| 88 |
|
| 89 |
+
# แปลง Sentiment จาก 'Positive'/'Negative' ของ distilbert
|
| 90 |
+
# เป็น 'Positive'/'Negative'/'Neutral' (แต่โมเดลนี้ไม่มี Neutral)
|
| 91 |
+
final_sentiment = "Positive" if sentiment == "Positive" else "Negative"
|
| 92 |
+
|
| 93 |
return {
|
| 94 |
+
"sentiment": final_sentiment,
|
| 95 |
"score": score,
|
| 96 |
"theme": theme,
|
| 97 |
"impact": impact,
|
|
|
|
| 117 |
**news,
|
| 118 |
**sentiment_result
|
| 119 |
})
|
| 120 |
+
return results
|
|
|
|
|
|