|
|
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 |
|
|
|
|
|
|
|
|
@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() |
|
|
|
|
|
|
|
|
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())) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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%}") |
|
|
|