Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -36,36 +36,113 @@ def get_nlp(model_name: str):
|
|
36 |
logger.error(f"Error loading model {model_name}: {e}")
|
37 |
raise gr.Error(f"ไม่สามารถโหลดโมเดล {model_name} ได้: {str(e)}")
|
38 |
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
"
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
"
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
"
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
}
|
58 |
|
59 |
-
def get_label_info(label: str) -> Dict:
|
60 |
-
"""Get label information with fallback for unknown labels"""
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
"code": -1,
|
63 |
"name": label.lower(),
|
64 |
"emoji": "🔍",
|
65 |
"color": "#64748b",
|
66 |
"bg": "rgba(100, 116, 139, 0.2)",
|
67 |
-
"description": "ไม่ทราบ"
|
68 |
-
}
|
69 |
|
70 |
def split_sentences(text: str) -> List[str]:
|
71 |
"""Enhanced sentence splitting with better Thai support"""
|
@@ -122,13 +199,13 @@ def analyze_text(text: str, model_name: str) -> str:
|
|
122 |
"""
|
123 |
|
124 |
# Header
|
125 |
-
html_parts = ["""
|
126 |
<div style="background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%); color: #f8fafc; padding: 24px; border-radius: 16px 16px 0 0; margin-bottom: 0;">
|
127 |
<h2 style="margin: 0; font-size: 24px; font-weight: 700; display: flex; align-items: center; gap: 12px;">
|
128 |
<span style="font-size: 28px;">🧠</span>
|
129 |
ผลการวิเคราะห์ความรู้สึก
|
130 |
</h2>
|
131 |
-
<p style="margin: 8px 0 0 0; opacity: 0.9; font-size: 14px;">โมเดล:
|
132 |
</div>
|
133 |
"""]
|
134 |
|
@@ -143,7 +220,7 @@ def analyze_text(text: str, model_name: str) -> str:
|
|
143 |
label = result['label']
|
144 |
score = result['score']
|
145 |
|
146 |
-
label_info = get_label_info(label)
|
147 |
label_name = label_info["name"]
|
148 |
|
149 |
if label_name in sentiment_counts:
|
@@ -158,7 +235,8 @@ def analyze_text(text: str, model_name: str) -> str:
|
|
158 |
'sentence': sentence,
|
159 |
'label_info': label_info,
|
160 |
'score': score,
|
161 |
-
'index': i
|
|
|
162 |
})
|
163 |
|
164 |
except Exception as e:
|
@@ -201,6 +279,9 @@ def analyze_text(text: str, model_name: str) -> str:
|
|
201 |
<span style="background: {label_info['color']}; color: #f8fafc; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: 600; text-transform: uppercase;">
|
202 |
{label_info['description']}
|
203 |
</span>
|
|
|
|
|
|
|
204 |
<span style="color: #94a3b8; font-size: 14px;">ประโยคที่ {result['index']}</span>
|
205 |
</div>
|
206 |
<p style="color: #f8fafc; margin: 0 0 12px 0; font-size: 16px; line-height: 1.5;">
|
|
|
36 |
logger.error(f"Error loading model {model_name}: {e}")
|
37 |
raise gr.Error(f"ไม่สามารถโหลดโมเดล {model_name} ได้: {str(e)}")
|
38 |
|
39 |
+
# Model-specific label mappings
|
40 |
+
MODEL_LABEL_MAPPINGS = {
|
41 |
+
# MultiSent E5 Pro - 4 classes (question, negative, neutral, positive)
|
42 |
+
"ZombitX64/MultiSent-E5-Pro": {
|
43 |
+
"LABEL_0": {"code": 0, "name": "question", "emoji": "🤔", "color": "#60a5fa", "bg": "rgba(96, 165, 250, 0.2)", "description": "คำถาม"},
|
44 |
+
"LABEL_1": {"code": 1, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
45 |
+
"LABEL_2": {"code": 2, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
46 |
+
"LABEL_3": {"code": 3, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
47 |
+
},
|
48 |
|
49 |
+
# Thai Sentiment E5 - 3 classes (negative, neutral, positive)
|
50 |
+
"ZombitX64/Thai-sentiment-e5": {
|
51 |
+
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
52 |
+
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
53 |
+
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
54 |
+
},
|
55 |
+
|
56 |
+
# WangchanBERTa - usually neg/neu/pos
|
57 |
+
"poom-sci/WangchanBERTa-finetuned-sentiment": {
|
58 |
+
"neg": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
59 |
+
"neu": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
60 |
+
"pos": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
61 |
+
},
|
62 |
+
|
63 |
+
# Sandbox Thai - 3 classes
|
64 |
+
"SandboxBhh/sentiment-thai-text-model": {
|
65 |
+
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
66 |
+
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
67 |
+
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
68 |
+
},
|
69 |
+
|
70 |
+
# MultiSent E5 - 3 classes
|
71 |
+
"ZombitX64/MultiSent-E5": {
|
72 |
+
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
73 |
+
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
74 |
+
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
75 |
+
},
|
76 |
+
|
77 |
+
# WangchanBERTa Hyperopt
|
78 |
+
"Thaweewat/wangchanberta-hyperopt-sentiment-01": {
|
79 |
+
"neg": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
80 |
+
"neu": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
81 |
+
"pos": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
82 |
+
},
|
83 |
+
|
84 |
+
# Twitter XLM-RoBERTa - NEGATIVE/NEUTRAL/POSITIVE
|
85 |
+
"cardiffnlp/twitter-xlm-roberta-base-sentiment": {
|
86 |
+
"NEGATIVE": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
87 |
+
"NEUTRAL": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
88 |
+
"POSITIVE": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
89 |
+
},
|
90 |
+
|
91 |
+
# Wangchan Mobile
|
92 |
+
"phoner45/wangchan-sentiment-thai-text-model": {
|
93 |
+
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
94 |
+
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
95 |
+
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
96 |
+
},
|
97 |
+
|
98 |
+
# ZombitX64 Sentiment models - 3 classes
|
99 |
+
"ZombitX64/Sentiment-01": {
|
100 |
+
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
101 |
+
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
102 |
+
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
103 |
+
},
|
104 |
|
105 |
+
"ZombitX64/Sentiment-02": {
|
106 |
+
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
107 |
+
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
108 |
+
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
109 |
+
},
|
110 |
+
|
111 |
+
"ZombitX64/Sentiment-03": {
|
112 |
+
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
113 |
+
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
114 |
+
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
115 |
+
},
|
116 |
+
|
117 |
+
"ZombitX64/sentiment-103": {
|
118 |
+
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
119 |
+
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
120 |
+
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
121 |
+
},
|
122 |
+
|
123 |
+
"ZombitX64/sentimentSumdata-v1": {
|
124 |
+
"LABEL_0": {"code": 0, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
125 |
+
"LABEL_1": {"code": 1, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
126 |
+
"LABEL_2": {"code": 2, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
127 |
+
},
|
128 |
}
|
129 |
|
130 |
+
def get_label_info(label: str, model_name: str) -> Dict:
|
131 |
+
"""Get label information for specific model with fallback for unknown labels"""
|
132 |
+
model_mappings = MODEL_LABEL_MAPPINGS.get(model_name, {})
|
133 |
+
|
134 |
+
if label in model_mappings:
|
135 |
+
return model_mappings[label]
|
136 |
+
|
137 |
+
# Fallback for unknown labels
|
138 |
+
return {
|
139 |
"code": -1,
|
140 |
"name": label.lower(),
|
141 |
"emoji": "🔍",
|
142 |
"color": "#64748b",
|
143 |
"bg": "rgba(100, 116, 139, 0.2)",
|
144 |
+
"description": f"ไม่ทราบ ({label})"
|
145 |
+
}
|
146 |
|
147 |
def split_sentences(text: str) -> List[str]:
|
148 |
"""Enhanced sentence splitting with better Thai support"""
|
|
|
199 |
"""
|
200 |
|
201 |
# Header
|
202 |
+
html_parts = [f"""
|
203 |
<div style="background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%); color: #f8fafc; padding: 24px; border-radius: 16px 16px 0 0; margin-bottom: 0;">
|
204 |
<h2 style="margin: 0; font-size: 24px; font-weight: 700; display: flex; align-items: center; gap: 12px;">
|
205 |
<span style="font-size: 28px;">🧠</span>
|
206 |
ผลการวิเคราะห์ความรู้สึก
|
207 |
</h2>
|
208 |
+
<p style="margin: 8px 0 0 0; opacity: 0.9; font-size: 14px;">โมเดล: {model_name.split('/')[-1]}</p>
|
209 |
</div>
|
210 |
"""]
|
211 |
|
|
|
220 |
label = result['label']
|
221 |
score = result['score']
|
222 |
|
223 |
+
label_info = get_label_info(label, model_name)
|
224 |
label_name = label_info["name"]
|
225 |
|
226 |
if label_name in sentiment_counts:
|
|
|
235 |
'sentence': sentence,
|
236 |
'label_info': label_info,
|
237 |
'score': score,
|
238 |
+
'index': i,
|
239 |
+
'original_label': label
|
240 |
})
|
241 |
|
242 |
except Exception as e:
|
|
|
279 |
<span style="background: {label_info['color']}; color: #f8fafc; padding: 4px 12px; border-radius: 20px; font-size: 12px; font-weight: 600; text-transform: uppercase;">
|
280 |
{label_info['description']}
|
281 |
</span>
|
282 |
+
<span style="color: #94a3b8; font-size: 12px; background: #1e293b; padding: 2px 8px; border-radius: 12px;">
|
283 |
+
{result['original_label']}
|
284 |
+
</span>
|
285 |
<span style="color: #94a3b8; font-size: 14px;">ประโยคที่ {result['index']}</span>
|
286 |
</div>
|
287 |
<p style="color: #f8fafc; margin: 0 0 12px 0; font-size: 16px; line-height: 1.5;">
|