Spaces:
Sleeping
Sleeping
Commit ·
adb3a4d
1
Parent(s): 19185a1
commit
Browse files
app.py
CHANGED
|
@@ -1,7 +1,36 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import argostranslate.translate
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
sentiment_model = pipeline(
|
| 6 |
"sentiment-analysis",
|
| 7 |
model="w11wo/indonesian-roberta-base-sentiment-classifier"
|
|
@@ -18,19 +47,14 @@ topic_model = pipeline(
|
|
| 18 |
model="YagiASAFAS/indonesia-news-classification-bert"
|
| 19 |
)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
try:
|
| 23 |
-
return argostranslate.translate.translate(label_en, "en", "id")
|
| 24 |
-
except Exception:
|
| 25 |
-
return label_en
|
| 26 |
-
|
| 27 |
def analyze_text(text):
|
| 28 |
if not text or not text.strip():
|
| 29 |
return {"error": "Teks kosong. Silakan masukkan kalimat Bahasa Indonesia."}
|
| 30 |
|
| 31 |
sentiment = sentiment_model(text)[0]
|
| 32 |
sentiment_result = {
|
| 33 |
-
"label":
|
| 34 |
"score": round(sentiment["score"], 4)
|
| 35 |
}
|
| 36 |
|
|
@@ -42,7 +66,7 @@ def analyze_text(text):
|
|
| 42 |
|
| 43 |
topic = topic_model(text)[0]
|
| 44 |
topic_result = {
|
| 45 |
-
"label": translate_label(topic["label"]),
|
| 46 |
"score": round(topic["score"], 4)
|
| 47 |
}
|
| 48 |
|
|
@@ -52,12 +76,13 @@ def analyze_text(text):
|
|
| 52 |
"topic": topic_result
|
| 53 |
}
|
| 54 |
|
|
|
|
| 55 |
demo = gr.Interface(
|
| 56 |
fn=analyze_text,
|
| 57 |
inputs=gr.Textbox(lines=3, placeholder="Masukkan kalimat Bahasa Indonesia..."),
|
| 58 |
outputs=gr.JSON(label="Hasil Analisis"),
|
| 59 |
title="Analisis Sentimen, Entitas, & Topik Bahasa Indonesia",
|
| 60 |
-
description="Gunakan AI untuk analisis sentimen, pengenalan entitas, dan deteksi topik otomatis
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
| 3 |
+
import argostranslate.package
|
| 4 |
import argostranslate.translate
|
| 5 |
+
import os
|
| 6 |
+
import urllib.request
|
| 7 |
|
| 8 |
+
# ----- Fungsi untuk download dan install Argos Translate model (en -> id) -----
|
| 9 |
+
def setup_argos_translate():
|
| 10 |
+
model_path = "en_id.argosmodel"
|
| 11 |
+
if not os.path.exists(model_path):
|
| 12 |
+
print("Downloading English -> Indonesian translation model...")
|
| 13 |
+
url = "https://www.argosopentech.com/models/en_id.argosmodel"
|
| 14 |
+
urllib.request.urlretrieve(url, model_path)
|
| 15 |
+
# Install package jika belum
|
| 16 |
+
argostranslate.package.install_from_path(model_path)
|
| 17 |
+
|
| 18 |
+
# Jalankan setup
|
| 19 |
+
setup_argos_translate()
|
| 20 |
+
|
| 21 |
+
# Load languages
|
| 22 |
+
installed_languages = argostranslate.translate.get_installed_languages()
|
| 23 |
+
en_lang = next(filter(lambda l: l.code == "en", installed_languages))
|
| 24 |
+
id_lang = next(filter(lambda l: l.code == "id", installed_languages))
|
| 25 |
+
|
| 26 |
+
# ----- Fungsi translate label -----
|
| 27 |
+
def translate_label(label_en):
|
| 28 |
+
try:
|
| 29 |
+
return en_lang.get_translation(id_lang).translate(label_en)
|
| 30 |
+
except Exception:
|
| 31 |
+
return label_en
|
| 32 |
+
|
| 33 |
+
# ----- Load Models -----
|
| 34 |
sentiment_model = pipeline(
|
| 35 |
"sentiment-analysis",
|
| 36 |
model="w11wo/indonesian-roberta-base-sentiment-classifier"
|
|
|
|
| 47 |
model="YagiASAFAS/indonesia-news-classification-bert"
|
| 48 |
)
|
| 49 |
|
| 50 |
+
# ----- Fungsi Analisis Teks -----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def analyze_text(text):
|
| 52 |
if not text or not text.strip():
|
| 53 |
return {"error": "Teks kosong. Silakan masukkan kalimat Bahasa Indonesia."}
|
| 54 |
|
| 55 |
sentiment = sentiment_model(text)[0]
|
| 56 |
sentiment_result = {
|
| 57 |
+
"label": sentiment["label"],
|
| 58 |
"score": round(sentiment["score"], 4)
|
| 59 |
}
|
| 60 |
|
|
|
|
| 66 |
|
| 67 |
topic = topic_model(text)[0]
|
| 68 |
topic_result = {
|
| 69 |
+
"label": translate_label(topic["label"]), # otomatis translate offline
|
| 70 |
"score": round(topic["score"], 4)
|
| 71 |
}
|
| 72 |
|
|
|
|
| 76 |
"topic": topic_result
|
| 77 |
}
|
| 78 |
|
| 79 |
+
# ----- Gradio UI -----
|
| 80 |
demo = gr.Interface(
|
| 81 |
fn=analyze_text,
|
| 82 |
inputs=gr.Textbox(lines=3, placeholder="Masukkan kalimat Bahasa Indonesia..."),
|
| 83 |
outputs=gr.JSON(label="Hasil Analisis"),
|
| 84 |
title="Analisis Sentimen, Entitas, & Topik Bahasa Indonesia",
|
| 85 |
+
description="Gunakan AI untuk analisis sentimen, pengenalan entitas, dan deteksi topik otomatis."
|
| 86 |
)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|