PPKM_sentiment / predict.py
William14045's picture
commit1
15462a3
raw
history blame contribute delete
No virus
1.62 kB
import streamlit as st
import pandas as pd
import numpy as np
import tensorflow as tf
import pickle
import tensorflow_hub as hub
hub_layer = hub.KerasLayer("https://tfhub.dev/google/nnlm-id-dim128-with-normalization/2",
input_shape=[], dtype=tf.string,)
tf.keras.utils.get_custom_objects()['KerasLayer'] = hub_layer
nlp = tf.keras.models.load_model('sentiment_PPKM_NLP_Model.h5')
with open('formalisasi.txt', 'r') as file:
function_text = file.read()
with open("proses text.txt",'r') as file1:
proc = file1.read()
with open('kamus.txt','rb') as file2:
kamus = pickle.load(file2)
def create_function_from_text(function_tex):
# Gunakan pustaka exec() untuk mengevaluasi teks menjadi objek fungsi
exec(function_tex, globals())
# Ambil fungsi yang telah dibuat dan kembalikan sebagai output
return list(globals().values())[0]
formalize_words = create_function_from_text(function_text)
text_process = create_function_from_text(proc)
def run():
with st.form('key=sentiment prediction'):
sentiment = st.text_input('ketikkan tweet anda:', '')
submitted = st.form_submit_button('Predict')
data_inf = {
'sentiment': sentiment
}
data_inf = pd.DataFrame([data_inf])
# Menerapkan fungsi klasifikasi sentimen menggunakan apply pada DataFrame
data_inf['proc_sentiment'] = data_inf['sentiment'].apply(text_process)
if submitted:
y_pred_inf = [np.argmax(pred) for pred in nlp.predict(data_inf['proc_sentiment'])]
st.write('sentiment : ', str(y_pred_inf))
if __name__ == '__main__':
run()