Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +73 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,75 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import pickle
|
| 4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# --- 1. CONFIGURATION & SETUP ---
|
| 8 |
+
st.set_page_config(page_title="IMDb Sentiment Analysis", page_icon="π¬")
|
| 9 |
+
|
| 10 |
+
# Constants (Must match training parameters)
|
| 11 |
+
MAX_LEN = 200
|
| 12 |
+
MODEL_PATH = "src/sentiment_lstm_model.h5"
|
| 13 |
+
TOKENIZER_PATH = "src/tokenizer.pickle"
|
| 14 |
+
|
| 15 |
+
# --- 2. LOAD RESOURCES (Cached for performance) ---
|
| 16 |
+
@st.cache_resource
|
| 17 |
+
def load_model():
|
| 18 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
| 19 |
+
return model
|
| 20 |
+
|
| 21 |
+
@st.cache_resource
|
| 22 |
+
def load_tokenizer():
|
| 23 |
+
with open(TOKENIZER_PATH, 'rb') as handle:
|
| 24 |
+
tokenizer = pickle.load(handle)
|
| 25 |
+
return tokenizer
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
model = load_model()
|
| 29 |
+
tokenizer = load_tokenizer()
|
| 30 |
+
st.success("System loaded successfully! π")
|
| 31 |
+
except Exception as e:
|
| 32 |
+
st.error(f"Error loading files: {e}")
|
| 33 |
+
st.stop()
|
| 34 |
+
|
| 35 |
+
# --- 3. PREDICTION FUNCTION ---
|
| 36 |
+
def predict_sentiment(review_text):
|
| 37 |
+
# Preprocess the text
|
| 38 |
+
# Note: Text cleaning is handled implicitly by the tokenizer to some extent,
|
| 39 |
+
# but for best results, basic lowercase/strip is recommended.
|
| 40 |
+
review_seq = tokenizer.texts_to_sequences([review_text])
|
| 41 |
+
review_pad = pad_sequences(review_seq, maxlen=MAX_LEN)
|
| 42 |
+
|
| 43 |
+
# Predict
|
| 44 |
+
probability = model.predict(review_pad)[0][0]
|
| 45 |
+
|
| 46 |
+
if probability > 0.5:
|
| 47 |
+
return "Positive", probability
|
| 48 |
+
else:
|
| 49 |
+
return "Negative", 1 - probability
|
| 50 |
+
|
| 51 |
+
# --- 4. STREAMLIT UI ---
|
| 52 |
+
st.title("π¬ Movie Review Sentiment Analysis")
|
| 53 |
+
st.markdown("Enter a movie review below to check if it's **Positive** or **Negative**.")
|
| 54 |
+
st.markdown("*(Powered by LSTM & Word2Vec)*")
|
| 55 |
+
|
| 56 |
+
# Text Input
|
| 57 |
+
user_input = st.text_area("Write your review here (English):", height=150)
|
| 58 |
|
| 59 |
+
if st.button("Analyze Sentiment"):
|
| 60 |
+
if user_input.strip() == "":
|
| 61 |
+
st.warning("Please enter some text first.")
|
| 62 |
+
else:
|
| 63 |
+
with st.spinner("Analyzing..."):
|
| 64 |
+
sentiment, score = predict_sentiment(user_input)
|
| 65 |
+
|
| 66 |
+
# Display Result
|
| 67 |
+
st.divider()
|
| 68 |
+
if sentiment == "Positive":
|
| 69 |
+
st.balloons()
|
| 70 |
+
st.success(f"**Verdict:** {sentiment} π")
|
| 71 |
+
else:
|
| 72 |
+
st.error(f"**Verdict:** {sentiment} π")
|
| 73 |
+
|
| 74 |
+
st.progress(float(score))
|
| 75 |
+
st.caption(f"Confidence Score: {score:.2%}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|