| import gradio as gr |
| from transformers import pipeline |
|
|
| |
| print("🚀 Sedang memuat model NLP (Multilingual)...") |
| |
| 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: |
| |
| results = sentiment_pipeline(text)[0] |
| |
| |
| label_mapping = { |
| "positive": "😊 Positif", |
| "neutral": "😐 Netral", |
| "negative": "😡 Negatif" |
| } |
| |
| |
| |
| confidences = {label_mapping.get(res['label'], res['label'].capitalize()): res['score'] for res in results} |
| |
| |
| 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)}", {} |
|
|
| |
| 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): |
| |
| 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): |
| |
| out_kesimpulan = gr.Markdown(label="Kesimpulan AI") |
| out_label = gr.Label(label="📊 Distribusi Emosi (Probabilitas)") |
| |
| |
| 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 |
| ) |
| |
| |
| btn.click(fn=analyze_sentiment, inputs=inp_text, outputs=[out_kesimpulan, out_label]) |
|
|
| if __name__ == "__main__": |
| demo.launch() |