Create app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import streamlit as stimport numpy as npimport pickleimport jsonimport reimport tensorflow as tffrom tensorflow.keras.preprocessing.sequence import pad_sequences
# --- Load Preprocessing Objects ---@st.cache_resourcedef 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%}")app.pyAdded+49