|
|
import gradio as gr |
|
|
import pickle |
|
|
import re |
|
|
import nltk |
|
|
from nltk.corpus import stopwords |
|
|
|
|
|
|
|
|
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() |
|
|
text = re.sub(r'<.*?>', '', text) |
|
|
text = re.sub(r'[^a-z\s]', '', text) |
|
|
words = text.split() |
|
|
words = [w for w in words if w not in stop_words] |
|
|
return ' '.join(words) |
|
|
|
|
|
|
|
|
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}" |
|
|
|
|
|
|
|
|
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() |
|
|
|