isan2001 commited on
Commit
f583409
·
1 Parent(s): 120139e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ from transformers import BertTokenizer
4
+ from transformers import TFBertForSequenceClassification
5
+
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
+
14
+ def predict_sentiment(text):
15
+ input_ids = tf.constant(bert_tokenizer.encode(text, add_special_tokens=True))[None, :] # Menambahkan token khusus [CLS] dan [SEP]
16
+ logits = bert_model(input_ids)[0]
17
+ probabilities = tf.nn.softmax(logits, axis=1)
18
+ sentiment = tf.argmax(probabilities, axis=1)
19
+ return sentiment.numpy()[0]
20
+
21
+
22
+ # Judul aplikasi
23
+ st.title('Prediksi Sentimen menggunakan BERT')
24
+
25
+ # Input teks
26
+ text = st.text_area('Masukkan teks', '')
27
+
28
+ # Tombol untuk memprediksi sentimen
29
+ if st.button('Prediksi'):
30
+ if text.strip() == '':
31
+ st.warning('Masukkan teks terlebih dahulu.')
32
+ else:
33
+ sentiment = predict_sentiment(text)
34
+ if sentiment == 0:
35
+ st.error('Sentimen: Negatif')
36
+ else:
37
+ st.success('Sentimen: Positif')