Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load model dan tokenizer dari Hugging Face Hub
|
| 7 |
+
model = tf.keras.models.load_model("jeanetrixsiee/bert-sentimen-model")
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("jeanetrixsiee/bert-sentimen-model")
|
| 9 |
+
|
| 10 |
+
# Label kelas
|
| 11 |
+
labels = ['Negative', 'Neutral', 'Positive', 'Very Negative', 'Very Positive']
|
| 12 |
+
|
| 13 |
+
def predict_sentiment(text):
|
| 14 |
+
# Tokenisasi
|
| 15 |
+
inputs = tokenizer(text, return_tensors="tf", padding=True, truncation=True, max_length=256)
|
| 16 |
+
|
| 17 |
+
# Prediksi
|
| 18 |
+
outputs = model(inputs) # TensorFlow model output
|
| 19 |
+
|
| 20 |
+
# Kalau model output pakai logits, gunakan softmax
|
| 21 |
+
if hasattr(outputs, "logits"):
|
| 22 |
+
probs = tf.nn.softmax(outputs.logits, axis=1)
|
| 23 |
+
else:
|
| 24 |
+
probs = tf.nn.softmax(outputs, axis=1)
|
| 25 |
+
|
| 26 |
+
# Konversi ke dictionary
|
| 27 |
+
return {labels[i]: float(probs[0][i]) for i in range(len(labels))}
|
| 28 |
+
|
| 29 |
+
# Gradio UI
|
| 30 |
+
demo = gr.Interface(
|
| 31 |
+
fn=predict_sentiment,
|
| 32 |
+
inputs=gr.Textbox(lines=3, placeholder="Tulis komentar di sini..."),
|
| 33 |
+
outputs=gr.Label(num_top_classes=3),
|
| 34 |
+
title="Demo Sentimen BERT Bahasa Inggris",
|
| 35 |
+
description="Prediksi sentimen komentar menggunakan BERT base (TensorFlow). Kategori: Very Negative, Negative, Neutral, Positive, Very Positive",
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Jalankan
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
demo.launch()
|