aboalaa147's picture
Update app.py
610bd35 verified
import gradio as gr
import pickle
import re
import nltk
from nltk.corpus import stopwords
# Download stopwords if not already present
nltk.download('stopwords', quiet=True)
stop_words = set(stopwords.words('english'))
def clean_text(text):
"""Clean and preprocess text similar to training preprocessing"""
text = text.lower() # Convert to lowercase
text = re.sub(r'<.*?>', '', text) # Remove HTML tags
text = re.sub(r'[^a-z\s]', '', text) # Remove non-alphabetic characters
words = text.split() # Split into words
words = [w for w in words if w not in stop_words] # Remove stopwords
return ' '.join(words) # Join words back
# Load model and vectorizer
with open("model.pkl", "rb") as f:
model = pickle.load(f)
with open("vectorizer.pkl", "rb") as f:
vectorizer = pickle.load(f)
def analyze_review(review_text):
if not review_text.strip():
return "⚠️ Please enter a review."
try:
cleaned_text = clean_text(review_text)
text_vectorized = vectorizer.transform([cleaned_text])
prediction = model.predict(text_vectorized)[0]
if prediction == 1:
return "✅ Positive Review"
else:
return "❌ Negative Review"
except Exception as e:
return f"Error during prediction: {e}"
# Create Gradio interface
iface = gr.Interface(
fn=analyze_review,
inputs=gr.Textbox(lines=6, placeholder="Enter your movie review here..."),
outputs="text",
title="🎬 IMDB Sentiment Analysis",
description="Enter a movie review and get a Positive/Negative sentiment prediction."
)
if __name__ == "__main__":
iface.launch()