Spaces:
Running
Running
Nattapong Tapachoom
commited on
Commit
·
4ff23b9
1
Parent(s):
b89c124
Enhance sentiment analysis functionality with multi-sentence support and improved UI elements
Browse files
app.py
CHANGED
|
@@ -18,18 +18,117 @@ label_name_map = {
|
|
| 18 |
}
|
| 19 |
|
| 20 |
def analyze_text(text):
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
demo = gr.Interface(
|
| 29 |
fn=analyze_text,
|
| 30 |
-
inputs=gr.Textbox(
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
)
|
| 34 |
|
| 35 |
demo.launch()
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
def analyze_text(text):
|
| 21 |
+
# แยกประโยคโดยใช้ \n หรือจุด
|
| 22 |
+
import re
|
| 23 |
+
sentences = [s.strip() for s in re.split(r'[.\n]', text) if s.strip()]
|
| 24 |
+
|
| 25 |
+
if not sentences:
|
| 26 |
+
return "❗ กรุณาใส่ข้อความที่ต้องการวิเคราะห์"
|
| 27 |
+
|
| 28 |
+
# สีและไอคอนสำหรับแต่ละ sentiment
|
| 29 |
+
sentiment_style = {
|
| 30 |
+
"positive": {"emoji": "😊", "color": "#4CAF50", "bg": "#E8F5E8"},
|
| 31 |
+
"negative": {"emoji": "😔", "color": "#F44336", "bg": "#FFEBEE"},
|
| 32 |
+
"neutral": {"emoji": "😐", "color": "#FF9800", "bg": "#FFF3E0"},
|
| 33 |
+
"question": {"emoji": "❓", "color": "#2196F3", "bg": "#E3F2FD"}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
results = []
|
| 37 |
+
results.append("📊 **ผลการวิเคราะห์ความรู้สึก**\n" + "="*50 + "\n")
|
| 38 |
+
|
| 39 |
+
for i, sentence in enumerate(sentences, 1):
|
| 40 |
+
result = nlp(sentence)[0]
|
| 41 |
+
label = result['label']
|
| 42 |
+
score = result['score']
|
| 43 |
+
code = label_map.get(label, -1)
|
| 44 |
+
label_name = label_name_map.get(label, label)
|
| 45 |
+
|
| 46 |
+
# ดึงสไตล์ตาม sentiment
|
| 47 |
+
style = sentiment_style.get(label_name, {"emoji": "🔍", "color": "#666666", "bg": "#F5F5F5"})
|
| 48 |
+
|
| 49 |
+
# สร้าง progress bar สำหรับความมั่นใจ
|
| 50 |
+
bar_length = int(score * 20) # 20 คือความยาวของ progress bar
|
| 51 |
+
progress_bar = "█" * bar_length + "░" * (20 - bar_length)
|
| 52 |
+
|
| 53 |
+
result_text = f"""
|
| 54 |
+
🔸 **ประโยคที่ {i}:** "{sentence}"
|
| 55 |
+
|
| 56 |
+
{style['emoji']} **ผลวิเคราะห์:** {label_name.upper()} (รหัス: {code})
|
| 57 |
+
📈 **ความมั่นใจ:** {score:.2f} ({score*100:.1f}%)
|
| 58 |
+
{progress_bar} {score:.2f}
|
| 59 |
+
|
| 60 |
+
{'─' * 60}
|
| 61 |
+
"""
|
| 62 |
+
results.append(result_text)
|
| 63 |
+
|
| 64 |
+
# เพิ่มสรุปผลรวม
|
| 65 |
+
total_sentences = len(sentences)
|
| 66 |
+
results.append(f"\n📋 **สรุป:** วิเคราะห์ทั้งหมด {total_sentences} ประโยค")
|
| 67 |
+
|
| 68 |
+
return "\n".join(results)
|
| 69 |
|
| 70 |
demo = gr.Interface(
|
| 71 |
fn=analyze_text,
|
| 72 |
+
inputs=gr.Textbox(
|
| 73 |
+
lines=6,
|
| 74 |
+
placeholder="💬 พิมพ์ข้อความหลายประโยคที่นี่...\n\n✨ คำแนะนำ:\n• แยกแต่ละประโยคด้วยจุด (.) หรือขึ้นบรรทัดใหม่\n• สามารถใส่ได้หลายประโยคพร้อมกัน\n• รองรับข้อความภาษาไทย",
|
| 75 |
+
label="📝 ข้อความที่ต้องการวิเคราะห์"
|
| 76 |
+
),
|
| 77 |
+
outputs=gr.Textbox(
|
| 78 |
+
label="📊 ผลการวิเคราะห์ความรู้สึก",
|
| 79 |
+
lines=15,
|
| 80 |
+
show_copy_button=True
|
| 81 |
+
),
|
| 82 |
+
title="🧠 AI วิเคราะห์ความรู้สึกภาษาไทย",
|
| 83 |
+
description="""
|
| 84 |
+
<div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin: 10px 0;">
|
| 85 |
+
<h3>🚀 ระบบวิเคราะห์ความรู้สึกขั้นสูง</h3>
|
| 86 |
+
<p>วิเคราะห์ความรู้สึกในข้อความภาษาไทยด้วย AI | รองรับการวิเคราะห์หลายประโยคพร้อมกัน</p>
|
| 87 |
+
</div>
|
| 88 |
+
|
| 89 |
+
<div style="display: flex; justify-content: space-around; margin: 20px 0; padding: 15px; background: #f8f9fa; border-radius: 8px;">
|
| 90 |
+
<div style="text-align: center;">
|
| 91 |
+
<div style="font-size: 24px;">😊</div>
|
| 92 |
+
<strong>Positive</strong><br>
|
| 93 |
+
<small>ความรู้สึกเชิงบวก</small>
|
| 94 |
+
</div>
|
| 95 |
+
<div style="text-align: center;">
|
| 96 |
+
<div style="font-size: 24px;">😔</div>
|
| 97 |
+
<strong>Negative</strong><br>
|
| 98 |
+
<small>ความรู้สึกเชิงลบ</small>
|
| 99 |
+
</div>
|
| 100 |
+
<div style="text-align: center;">
|
| 101 |
+
<div style="font-size: 24px;">😐</div>
|
| 102 |
+
<strong>Neutral</strong><br>
|
| 103 |
+
<small>ความรู้สึกเป็นกลาง</small>
|
| 104 |
+
</div>
|
| 105 |
+
<div style="text-align: center;">
|
| 106 |
+
<div style="font-size: 24px;">❓</div>
|
| 107 |
+
<strong>Question</strong><br>
|
| 108 |
+
<small>ประโยคคำถาม</small>
|
| 109 |
+
</div>
|
| 110 |
+
</div>
|
| 111 |
+
""",
|
| 112 |
+
theme=gr.themes.Soft(
|
| 113 |
+
primary_hue="blue",
|
| 114 |
+
secondary_hue="purple",
|
| 115 |
+
neutral_hue="gray"
|
| 116 |
+
),
|
| 117 |
+
css="""
|
| 118 |
+
.gradio-container {
|
| 119 |
+
max-width: 900px !important;
|
| 120 |
+
margin: auto !important;
|
| 121 |
+
}
|
| 122 |
+
.output-markdown {
|
| 123 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
|
| 124 |
+
}
|
| 125 |
+
""",
|
| 126 |
+
examples=[
|
| 127 |
+
["วันนี้อากาศดีมาก ฉันรู้สึกมีความสุขมาก"],
|
| 128 |
+
["ฉันไม่ชอบอาหารนี้ รสชาติแปลกมาก"],
|
| 129 |
+
["วันนี้เป็นยังไง\nเรียนหนังสือกันไหม"],
|
| 130 |
+
["บริการดีมาก พนักงานใจดี\nแต่ของมีราคาแพงไปหน่อย\nโดยรวมแล้วพอใจ"]
|
| 131 |
+
]
|
| 132 |
)
|
| 133 |
|
| 134 |
demo.launch()
|