|
import tensorflow as tf |
|
import streamlit as st |
|
from transformers import BertTokenizer |
|
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory |
|
from transformers import TFBertForSequenceClassification |
|
|
|
|
|
|
|
PRE_TRAINED_MODEL = 'indobenchmark/indobert-base-p2' |
|
bert_tokenizer = BertTokenizer.from_pretrained(PRE_TRAINED_MODEL) |
|
bert_model = TFBertForSequenceClassification.from_pretrained(PRE_TRAINED_MODEL, num_labels=2) |
|
bert_model.load_weights('model.h5') |
|
|
|
|
|
stemmer = StemmerFactory().create_stemmer() |
|
|
|
def preprocess_text(text): |
|
|
|
stemmed_text = stemmer.stem(text.lower()) |
|
|
|
return stemmed_text |
|
|
|
def predict_sentiment(text): |
|
preprocessed_text = preprocess_text(text) |
|
input_ids = tf.constant(bert_tokenizer.encode(preprocessed_text, add_special_tokens=True))[None, :] |
|
logits = bert_model(input_ids)[0] |
|
probabilities = tf.nn.softmax(logits, axis=1) |
|
sentiment = tf.argmax(probabilities, axis=1) |
|
return sentiment.numpy()[0] |
|
|
|
|
|
|
|
st.title('Prediksi Sentimen menggunakan BERT') |
|
|
|
|
|
text = st.text_area('Masukkan teks', '') |
|
|
|
|
|
if st.button('Prediksi'): |
|
if text.strip() == '': |
|
st.warning('Masukkan teks terlebih dahulu.') |
|
else: |
|
sentiment, probabilities = predict_sentiment(text) |
|
|
|
|
|
positive_probability = probabilities[1] * 100 |
|
negative_probability = probabilities[0] * 100 |
|
st.write(f'HASIL PREDIKSI') |
|
if sentiment == 0: |
|
st.write(f'Negatif ({negative_probability:.2f}%)') |
|
else: |
|
st.write(f'Positif ({positive_probability:.2f}%)') |