rafhiromadoni commited on
Commit
1841ed2
·
verified ·
1 Parent(s): d4826dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -71
app.py CHANGED
@@ -1,72 +1,77 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
-
4
- # 1. LOAD MODEL NLP UNTUK SENTIMEN
5
- print("🚀 Sedang memuat model NLP (RoBERTa)...")
6
- # top_k=None memastikan model mengembalikan skor probabilitas untuk ketiga kelas (Positif, Netral, Negatif)
7
- sentiment_pipeline = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment-latest", top_k=None)
8
-
9
- def analyze_sentiment(text):
10
- if not text.strip():
11
- return "⚠️ Teks kosong", {"😐 Netral": 1.0}
12
-
13
- try:
14
- # AI memproses kalimat
15
- results = sentiment_pipeline(text)[0]
16
-
17
- # Mapping label bahasa Inggris ke visualisasi UI bahasa Indonesia
18
- label_mapping = {
19
- "positive": "😊 Positif",
20
- "neutral": "😐 Netral",
21
- "negative": "😡 Negatif"
22
- }
23
-
24
- # Menyusun dictionary confidences untuk UI Progress Bar Gradio
25
- confidences = {label_mapping[res['label']]: res['score'] for res in results}
26
-
27
- # Mencari sentimen dengan skor tertinggi
28
- top_label = max(confidences, key=confidences.get)
29
-
30
- return f"### Kesimpulan: Teks bersentimen **{top_label}**", confidences
31
-
32
- except Exception as e:
33
- return f"⚠️ Terjadi kesalahan: {str(e)}", {}
34
-
35
- # 2. ANTARMUKA GRADIO
36
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
37
- gr.Markdown("""
38
- <h1 style='text-align: center;'>💬 AI Sentiment Monitoring System</h1>
39
- <p style='text-align: center;'>Sistem NLP cerdas untuk mendeteksi emosi dan opini publik dari teks atau ulasan secara <i>real-time</i>.</p>
40
- """)
41
-
42
- with gr.Row():
43
- with gr.Column(scale=2):
44
- # Input teks
45
- inp_text = gr.Textbox(
46
- label="📝 Masukkan Teks / Opini (Disarankan Bahasa Inggris)",
47
- placeholder="Contoh: The new product feature is absolutely amazing, but the customer service was terrible!",
48
- lines=4
49
- )
50
- btn = gr.Button("🔍 Analisis Sentimen", variant="primary")
51
-
52
- with gr.Column(scale=1):
53
- # Output hasil
54
- out_kesimpulan = gr.Markdown(label="Kesimpulan AI")
55
- out_label = gr.Label(label="📊 Distribusi Emosi (Probabilitas)")
56
-
57
- # Menambahkan beberapa contoh agar pengunjung GitHub/HF Anda bisa langsung mencoba
58
- gr.Examples(
59
- examples=[
60
- "I absolutely love the new design, it's so intuitive and fast!",
61
- "The delivery was late and the packaging was completely ruined. Very disappointed.",
62
- "I bought this phone yesterday. The screen size is 6 inches and it comes with a charger.",
63
- "It's okay I guess. Not the best, but definitely not the worst."
64
- ],
65
- inputs=inp_text
66
- )
67
-
68
- # Hubungkan tombol
69
- btn.click(fn=analyze_sentiment, inputs=inp_text, outputs=[out_kesimpulan, out_label])
70
-
71
- if __name__ == "__main__":
 
 
 
 
 
72
  demo.launch()
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # 1. LOAD MODEL NLP MULTILINGUAL
5
+ print("🚀 Sedang memuat model NLP (Multilingual)...")
6
+ # Menggunakan model DistilBERT Multilingual yang mendukung Bahasa Indonesia, Inggris, dan bahasa lainnya
7
+ sentiment_pipeline = pipeline(
8
+ "text-classification",
9
+ model="lxyuan/distilbert-base-multilingual-cased-sentiments-student",
10
+ top_k=None
11
+ )
12
+
13
+ def analyze_sentiment(text):
14
+ if not text.strip():
15
+ return "⚠️ Teks kosong", {"😐 Netral": 1.0}
16
+
17
+ try:
18
+ # AI memproses kalimat
19
+ results = sentiment_pipeline(text)[0]
20
+
21
+ # Mapping label dari model (positive/neutral/negative) ke visualisasi UI bahasa Indonesia
22
+ label_mapping = {
23
+ "positive": "😊 Positif",
24
+ "neutral": "😐 Netral",
25
+ "negative": "😡 Negatif"
26
+ }
27
+
28
+ # Menyusun dictionary confidences untuk UI Progress Bar Gradio
29
+ # Jika model mengeluarkan label lain, kita gunakan label aslinya
30
+ confidences = {label_mapping.get(res['label'], res['label'].capitalize()): res['score'] for res in results}
31
+
32
+ # Mencari sentimen dengan skor tertinggi
33
+ top_label = max(confidences, key=confidences.get)
34
+
35
+ return f"### Kesimpulan: Teks bersentimen **{top_label}**", confidences
36
+
37
+ except Exception as e:
38
+ return f"⚠️ Terjadi kesalahan: {str(e)}", {}
39
+
40
+ # 2. ANTARMUKA GRADIO
41
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
42
+ gr.Markdown("""
43
+ <h1 style='text-align: center;'>💬 AI Sentiment Monitoring System</h1>
44
+ <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>
45
+ """)
46
+
47
+ with gr.Row():
48
+ with gr.Column(scale=2):
49
+ # Input teks sudah diperbarui untuk mengundang dua bahasa
50
+ inp_text = gr.Textbox(
51
+ label="📝 Masukkan Teks / Opini (ID / EN)",
52
+ placeholder="Contoh: Fitur barunya sangat keren dan cepat, tapi sayang CS-nya sangat lambat membalas!",
53
+ lines=4
54
+ )
55
+ btn = gr.Button("🔍 Analisis Sentimen", variant="primary")
56
+
57
+ with gr.Column(scale=1):
58
+ # Output hasil
59
+ out_kesimpulan = gr.Markdown(label="Kesimpulan AI")
60
+ out_label = gr.Label(label="📊 Distribusi Emosi (Probabilitas)")
61
+
62
+ # Menambahkan contoh bilingual (campuran bahasa) agar pengunjung bisa langsung menguji kehebatannya
63
+ gr.Examples(
64
+ examples=[
65
+ "Pelayanannya sangat buruk, pesanan saya telat 3 hari dan kurirnya tidak ramah. Kecewa berat!",
66
+ "I absolutely love the new design, it's so intuitive and fast!",
67
+ "Harga produk ini lumayan mahal, tapi kualitasnya standar saja. Not bad lah.",
68
+ "The delivery was late but the product is okay."
69
+ ],
70
+ inputs=inp_text
71
+ )
72
+
73
+ # Hubungkan tombol
74
+ btn.click(fn=analyze_sentiment, inputs=inp_text, outputs=[out_kesimpulan, out_label])
75
+
76
+ if __name__ == "__main__":
77
  demo.launch()