Spaces:
Sleeping
Sleeping
Commit ·
44aa281
1
Parent(s): bd64784
eighth commit
Browse files
app.py
CHANGED
|
@@ -1,29 +1,97 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
sentiment = sentiment_model(text)[0]
|
| 11 |
-
ner = ner_model(text)
|
| 12 |
-
topic = topic_model(text)[0]
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return {
|
| 15 |
-
"sentiment":
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
demo = gr.Interface(
|
| 21 |
-
fn=
|
| 22 |
-
inputs=gr.Textbox(
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
)
|
| 27 |
|
|
|
|
| 28 |
if __name__ == "__main__":
|
| 29 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# ==============================
|
| 5 |
+
# 1️⃣ Load semua model
|
| 6 |
+
# ==============================
|
| 7 |
+
|
| 8 |
+
# Sentiment Analysis (IndoBERT)
|
| 9 |
+
sentiment_model = pipeline(
|
| 10 |
+
"sentiment-analysis",
|
| 11 |
+
model="w11wo/indonesian-roberta-base-sentiment-classifier"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Named Entity Recognition (NER)
|
| 15 |
+
ner_model = pipeline(
|
| 16 |
+
"ner",
|
| 17 |
+
model="cahya/bert-base-indonesian-NER",
|
| 18 |
+
aggregation_strategy="simple"
|
| 19 |
+
)
|
| 20 |
|
| 21 |
+
# Zero-shot Topic Classification (multilingual)
|
| 22 |
+
topic_model = pipeline(
|
| 23 |
+
"zero-shot-classification",
|
| 24 |
+
model="joeddav/xlm-roberta-large-xnli"
|
| 25 |
+
)
|
| 26 |
|
| 27 |
+
# Daftar label topik yang ingin dikenali
|
| 28 |
+
TOPIC_LABELS = [
|
| 29 |
+
"politik",
|
| 30 |
+
"ekonomi",
|
| 31 |
+
"olahraga",
|
| 32 |
+
"teknologi",
|
| 33 |
+
"pendidikan",
|
| 34 |
+
"kesehatan",
|
| 35 |
+
"hiburan",
|
| 36 |
+
"sosial"
|
| 37 |
+
]
|
| 38 |
+
|
| 39 |
+
# ==============================
|
| 40 |
+
# 2️⃣ Fungsi analisis teks
|
| 41 |
+
# ==============================
|
| 42 |
+
|
| 43 |
+
def analyze_text(text):
|
| 44 |
+
if not text or not text.strip():
|
| 45 |
+
return {
|
| 46 |
+
"error": "Teks kosong. Silakan masukkan kalimat Bahasa Indonesia."
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Analisis sentimen
|
| 50 |
sentiment = sentiment_model(text)[0]
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
# Deteksi entitas
|
| 53 |
+
entities = ner_model(text)
|
| 54 |
+
entity_result = [
|
| 55 |
+
{
|
| 56 |
+
"entity": e["entity_group"],
|
| 57 |
+
"word": e["word"],
|
| 58 |
+
"score": round(e["score"], 4)
|
| 59 |
+
}
|
| 60 |
+
for e in entities
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
# Klasifikasi topik
|
| 64 |
+
topic = topic_model(text, TOPIC_LABELS)
|
| 65 |
+
topic_result = {
|
| 66 |
+
"label": topic["labels"][0],
|
| 67 |
+
"score": round(topic["scores"][0], 4)
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
# Gabungkan hasil
|
| 71 |
return {
|
| 72 |
+
"sentiment": {
|
| 73 |
+
"label": sentiment["label"],
|
| 74 |
+
"score": round(sentiment["score"], 4)
|
| 75 |
+
},
|
| 76 |
+
"entities": entity_result,
|
| 77 |
+
"topic": topic_result
|
| 78 |
}
|
| 79 |
|
| 80 |
+
# ==============================
|
| 81 |
+
# 3️⃣ UI Gradio
|
| 82 |
+
# ==============================
|
| 83 |
+
|
| 84 |
demo = gr.Interface(
|
| 85 |
+
fn=analyze_text,
|
| 86 |
+
inputs=gr.Textbox(
|
| 87 |
+
lines=3,
|
| 88 |
+
placeholder="Masukkan kalimat Bahasa Indonesia..."
|
| 89 |
+
),
|
| 90 |
+
outputs=gr.JSON(label="Hasil Analisis"),
|
| 91 |
+
title="Analisis Sentimen, Entitas, & Topik Bahasa Indonesia",
|
| 92 |
+
description="Gunakan AI berbasis IndoBERT & XLM-R untuk analisis sentimen, pengenalan entitas, dan deteksi topik otomatis."
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Jalankan app
|
| 96 |
if __name__ == "__main__":
|
| 97 |
demo.launch()
|