File size: 3,161 Bytes
1841ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4826dc
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
import gradio as gr
from transformers import pipeline

# 1. LOAD MODEL NLP MULTILINGUAL
print("🚀 Sedang memuat model NLP (Multilingual)...")
# Menggunakan model DistilBERT Multilingual yang mendukung Bahasa Indonesia, Inggris, dan bahasa lainnya
sentiment_pipeline = pipeline(
    "text-classification", 
    model="lxyuan/distilbert-base-multilingual-cased-sentiments-student", 
    top_k=None
)

def analyze_sentiment(text):
    if not text.strip():
        return "⚠️ Teks kosong", {"😐 Netral": 1.0}
    
    try:
        # AI memproses kalimat
        results = sentiment_pipeline(text)[0]
        
        # Mapping label dari model (positive/neutral/negative) ke visualisasi UI bahasa Indonesia
        label_mapping = {
            "positive": "😊 Positif",
            "neutral": "😐 Netral",
            "negative": "😡 Negatif"
        }
        
        # Menyusun dictionary confidences untuk UI Progress Bar Gradio
        # Jika model mengeluarkan label lain, kita gunakan label aslinya
        confidences = {label_mapping.get(res['label'], res['label'].capitalize()): res['score'] for res in results}
        
        # Mencari sentimen dengan skor tertinggi
        top_label = max(confidences, key=confidences.get)
        
        return f"### Kesimpulan: Teks bersentimen **{top_label}**", confidences
        
    except Exception as e:
        return f"⚠️ Terjadi kesalahan: {str(e)}", {}

# 2. ANTARMUKA GRADIO
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    <h1 style='text-align: center;'>💬 AI Sentiment Monitoring System</h1>
    <p style='text-align: center;'>Sistem NLP cerdas untuk mendeteksi emosi dan opini publik dari teks atau ulasan dalam <b>Bahasa Indonesia</b> maupun <b>Bahasa Inggris</b>.</p>
    """)
    
    with gr.Row():
        with gr.Column(scale=2):
            # Input teks sudah diperbarui untuk mengundang dua bahasa
            inp_text = gr.Textbox(
                label="📝 Masukkan Teks / Opini (ID / EN)", 
                placeholder="Contoh: Fitur barunya sangat keren dan cepat, tapi sayang CS-nya sangat lambat membalas!",
                lines=4
            )
            btn = gr.Button("🔍 Analisis Sentimen", variant="primary")
            
        with gr.Column(scale=1):
            # Output hasil
            out_kesimpulan = gr.Markdown(label="Kesimpulan AI")
            out_label = gr.Label(label="📊 Distribusi Emosi (Probabilitas)")
            
    # Menambahkan contoh bilingual (campuran bahasa) agar pengunjung bisa langsung menguji kehebatannya
    gr.Examples(
        examples=[
            "Pelayanannya sangat buruk, pesanan saya telat 3 hari dan kurirnya tidak ramah. Kecewa berat!",
            "I absolutely love the new design, it's so intuitive and fast!",
            "Harga produk ini lumayan mahal, tapi kualitasnya standar saja. Not bad lah.",
            "The delivery was late but the product is okay."
        ],
        inputs=inp_text
    )
            
    # Hubungkan tombol
    btn.click(fn=analyze_sentiment, inputs=inp_text, outputs=[out_kesimpulan, out_label])

if __name__ == "__main__":
    demo.launch()