Skip to content

Instantly share code, notes, and snippets.

@tuttinator
Created May 5, 2026 05:08
Show Gist options
  • Select an option

  • Save tuttinator/c891c4d45e817d143d2bb5ae3aa99d4a to your computer and use it in GitHub Desktop.

Select an option

Save tuttinator/c891c4d45e817d143d2bb5ae3aa99d4a to your computer and use it in GitHub Desktop.
Indonesian sentiment analysis with IndoBERT

Indonesian Sentiment Analysis with IndoBERT

A small Python example for classifying Indonesian text sentiment with a Hugging Face Transformers pipeline.

The script uses mdhugol/indonesia-bert-sentiment-classification, a sentiment classification model fine-tuned from IndoBERT.

Setup

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Run

python app.py

The first run downloads the model from Hugging Face.

Labels

The model returns generic classifier labels, so app.py maps them to readable sentiment labels:

Model label Sentiment
LABEL_0 positive
LABEL_1 neutral
LABEL_2 negative

Use in Code

from app import classify_sentiment

result = classify_sentiment("Pelayanannya sangat baik dan prosesnya cepat.")
print(result)

Example output:

{
    "text": "Pelayanannya sangat baik dan prosesnya cepat.",
    "label": "positive",
    "score": 0.99,
}
from typing import Any, Dict
from transformers import pipeline
MODEL_NAME = "mdhugol/indonesia-bert-sentiment-classification"
LABELS = {
"LABEL_0": "positive",
"LABEL_1": "neutral",
"LABEL_2": "negative",
}
classifier = pipeline("sentiment-analysis", model=MODEL_NAME, tokenizer=MODEL_NAME)
EXAMPLES = [
# Formal
"Pelayanannya sangat baik dan prosesnya cepat.",
"Aplikasi ini sering error dan sangat mengecewakan.",
"Menurut saya produknya biasa saja, tidak terlalu bagus tapi juga tidak buruk.",
# Informal
"barangnya mantappp, pengiriman cepet, seller ramah bgt 👍",
"parah sih ini, udah nunggu lama eh barangnya rusak.",
"lumayan lah, ga jelek-jelek amat.",
"gw kira bakal bagus, ternyata zonk wkwk.",
"adminnya slow response banget, bikin males.",
"worth it sih dengan harga segini.",
"nggak nyesel beli di sini, recommended!",
"fiturnya oke, tapi UI-nya agak ribet.",
"yaudah lah, standar aja menurutku.",
"anjir bagus banget hasilnya, di luar ekspektasi.",
"ga sesuai foto, kecewa berat.",
"kirain abal-abal, ternyata legit juga.",
"bagus sih, cuma packaging-nya kurang aman.",
"not bad, tapi masih banyak bug.",
]
def classify_sentiment(text: str) -> Dict[str, Any]:
result = classifier(text)[0]
return {
"text": text,
"label": LABELS.get(result["label"], result["label"]),
"score": result["score"],
}
if __name__ == "__main__":
for text in EXAMPLES:
result = classify_sentiment(text)
print(f"Text: {result['text']}")
print(f"Sentiment: {result['label']} ({result['score']:.2%})")
print()
transformers>=4.40,<5
torch>=2.2
sentencepiece>=0.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment