File size: 1,515 Bytes
f34ca92
 
 
 
 
 
 
 
2e39850
f34ca92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Contents of `app2.py`
import streamlit as st
import tensorflow as tf
from tensorflow.keras.models import load_model

# Function to load the model
@st.cache_data
def load_sentiment_model():
    model = load_model('model.keras')
    return model

# Function to make predictions
def predict_sentiment(review_text, model):
    # Perform prediction
    pred = model.predict([review_text])
    prediction = tf.where(pred >= 0.5, 1, 0)
    
    # Convert tensor to a list of 1s and 0s
    predictions_list = prediction.numpy().flatten().tolist()
    
    # Replace 1 with 'Recommending' and 0 with 'Not Recommending'
    predictions_recommended = ['The author recommending this product' if x == 1 else 'The author not recommending this product' for x in predictions_list]
    
    return predictions_recommended

# Streamlit app function
def app():
    st.title('Make Predictions')
    
    # Load the model
    model = load_sentiment_model()
    
    # Text input for user
    user_input = st.text_area("Enter your review:", "")
    
    if st.button("Predict"):
        if user_input:
            # Display a loading message while predicting
            with st.spinner('Predicting...'):
                # Perform prediction
                predictions = predict_sentiment(user_input, model)
                
                # Display the prediction result
                st.success(f'Prediction: {predictions[0]}')
        else:
            st.warning("Please enter a review.")

if __name__ == '__main__':
    app()