Spaces:
Sleeping
Sleeping
| 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. |