Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,36 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
tokenizer = AutoTokenizer.from_pretrained('hasbigani/sentiment') # Ganti dengan username dan repo kamu
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Ganti dengan nama repository model kamu
|
| 6 |
+
model_name = "username/indobertsentiment"
|
|
|
|
| 7 |
|
| 8 |
+
# Load model & tokenizer
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
# Label mapping (ubah sesuai label modelmu)
|
| 13 |
+
label_map = {
|
| 14 |
+
0: "Negatif",
|
| 15 |
+
1: "Netral",
|
| 16 |
+
2: "Positif"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# Fungsi prediksi
|
| 20 |
+
def predict_sentiment(text):
|
| 21 |
+
inputs = tokenizer([text], padding=True, truncation=True, return_tensors="pt")
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
pred = torch.argmax(outputs.logits, dim=-1).item()
|
| 25 |
+
return label_map[pred]
|
| 26 |
+
|
| 27 |
+
# Gradio interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=predict_sentiment,
|
| 30 |
+
inputs=gr.Textbox(lines=3, placeholder="Tulis teks di sini..."),
|
| 31 |
+
outputs=gr.Label(),
|
| 32 |
+
title="Demo Sentimen IndoBERT",
|
| 33 |
+
description="Masukkan kalimat berbahasa Indonesia untuk menguji model sentimen yang sudah diupload di Hugging Face."
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
iface.launch()
|