File size: 1,858 Bytes
30be50f
 
 
 
 
 
 
 
 
 
 
 
 
 
5a993ac
 
 
 
30be50f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
"""
Graded Challenge 7
Nama: Devin Yaung Lee
Batch: HCK-009
// eda.py //
program ini menjadi base model EDA interface.
"""
import streamlit as st
import pandas as pd
import numpy as np
from tensorflow import keras
from keras.models import load_model
import os

import nltk

nltk.download('punkt')

# Import the text preprocessing and prediction functions if they are defined elsewhere
from file import text_preprocessing

# Define a function to make predictions
def make_prediction(model, texts, text_preprocessing):
    # Apply custom text preprocessing
    preprocessed_texts = [text_preprocessing(text) for text in texts]

    # Predict using the loaded model
    predictions = model.predict(preprocessed_texts)
    return predictions

def run():
    st.title("Predict the User Sentiment")

    # Check if the model directory exists before loading the model

    model = load_model('model_lstm')

    # User input for review text
    user_input = st.text_area("Enter your review:")

    if st.button('Predict'):
        # Preprocess the user input
        preprocessed_text = text_preprocessing(user_input)

        # Make predictions using the preprocessed data
        predictions = make_prediction(model, [preprocessed_text], text_preprocessing)

        # Convert prediction probabilities to class labels
        predicted_class = np.argmax(predictions, axis=1)[0]

        # Mapping index to class label
        class_labels = {0: 'bad', 1: 'neutral', 2: 'good'}
        predicted_label = class_labels[predicted_class]

        # Display the prediction
        if predicted_label == 'bad':
            st.error("The model predicts the sentiment as bad.")
        elif predicted_label == 'neutral':
            st.warning("The model predicts the sentiment as neutral.")
        else:
            st.success("The model predicts the sentiment as good.")