File size: 1,700 Bytes
381b3f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import pickle
import json
import re
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences

# --- Load Preprocessing Objects ---
@st.cache_resource
def load_resources():
    model = tf.keras.models.load_model("ensemble_model.keras")
    with open("tokenizer.pkl", "rb") as f:
        tokenizer = pickle.load(f)
    with open("config.json", "r") as f:
        config = json.load(f)
    return model, tokenizer, config['max_length']

model, tokenizer, max_length = load_resources()

# --- Text Normalization ---
NON_ALPHANUM = re.compile(r'\W')
NON_ASCII = re.compile(r'[^a-z0-1\s]')
def normalize_text(text):
    return NON_ASCII.sub('', NON_ALPHANUM.sub(' ', text.lower()))

# --- Inference ---
def predict_sentiment(text):
    text = normalize_text(text)
    sequence = tokenizer.texts_to_sequences([text])
    padded = pad_sequences(sequence, maxlen=max_length)
    prediction = model.predict(padded)[0][0]
    sentiment = "Positive 😊" if prediction > 0.5 else "Negative 😞"
    return sentiment, float(prediction)

# --- UI ---
st.title("🗣️ Sentiment Analysis of Customer Reviews")
st.markdown("This app uses a deep learning model to predict the **sentiment** of customer feedback — whether it's **positive** or **negative**.")

user_input = st.text_area("Enter a customer review:")

if st.button("Analyze"):
    if user_input.strip() == "":
        st.warning("Please enter some text to analyze.")
    else:
        sentiment, confidence = predict_sentiment(user_input)
        st.subheader("Prediction")
        st.write(f"**Sentiment:** {sentiment}")
        st.write(f"**Confidence:** {confidence:.2%}")