File size: 1,580 Bytes
083abe6
 
 
ee04ce9
083abe6
 
ee04ce9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app.py

import torch
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# --- Load model and tokenizer ---
model_name = "taufiqdp/indonesian-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# --- Prediction function ---
def predict_sentiment(text):
    if not text.strip():
        return "⚠️ Mohon masukkan teks."

    inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True).to(device)
    with torch.inference_mode():
        logits = model(**inputs).logits
        pred_class = torch.argmax(logits, dim=1).item()

    labels = ['Negatif', 'Netral', 'Positif']
    sentiment = labels[pred_class]
    return f"**Hasil Analisis Sentimen:** {sentiment}"

# --- Build Gradio UI ---
title = "🇮🇩 Indonesian Sentiment Analyzer"
description = "Masukkan teks Bahasa Indonesia untuk mengetahui apakah sentimennya **Positif**, **Netral**, atau **Negatif**."

interface = gr.Interface(
    fn=predict_sentiment,
    inputs=gr.Textbox(lines=4, placeholder="Contoh: Pelayanan cepat dan memuaskan"),
    outputs="markdown",
    title=title,
    description=description,
    theme="gradio/soft",
    examples=[
        ["Makanan di restoran ini sangat enak dan lezat."],
        ["Pelayanan biasa saja, tidak buruk tapi tidak istimewa."],
        ["Saya kecewa dengan produk ini."]
    ]
)

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