File size: 5,995 Bytes
75db241
 
 
e5e18be
75db241
 
d35b5ab
 
 
b89c124
 
d35b5ab
 
b89c124
 
 
 
d35b5ab
 
75db241
4ff23b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8142326
4ff23b9
 
 
 
 
 
 
 
 
 
 
 
75db241
 
 
4ff23b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75db241
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import gradio as gr
from transformers import pipeline

model_name = "ZombitX64/Sentiment-01"
nlp = pipeline("sentiment-analysis", model=model_name)

label_map = {
    "LABEL_0": 0,
    "LABEL_1": 1,
    "LABEL_2": 2,
    "LABEL_3": 3
}
label_name_map = {
    "LABEL_0": "question",
    "LABEL_1": "negative",
    "LABEL_2": "neutral",
    "LABEL_3": "positive"
}

def analyze_text(text):
    # แยกประโยคโดยใช้ \n หรือจุด
    import re
    sentences = [s.strip() for s in re.split(r'[.\n]', text) if s.strip()]
    
    if not sentences:
        return "❗ กรุณาใส่ข้อความที่ต้องการวิเคราะห์"
    
    # สีและไอคอนสำหรับแต่ละ sentiment
    sentiment_style = {
        "positive": {"emoji": "😊", "color": "#4CAF50", "bg": "#E8F5E8"},
        "negative": {"emoji": "😔", "color": "#F44336", "bg": "#FFEBEE"},
        "neutral": {"emoji": "😐", "color": "#FF9800", "bg": "#FFF3E0"},
        "question": {"emoji": "❓", "color": "#2196F3", "bg": "#E3F2FD"}
    }
    
    results = []
    results.append("📊 **ผลการวิเคราะห์ความรู้สึก**\n" + "="*50 + "\n")
    
    for i, sentence in enumerate(sentences, 1):
        result = nlp(sentence)[0]
        label = result['label']
        score = result['score']
        code = label_map.get(label, -1)
        label_name = label_name_map.get(label, label)
        
        # ดึงสไตล์ตาม sentiment
        style = sentiment_style.get(label_name, {"emoji": "🔍", "color": "#666666", "bg": "#F5F5F5"})
        
        # สร้าง progress bar สำหรับความมั่นใจ
        bar_length = int(score * 20)  # 20 คือความยาวของ progress bar
        progress_bar = "█" * bar_length + "░" * (20 - bar_length)
        
        result_text = f"""
🔸 **ประโยคที่ {i}:** "{sentence}"

{style['emoji']} **ผลวิเคราะห์:** {label_name.upper()} (รหัส: {code})
📈 **ความมั่นใจ:** {score:.2f} ({score*100:.1f}%)
{progress_bar} {score:.2f}

{'─' * 60}
"""
        results.append(result_text)
    
    # เพิ่มสรุปผลรวม
    total_sentences = len(sentences)
    results.append(f"\n📋 **สรุป:** วิเคราะห์ทั้งหมด {total_sentences} ประโยค")
    
    return "\n".join(results)

demo = gr.Interface(
    fn=analyze_text,
    inputs=gr.Textbox(
        lines=6, 
        placeholder="💬 พิมพ์ข้อความหลายประโยคที่นี่...\n\n✨ คำแนะนำ:\n• แยกแต่ละประโยคด้วยจุด (.) หรือขึ้นบรรทัดใหม่\n• สามารถใส่ได้หลายประโยคพร้อมกัน\n• รองรับข้อความภาษาไทย",
        label="📝 ข้อความที่ต้องการวิเคราะห์"
    ),
    outputs=gr.Textbox(
        label="📊 ผลการวิเคราะห์ความรู้สึก",
        lines=15,
        show_copy_button=True
    ),
    title="🧠 AI วิเคราะห์ความรู้สึกภาษาไทย",
    description="""
    <div style="text-align: center; padding: 20px; background: linear-gradient(90deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin: 10px 0;">
        <h3>🚀 ระบบวิเคราะห์ความรู้สึกขั้นสูง</h3>
        <p>วิเคราะห์ความรู้สึกในข้อความภาษาไทยด้วย AI | รองรับการวิเคราะห์หลายประโยคพร้อมกัน</p>
    </div>
    
    <div style="display: flex; justify-content: space-around; margin: 20px 0; padding: 15px; background: #f8f9fa; border-radius: 8px;">
        <div style="text-align: center;">
            <div style="font-size: 24px;">😊</div>
            <strong>Positive</strong><br>
            <small>ความรู้สึกเชิงบวก</small>
        </div>
        <div style="text-align: center;">
            <div style="font-size: 24px;">😔</div>
            <strong>Negative</strong><br>
            <small>ความรู้สึกเชิงลบ</small>
        </div>
        <div style="text-align: center;">
            <div style="font-size: 24px;">😐</div>
            <strong>Neutral</strong><br>
            <small>ความรู้สึกเป็นกลาง</small>
        </div>
        <div style="text-align: center;">
            <div style="font-size: 24px;">❓</div>
            <strong>Question</strong><br>
            <small>ประโยคคำถาม</small>
        </div>
    </div>
    """,
    theme=gr.themes.Soft(
        primary_hue="blue",
        secondary_hue="purple",
        neutral_hue="gray"
    ),
    css="""
    .gradio-container {
        max-width: 900px !important;
        margin: auto !important;
    }
    .output-markdown {
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important;
    }
    """,
    examples=[
        ["วันนี้อากาศดีมาก ฉันรู้สึกมีความสุขมาก"],
        ["ฉันไม่ชอบอาหารนี้ รสชาติแปลกมาก"],
        ["วันนี้เป็นยังไง\nเรียนหนังสือกันไหม"],
        ["บริการดีมาก พนักงานใจดี\nแต่ของมีราคาแพงไปหน่อย\nโดยรวมแล้วพอใจ"]
    ]
)

demo.launch()