Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import re
|
@@ -40,6 +57,7 @@ def get_nlp(model_name: str):
|
|
40 |
|
41 |
# Enhanced label mapping with modern styling for dark blue theme
|
42 |
LABEL_MAPPINGS = {
|
|
|
43 |
"LABEL_0": {"code": 0, "name": "question", "emoji": "🤔", "color": "#60a5fa", "bg": "rgba(96, 165, 250, 0.2)", "description": "คำถาม"},
|
44 |
"LABEL_1": {"code": 1, "name": "very negative", "emoji": "😡", "color": "#ef4444", "bg": "rgba(239, 68, 68, 0.2)", "description": "เชิงลบมาก"},
|
45 |
"LABEL_2": {"code": 2, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
@@ -47,18 +65,21 @@ LABEL_MAPPINGS = {
|
|
47 |
"LABEL_4": {"code": 4, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
48 |
"LABEL_5": {"code": 5, "name": "very positive", "emoji": "🤩", "color": "#22c55e", "bg": "rgba(34, 197, 94, 0.2)", "description": "เชิงบวกมาก"},
|
49 |
|
|
|
|
|
50 |
"VERY_NEGATIVE": {"code": 1, "name": "very negative", "emoji": "😡", "color": "#ef4444", "bg": "rgba(239, 68, 68, 0.2)", "description": "เชิงลบมาก"},
|
51 |
"NEGATIVE": {"code": 2, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
52 |
"NEUTRAL": {"code": 3, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
53 |
"POSITIVE": {"code": 4, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
54 |
"VERY_POSITIVE": {"code": 5, "name": "very positive", "emoji": "🤩", "color": "#22c55e", "bg": "rgba(34, 197, 94, 0.2)", "description": "เชิงบวกมาก"},
|
55 |
-
|
56 |
-
|
57 |
-
"
|
58 |
-
"
|
59 |
-
|
60 |
-
"
|
61 |
-
"
|
|
|
62 |
}
|
63 |
|
64 |
def get_label_info(label: str) -> Dict:
|
@@ -137,7 +158,16 @@ def analyze_text(text: str, model_name: str) -> str:
|
|
137 |
</div>
|
138 |
"""]
|
139 |
|
140 |
-
sentiment_counts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
total_confidence = 0
|
142 |
sentence_results = []
|
143 |
|
@@ -151,6 +181,7 @@ def analyze_text(text: str, model_name: str) -> str:
|
|
151 |
label_info = get_label_info(label)
|
152 |
label_name = label_info["name"]
|
153 |
|
|
|
154 |
if label_name in sentiment_counts:
|
155 |
sentiment_counts[label_name] += 1
|
156 |
else:
|
@@ -224,24 +255,52 @@ def analyze_text(text: str, model_name: str) -> str:
|
|
224 |
|
225 |
# Create chart data for summary
|
226 |
chart_items = []
|
227 |
-
colors
|
228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
|
230 |
-
|
231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
percentage = (count / total_sentences) * 100
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
</div>
|
|
|
243 |
</div>
|
244 |
-
""
|
|
|
|
|
|
|
|
|
245 |
|
246 |
html_parts.append(f"""
|
247 |
<div style="padding: 24px; background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);">
|
@@ -446,9 +505,11 @@ with gr.Blocks(css=CUSTOM_CSS, theme=gr.themes.Base()) as demo:
|
|
446 |
<span>🗂️</span> คำอธิบายผลลัพธ์
|
447 |
</div>
|
448 |
<div class='legend-row'>
|
|
|
449 |
<div class='legend-item'><strong>😊 เชิงบวก</strong><br><small>Positive</small></div>
|
450 |
-
<div class='legend-item'><strong>😢 เชิงลบ</strong><br><small>Negative</small></div>
|
451 |
<div class='legend-item'><strong>😐 เป็นกลาง</strong><br><small>Neutral</small></div>
|
|
|
|
|
452 |
<div class='legend-item'><strong>🤔 คำถาม</strong><br><small>Question</small></div>
|
453 |
</div>
|
454 |
</div>
|
@@ -480,4 +541,5 @@ if __name__ == "__main__":
|
|
480 |
"docs_url": None,
|
481 |
"redoc_url": None,
|
482 |
}
|
483 |
-
)
|
|
|
|
1 |
+
นี่คือโค้ด Gradio ของคุณที่ผมได้ตรวจสอบและปรับปรุงให้แล้วครับ มีการแก้ไขข้อผิดพลาดเรื่อง `LABEL_MAPPINGS` ที่ซ้ำกันและเพิ่มคำอธิบายในส่วนของ Legend ให้ครบถ้วนตาม 5 ระดับ Sentiment และ "question" ครับ
|
2 |
+
|
3 |
+
-----
|
4 |
+
|
5 |
+
## สรุปการแก้ไขโค้ด
|
6 |
+
|
7 |
+
1. **แก้ไข `LABEL_MAPPINGS` ที่ซ้ำซ้อน:** โค้ดต้นฉบับมี `LABEL_MAPPINGS` ถูกประกาศซ้ำสองครั้ง และส่วนท้ายของ `LABEL_MAPPINGS` ยังมีรายการที่ซ้ำซ้อนและมี `code` ที่ไม่สอดคล้องกับแผน 6 Class (1 question + 5 sentiment levels) ที่เราคุยกันก่อนหน้านี้ ผมได้แก้ไขให้เหลือเพียงชุดเดียวที่ถูกต้องและสอดคล้องกัน
|
8 |
+
|
9 |
+
2. **อัปเดต `sentiment_counts`:** ในฟังก์ชัน `analyze_text` ได้เพิ่ม `very negative` และ `very positive` เข้าไปใน `sentiment_counts` เพื่อให้สามารถนับและแสดงผล Sentiment ทั้ง 5 ระดับได้อย่างถูกต้อง
|
10 |
+
|
11 |
+
3. **อัปเดต Legend ใน Gradio UI:** ได้เพิ่ม `very positive` และ `very negative` เข้าไปในส่วนของ Legend (คำอธิบายผลลัพธ์) ใน Gradio UI เพื่อให้ผู้ใช้เข้าใจผลลัพธ์ 5 ระดับได้อย่างชัดเจน
|
12 |
+
|
13 |
+
-----
|
14 |
+
|
15 |
+
## โค้ดที่แก้ไขแล้ว
|
16 |
+
|
17 |
+
```python
|
18 |
import gradio as gr
|
19 |
from transformers import pipeline
|
20 |
import re
|
|
|
57 |
|
58 |
# Enhanced label mapping with modern styling for dark blue theme
|
59 |
LABEL_MAPPINGS = {
|
60 |
+
# Mappings สำหรับ LABEL_X (จากโมเดล)
|
61 |
"LABEL_0": {"code": 0, "name": "question", "emoji": "🤔", "color": "#60a5fa", "bg": "rgba(96, 165, 250, 0.2)", "description": "คำถาม"},
|
62 |
"LABEL_1": {"code": 1, "name": "very negative", "emoji": "😡", "color": "#ef4444", "bg": "rgba(239, 68, 68, 0.2)", "description": "เชิงลบมาก"},
|
63 |
"LABEL_2": {"code": 2, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
|
|
65 |
"LABEL_4": {"code": 4, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
66 |
"LABEL_5": {"code": 5, "name": "very positive", "emoji": "🤩", "color": "#22c55e", "bg": "rgba(34, 197, 94, 0.2)", "description": "เชิงบวกมาก"},
|
67 |
|
68 |
+
# Mappings สำหรับชื่อเต็ม (ใช้สำหรับแสดงผลหรืออ้างอิง)
|
69 |
+
"QUESTION": {"code": 0, "name": "question", "emoji": "🤔", "color": "#60a5fa", "bg": "rgba(96, 165, 250, 0.2)", "description": "คำถาม"},
|
70 |
"VERY_NEGATIVE": {"code": 1, "name": "very negative", "emoji": "😡", "color": "#ef4444", "bg": "rgba(239, 68, 68, 0.2)", "description": "เชิงลบมาก"},
|
71 |
"NEGATIVE": {"code": 2, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
72 |
"NEUTRAL": {"code": 3, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
73 |
"POSITIVE": {"code": 4, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
74 |
"VERY_POSITIVE": {"code": 5, "name": "very positive", "emoji": "🤩", "color": "#22c55e", "bg": "rgba(34, 197, 94, 0.2)", "description": "เชิงบวกมาก"},
|
75 |
+
|
76 |
+
# Mappings สำหรับรหัสตัวเลข (ถ้าจำเป็นต้องแปลงจาก 0, 1, 2, ... เป็นชื่อ)
|
77 |
+
"0": {"code": 0, "name": "question", "emoji": "🤔", "color": "#60a5fa", "bg": "rgba(96, 165, 250, 0.2)", "description": "คำถาม"},
|
78 |
+
"1": {"code": 1, "name": "very negative", "emoji": "😡", "color": "#ef4444", "bg": "rgba(239, 68, 68, 0.2)", "description": "เชิงลบมาก"},
|
79 |
+
"2": {"code": 2, "name": "negative", "emoji": "😢", "color": "#f87171", "bg": "rgba(248, 113, 113, 0.2)", "description": "เชิงลบ"},
|
80 |
+
"3": {"code": 3, "name": "neutral", "emoji": "😐", "color": "#facc15", "bg": "rgba(250, 204, 21, 0.2)", "description": "เป็นกลาง"},
|
81 |
+
"4": {"code": 4, "name": "positive", "emoji": "😊", "color": "#34d399", "bg": "rgba(52, 211, 153, 0.2)", "description": "เชิงบวก"},
|
82 |
+
"5": {"code": 5, "name": "very positive", "emoji": "🤩", "color": "#22c55e", "bg": "rgba(34, 197, 94, 0.2)", "description": "เชิงบวกมาก"},
|
83 |
}
|
84 |
|
85 |
def get_label_info(label: str) -> Dict:
|
|
|
158 |
</div>
|
159 |
"""]
|
160 |
|
161 |
+
# Updated sentiment_counts to include all 5 sentiment levels plus question
|
162 |
+
sentiment_counts = {
|
163 |
+
"question": 0,
|
164 |
+
"very negative": 0,
|
165 |
+
"negative": 0,
|
166 |
+
"neutral": 0,
|
167 |
+
"positive": 0,
|
168 |
+
"very positive": 0,
|
169 |
+
"other": 0
|
170 |
+
}
|
171 |
total_confidence = 0
|
172 |
sentence_results = []
|
173 |
|
|
|
181 |
label_info = get_label_info(label)
|
182 |
label_name = label_info["name"]
|
183 |
|
184 |
+
# Increment count for the corresponding sentiment name
|
185 |
if label_name in sentiment_counts:
|
186 |
sentiment_counts[label_name] += 1
|
187 |
else:
|
|
|
255 |
|
256 |
# Create chart data for summary
|
257 |
chart_items = []
|
258 |
+
# Ensure colors and emojis include all 6 categories (question + 5 sentiments)
|
259 |
+
colors = {
|
260 |
+
"question": "#60a5fa",
|
261 |
+
"very negative": "#ef4444",
|
262 |
+
"negative": "#f87171",
|
263 |
+
"neutral": "#facc15",
|
264 |
+
"positive": "#34d399",
|
265 |
+
"very positive": "#22c55e",
|
266 |
+
"other": "#64748b"
|
267 |
+
}
|
268 |
+
emojis = {
|
269 |
+
"question": "🤔",
|
270 |
+
"very negative": "😡",
|
271 |
+
"negative": "😢",
|
272 |
+
"neutral": "😐",
|
273 |
+
"positive": "😊",
|
274 |
+
"very positive": "🤩",
|
275 |
+
"other": "🔍"
|
276 |
+
}
|
277 |
|
278 |
+
# Iterate through all possible sentiment types to ensure they are displayed, even if count is 0
|
279 |
+
# Or, iterate through sentiment_counts.items() if you only want to display sentiments found in the text
|
280 |
+
for sentiment_name, count in sentiment_counts.items():
|
281 |
+
if sentiment_name == "other" and count == 0: # Don't show 'other' if no unknown labels
|
282 |
+
continue
|
283 |
+
if total_sentences == 0:
|
284 |
+
percentage = 0
|
285 |
+
else:
|
286 |
percentage = (count / total_sentences) * 100
|
287 |
+
|
288 |
+
# Get description from LABEL_MAPPINGS using the sentiment name
|
289 |
+
# We can find it by iterating through LABEL_MAPPINGS values
|
290 |
+
description = next((info['description'] for info in LABEL_MAPPINGS.values() if info['name'] == sentiment_name), sentiment_name)
|
291 |
+
|
292 |
+
chart_items.append(f"""
|
293 |
+
<div style="display: flex; align-items: center; gap: 12px; padding: 12px; background: rgba(59, 130, 246, 0.1); border-radius: 8px;">
|
294 |
+
<span style="font-size: 24px;">{emojis.get(sentiment_name, '🔍')}</span>
|
295 |
+
<div style="flex: 1;">
|
296 |
+
<div style="font-weight: 600; color: #f8fafc; text-transform: capitalize;">{description}</div>
|
297 |
+
<div style="color: #94a3b8; font-size: 14px;">{count} ประโยค ({percentage:.1f}%)</div>
|
298 |
</div>
|
299 |
+
<div style="width: 60px; height: 6px; background: #334155; border-radius: 3px; overflow: hidden;">
|
300 |
+
<div style="width: {percentage}%; height: 100%; background: {colors.get(sentiment_name, '#64748b')}; transition: all 0.3s ease;"></div>
|
301 |
+
</div>
|
302 |
+
</div>
|
303 |
+
""")
|
304 |
|
305 |
html_parts.append(f"""
|
306 |
<div style="padding: 24px; background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);">
|
|
|
505 |
<span>🗂️</span> คำอธิบายผลลัพธ์
|
506 |
</div>
|
507 |
<div class='legend-row'>
|
508 |
+
<div class='legend-item'><strong>🤩 เชิงบวกมาก</strong><br><small>Very Positive</small></div>
|
509 |
<div class='legend-item'><strong>😊 เชิงบวก</strong><br><small>Positive</small></div>
|
|
|
510 |
<div class='legend-item'><strong>😐 เป็นกลาง</strong><br><small>Neutral</small></div>
|
511 |
+
<div class='legend-item'><strong>😢 เชิงลบ</strong><br><small>Negative</small></div>
|
512 |
+
<div class='legend-item'><strong>😡 เชิงลบมาก</strong><br><small>Very Negative</small></div>
|
513 |
<div class='legend-item'><strong>🤔 คำถาม</strong><br><small>Question</small></div>
|
514 |
</div>
|
515 |
</div>
|
|
|
541 |
"docs_url": None,
|
542 |
"redoc_url": None,
|
543 |
}
|
544 |
+
)
|
545 |
+
```
|