isan2001 commited on
Commit
344269b
1 Parent(s): cbdad76

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from transformers import BertTokenizer
3
+ from transformers import TFBertForSequenceClassification
4
+ from Sastrawi.Stemmer.StemmerFactory import StemmerFactory # Import Sastrawi
5
+ import streamlit as st
6
+
7
+ # Fungsi untuk memuat model BERT dan tokenizer
8
+ PRE_TRAINED_MODEL = 'indobenchmark/indobert-base-p2'
9
+ bert_tokenizer = BertTokenizer.from_pretrained(PRE_TRAINED_MODEL)
10
+ bert_model = TFBertForSequenceClassification.from_pretrained(PRE_TRAINED_MODEL, num_labels=2)
11
+ bert_model.load_weights('model.h5')
12
+
13
+ # Inisialisasi stemmer dari Sastrawi
14
+ stemmer = StemmerFactory().create_stemmer() # Membuat stemmer Sastrawi
15
+
16
+ def preprocess_text(text):
17
+ # Menggunakan Sastrawi untuk stemming
18
+ stemmed_text = stemmer.stem(text.lower())
19
+
20
+ return stemmed_text
21
+
22
+ def predict_sentiment(text):
23
+ preprocessed_text = preprocess_text(text) # Pra-pemrosesan teks dengan Sastrawi
24
+ input_ids = tf.constant(bert_tokenizer.encode(preprocessed_text, add_special_tokens=True))[None, :]
25
+ logits = bert_model(input_ids)[0]
26
+ probabilities = tf.nn.softmax(logits, axis=1)
27
+ sentiment = tf.argmax(probabilities, axis=1)
28
+ return sentiment.numpy()[0], probabilities.numpy()[0]
29
+
30
+ # Judul aplikasi
31
+ st.title('Prediksi Sentimen menggunakan BERT')
32
+
33
+ # Input teks
34
+ text = st.text_area('Masukkan teks', '')
35
+
36
+ # Tombol untuk memprediksi sentimen
37
+ if st.button('Prediksi'):
38
+ if text.strip() == '':
39
+ st.warning('Masukkan teks terlebih dahulu.')
40
+ else:
41
+ sentiment, probabilities = predict_sentiment(text)
42
+
43
+ # Menghitung persentase probabilitas sentimen positif
44
+ positive_probability = probabilities[1] * 100
45
+ negative_probability = probabilities[0] * 100
46
+ st.write(f'HASIL PREDIKSI')
47
+ if sentiment == 0:
48
+ st.write(f'Negatif ({negative_probability:.2f}%)')
49
+ else:
50
+ st.write(f'Positif ({positive_probability:.2f}%)')