Spaces:
Sleeping
Sleeping
Commit ·
5b878de
1
Parent(s): adb3a4d
commit
Browse files
app.py
CHANGED
|
@@ -1,36 +1,6 @@
|
|
| 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,7 +17,6 @@ topic_model = pipeline(
|
|
| 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."}
|
|
@@ -66,7 +35,7 @@ def analyze_text(text):
|
|
| 66 |
|
| 67 |
topic = topic_model(text)[0]
|
| 68 |
topic_result = {
|
| 69 |
-
"label":
|
| 70 |
"score": round(topic["score"], 4)
|
| 71 |
}
|
| 72 |
|
|
@@ -76,13 +45,12 @@ def analyze_text(text):
|
|
| 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__":
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
sentiment_model = pipeline(
|
| 5 |
"sentiment-analysis",
|
| 6 |
model="w11wo/indonesian-roberta-base-sentiment-classifier"
|
|
|
|
| 17 |
model="YagiASAFAS/indonesia-news-classification-bert"
|
| 18 |
)
|
| 19 |
|
|
|
|
| 20 |
def analyze_text(text):
|
| 21 |
if not text or not text.strip():
|
| 22 |
return {"error": "Teks kosong. Silakan masukkan kalimat Bahasa Indonesia."}
|
|
|
|
| 35 |
|
| 36 |
topic = topic_model(text)[0]
|
| 37 |
topic_result = {
|
| 38 |
+
"label": topic["label"],
|
| 39 |
"score": round(topic["score"], 4)
|
| 40 |
}
|
| 41 |
|
|
|
|
| 45 |
"topic": topic_result
|
| 46 |
}
|
| 47 |
|
|
|
|
| 48 |
demo = gr.Interface(
|
| 49 |
fn=analyze_text,
|
| 50 |
inputs=gr.Textbox(lines=3, placeholder="Masukkan kalimat Bahasa Indonesia..."),
|
| 51 |
outputs=gr.JSON(label="Hasil Analisis"),
|
| 52 |
title="Analisis Sentimen, Entitas, & Topik Bahasa Indonesia",
|
| 53 |
+
description="Gunakan AI untuk analisis sentimen, pengenalan entitas, dan deteksi topik otomatis (multilingual)."
|
| 54 |
)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|