Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,12 @@
|
|
| 1 |
-
import
|
| 2 |
import pickle
|
| 3 |
import re
|
| 4 |
import nltk
|
| 5 |
from nltk.corpus import stopwords
|
| 6 |
|
| 7 |
# Download stopwords if not already present
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
except LookupError:
|
| 11 |
-
nltk.download('stopwords')
|
| 12 |
-
stop_words = set(stopwords.words('english'))
|
| 13 |
|
| 14 |
def clean_text(text):
|
| 15 |
"""Clean and preprocess text similar to training preprocessing"""
|
|
@@ -20,75 +17,34 @@ def clean_text(text):
|
|
| 20 |
words = [w for w in words if w not in stop_words] # Remove stopwords
|
| 21 |
return ' '.join(words) # Join words back
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
st.stop()
|
| 35 |
except Exception as e:
|
| 36 |
-
|
| 37 |
-
st.stop()
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# App title
|
| 48 |
-
st.title("🎬 IMDB Sentiment Analysis")
|
| 49 |
-
st.markdown("Enter a movie review below to analyze its sentiment!")
|
| 50 |
-
|
| 51 |
-
# Load model and vectorizer
|
| 52 |
-
model, vectorizer = load_model_and_vectorizer()
|
| 53 |
-
|
| 54 |
-
# Text input area
|
| 55 |
-
review_text = st.text_area(
|
| 56 |
-
"Movie Review:",
|
| 57 |
-
placeholder="Enter your movie review here...",
|
| 58 |
-
height=150
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
# Analyze button
|
| 62 |
-
if st.button("Analyze", type="primary"):
|
| 63 |
-
if not review_text.strip():
|
| 64 |
-
st.warning("Please enter a review first.")
|
| 65 |
-
else:
|
| 66 |
-
try:
|
| 67 |
-
# Clean the input text
|
| 68 |
-
cleaned_text = clean_text(review_text)
|
| 69 |
-
|
| 70 |
-
# Transform text using the loaded vectorizer
|
| 71 |
-
text_vectorized = vectorizer.transform([cleaned_text])
|
| 72 |
-
|
| 73 |
-
# Make prediction
|
| 74 |
-
prediction = model.predict(text_vectorized)[0]
|
| 75 |
-
|
| 76 |
-
# Display result
|
| 77 |
-
if prediction == 1:
|
| 78 |
-
st.success("✅ Positive Review")
|
| 79 |
-
else:
|
| 80 |
-
st.error("❌ Negative Review")
|
| 81 |
-
|
| 82 |
-
except Exception as e:
|
| 83 |
-
st.error(f"Error during prediction: {e}")
|
| 84 |
-
|
| 85 |
-
# Add some info about the model
|
| 86 |
-
with st.expander("ℹ️ About this model"):
|
| 87 |
-
st.write("""
|
| 88 |
-
This sentiment analysis model was trained on the IMDB movie reviews dataset
|
| 89 |
-
using Logistic Regression with TF-IDF features. The model classifies movie
|
| 90 |
-
reviews as either positive or negative sentiment.
|
| 91 |
-
""")
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import pickle
|
| 3 |
import re
|
| 4 |
import nltk
|
| 5 |
from nltk.corpus import stopwords
|
| 6 |
|
| 7 |
# Download stopwords if not already present
|
| 8 |
+
nltk.download('stopwords', quiet=True)
|
| 9 |
+
stop_words = set(stopwords.words('english'))
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def clean_text(text):
|
| 12 |
"""Clean and preprocess text similar to training preprocessing"""
|
|
|
|
| 17 |
words = [w for w in words if w not in stop_words] # Remove stopwords
|
| 18 |
return ' '.join(words) # Join words back
|
| 19 |
|
| 20 |
+
# Load model and vectorizer
|
| 21 |
+
with open("model.pkl", "rb") as f:
|
| 22 |
+
model = pickle.load(f)
|
| 23 |
+
with open("vectorizer.pkl", "rb") as f:
|
| 24 |
+
vectorizer = pickle.load(f)
|
| 25 |
+
|
| 26 |
+
def analyze_review(review_text):
|
| 27 |
+
if not review_text.strip():
|
| 28 |
+
return "⚠️ Please enter a review."
|
| 29 |
try:
|
| 30 |
+
cleaned_text = clean_text(review_text)
|
| 31 |
+
text_vectorized = vectorizer.transform([cleaned_text])
|
| 32 |
+
prediction = model.predict(text_vectorized)[0]
|
| 33 |
+
if prediction == 1:
|
| 34 |
+
return "✅ Positive Review"
|
| 35 |
+
else:
|
| 36 |
+
return "❌ Negative Review"
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
+
return f"Error during prediction: {e}"
|
|
|
|
| 39 |
|
| 40 |
+
# Create Gradio interface
|
| 41 |
+
iface = gr.Interface(
|
| 42 |
+
fn=analyze_review,
|
| 43 |
+
inputs=gr.Textbox(lines=6, placeholder="Enter your movie review here..."),
|
| 44 |
+
outputs="text",
|
| 45 |
+
title="🎬 IMDB Sentiment Analysis",
|
| 46 |
+
description="Enter a movie review and get a Positive/Negative sentiment prediction."
|
| 47 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
+
iface.launch()
|