File size: 1,489 Bytes
7252317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import pickle
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer

# Load the model, tokenizer, and label map
model = load_model('sentiment_model.h5')

with open('tokenizer.pkl', 'rb') as file:
    tokenizer = pickle.load(file)

with open('label_map.pkl', 'rb') as file:
    label_map = pickle.load(file)

# Define max length
max_len = 100

def preprocess_text(text, tokenizer, max_len):
    sequence = tokenizer.texts_to_sequences([text])
    padded_sequence = pad_sequences(sequence, maxlen=max_len)
    return padded_sequence

def predict_sentiment(text, model, tokenizer, max_len, label_map):
    processed_text = preprocess_text(text, tokenizer, max_len)
    prediction = model.predict(processed_text)
    predicted_class = np.argmax(prediction, axis=1)[0]
    predicted_label = label_map[predicted_class]
    return predicted_label

# Streamlit app
st.title("Sentiment Analysis App")

st.write("Enter a text to predict its sentiment:")

text_input = st.text_area("Text", "Type your text here...")

if st.button('Predict'):
    if text_input:
        predicted_sentiment = predict_sentiment(text_input, model, tokenizer, max_len, label_map)
        st.write(f"The predicted sentiment for the text is: {predicted_sentiment}")
    else:
        st.write("Please enter some text.")