File size: 5,067 Bytes
783f1c5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | import gradio as gr
import tensorflow as tf
import json
import numpy as np
import os
print("π Starting IMDB Sentiment Analysis App...")
# ---------------------------
# 1. Configuration
# ---------------------------
MAX_LEN = 200
VOCAB_SIZE = 10000
# ---------------------------
# 2. Load model and artifacts
# ---------------------------
def load_resources():
"""Load model and word index with error handling"""
try:
# Try loading the local model first
print("Loading model from local directory...")
model = tf.keras.models.load_model("model/saved_model.keras")
print("β
Model loaded successfully!")
except Exception as e:
print(f"β Error loading local model: {e}")
print("Please run train.py first to train and save the model.")
return None, None
try:
# Load word index
with open("model/word_index.json", "r") as f:
word_index = json.load(f)
print("β
Word index loaded successfully!")
return model, word_index
except Exception as e:
print(f"β Error loading word index: {e}")
return model, None
model, word_index = load_resources()
if model is None:
print("β Failed to load model. Please train the model first.")
exit(1)
# ---------------------------
# 3. Text preprocessing
# ---------------------------
def decode_review(encoded_review):
"""Decode encoded review back to text (for debugging)"""
if not word_index:
return "Word index not available"
reverse_word_index = {v: k for k, v in word_index.items()}
return ' '.join([reverse_word_index.get(i, '?') for i in encoded_review if i not in [0, 1, 2, 3]])
def preprocess_text(text, max_len=MAX_LEN):
"""Convert text to sequence of integers"""
if word_index is None:
raise ValueError("Word index not loaded")
# Clean text
text = text.lower().replace('<br />', ' ').replace('/', ' ')
# Simple tokenization
tokens = text.split()
# Convert words to integers using word_index
# Note: IMDB dataset uses specific indexing (words start from 4)
sequence = []
for word in tokens:
# IMDB specific: word indices are offset by 3
word_id = word_index.get(word, 2) # 2 = <UNK> token
if word_id >= 4: # Only include actual words (not special tokens)
sequence.append(word_id)
# Truncate or pad sequence
sequence = sequence[:max_len]
sequence = sequence + [0] * (max_len - len(sequence))
return np.array([sequence])
# ---------------------------
# 4. Prediction function
# ---------------------------
def predict_sentiment(text):
"""Predict sentiment for given text"""
if text.strip() == "":
return {"Please enter text": 0.0}
try:
# Preprocess text
processed_text = preprocess_text(text)
# Make prediction
prediction = model.predict(processed_text, verbose=0)[0][0]
# Format output
if prediction > 0.5:
return {"Positive π": float(prediction)}
else:
return {"Negative π": float(1 - prediction)}
except Exception as e:
print(f"Prediction error: {e}")
return {"Error in prediction": 0.0}
# ---------------------------
# 5. Gradio Interface
# ---------------------------
def create_interface():
"""Create and return Gradio interface"""
# Example reviews
examples = [
["This movie was absolutely fantastic! The acting was superb and the story was engaging from start to finish."],
["Terrible movie. Waste of time. Poor acting and boring storyline."],
["I loved this movie! The characters were well-developed and the cinematography was stunning."],
["Boring and predictable. I fell asleep halfway through."]
]
# Create interface
demo = gr.Interface(
fn=predict_sentiment,
inputs=gr.Textbox(
lines=3,
placeholder="Enter your movie review here...",
label="Movie Review"
),
outputs=gr.Label(
num_top_classes=2,
label="Sentiment Prediction"
),
examples=examples,
title="π¬ IMDB Movie Review Sentiment Analysis",
description=(
"This AI model analyzes movie reviews and predicts whether they have "
"**Positive** or **Negative** sentiment. The model was trained on the "
"IMDB movie review dataset.\n\n"
"**How to use:** Type a movie review in the text box and click Submit, "
"or try one of the examples below!"
),
theme="soft"
)
return demo
# ---------------------------
# 6. Main execution
# ---------------------------
if __name__ == "__main__":
print("π Starting Gradio web server...")
demo = create_interface()
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False # Set to True if you want a public link
) |