File size: 1,294 Bytes
7b7e7f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import pandas as pd
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle

# Load the model
model = load_model("model.h5")

# Load the tokenizer
with open("tokenizer.pkl", "rb") as handle:
    tokenizer = pickle.load(handle)

# Streamlit app
st.title("Sentiment Analysis of Reviews")
st.write("Enter a review to predict if it's good or bad.")

# Input text box
text = st.text_area("Write a review here", "")

# Adjust threshold
threshold = st.slider("Adjust prediction threshold", min_value=0.0, max_value=1.0, value=0.5)

# Predict button
if st.button("Predict"):
    if text:
        TokenText = tokenizer.texts_to_sequences([text])
        PadText = pad_sequences(TokenText, maxlen=100)
        Pred = model.predict(PadText)
        Pred_float = Pred[0][0]  # Extract the single float value
        binary_pred = (Pred_float > threshold).astype(int)
        if binary_pred == 0:
            st.write("Bad review")
        else:
            st.write("Good review")
        st.write(f"Prediction score: {Pred_float}")
    else:
        st.write("Please enter a review to predict.")

# To run the app, save this script and run `streamlit run your_script_name.py` in the terminal.