File size: 1,897 Bytes
f583409
503e370
f583409
503e370
f583409
 
 
 
 
 
 
 
 
8eb7f24
 
 
 
 
e815272
8eb7f24
 
f583409
 
8eb7f24
 
f583409
 
b38636e
 
 
f583409
 
 
 
 
 
 
 
167dedf
f583409
 
 
167dedf
 
 
 
 
 
b38636e
167dedf
f583409
167dedf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import tensorflow as tf
import streamlit as st
from transformers import BertTokenizer
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
from transformers import TFBertForSequenceClassification


# Fungsi untuk memuat model BERT dan tokenizer
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')

# Inisialisasi stemmer dari Sastrawi
stemmer = StemmerFactory().create_stemmer()  # Membuat stemmer Sastrawi

def preprocess_text(text):
    # Menggunakan Sastrawi untuk stemming
    stemmed_text = stemmer.stem(text.lower())

    return stemmed_text

def predict_sentiment(text):
    preprocessed_text = preprocess_text(text)  # Pra-pemrosesan teks dengan Sastrawi
    input_ids = tf.constant(bert_tokenizer.encode(preprocessed_text, add_special_tokens=True))[None, :]  # Menambahkan token khusus [CLS] dan [SEP]
    logits = bert_model(input_ids)[0]
    probabilities = tf.nn.softmax(logits, axis=1)
    sentiment = tf.argmax(probabilities, axis=1)
    return sentiment.numpy()[0]


# Judul aplikasi
st.title('Prediksi Sentimen menggunakan BERT')

# Input teks
text = st.text_area('Masukkan teks', '')

# Tombol untuk memprediksi sentimen
if st.button('Prediksi'):
    if text.strip() == '':
        st.warning('Masukkan teks terlebih dahulu.')
    else:
        sentiment, probabilities = predict_sentiment(text)
        
        # Menghitung persentase probabilitas sentimen positif
        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}%)')