rafhiromadoni commited on
Commit
d4826dc
·
verified ·
1 Parent(s): 11b763d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +72 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ transformers
2
+ torch