Update app.py
Browse files
app.py
CHANGED
|
@@ -1,160 +1,122 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
import json
|
| 4 |
-
import numpy as np
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
print("π Starting IMDB Sentiment Analysis App...")
|
| 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 |
-
model,
|
| 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 |
-
except Exception as e:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# Create interface
|
| 124 |
-
demo = gr.Interface(
|
| 125 |
-
fn=predict_sentiment,
|
| 126 |
-
inputs=gr.Textbox(
|
| 127 |
-
lines=3,
|
| 128 |
-
placeholder="Enter your movie review here...",
|
| 129 |
-
label="Movie Review"
|
| 130 |
-
),
|
| 131 |
-
outputs=gr.Label(
|
| 132 |
-
num_top_classes=2,
|
| 133 |
-
label="Sentiment Prediction"
|
| 134 |
-
),
|
| 135 |
-
examples=examples,
|
| 136 |
-
title="π¬ IMDB Movie Review Sentiment Analysis",
|
| 137 |
-
description=(
|
| 138 |
-
"This AI model analyzes movie reviews and predicts whether they have "
|
| 139 |
-
"**Positive** or **Negative** sentiment. The model was trained on the "
|
| 140 |
-
"IMDB movie review dataset.\n\n"
|
| 141 |
-
"**How to use:** Type a movie review in the text box and click Submit, "
|
| 142 |
-
"or try one of the examples below!"
|
| 143 |
-
),
|
| 144 |
-
theme="soft"
|
| 145 |
-
)
|
| 146 |
-
|
| 147 |
-
return demo
|
| 148 |
-
|
| 149 |
-
# ---------------------------
|
| 150 |
-
# 6. Main execution
|
| 151 |
-
# ---------------------------
|
| 152 |
-
if __name__ == "__main__":
|
| 153 |
-
print("π Starting Gradio web server...")
|
| 154 |
-
|
| 155 |
-
demo = create_interface()
|
| 156 |
-
demo.launch(
|
| 157 |
-
server_name="0.0.0.0",
|
| 158 |
-
server_port=7860,
|
| 159 |
-
share=False # Set to True if you want a public link
|
| 160 |
-
)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import json
|
| 4 |
+
import numpy as np
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
print("π Starting IMDB Sentiment Analysis App...")
|
| 8 |
+
|
| 9 |
+
# Configuration
|
| 10 |
+
MAX_LEN = 200
|
| 11 |
+
VOCAB_SIZE = 10000
|
| 12 |
+
|
| 13 |
+
def train_model():
|
| 14 |
+
"""Train the model if not exists"""
|
| 15 |
+
print("Training model...")
|
| 16 |
+
|
| 17 |
+
# Import training components
|
| 18 |
+
from tensorflow.keras.datasets import imdb
|
| 19 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 20 |
+
from tensorflow.keras.models import Sequential
|
| 21 |
+
from tensorflow.keras.layers import Embedding, GlobalAveragePooling1D, Dense, Dropout
|
| 22 |
+
|
| 23 |
+
# Load and preprocess data
|
| 24 |
+
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=VOCAB_SIZE)
|
| 25 |
+
x_train = pad_sequences(x_train, maxlen=MAX_LEN)
|
| 26 |
+
x_test = pad_sequences(x_test, maxlen=MAX_LEN)
|
| 27 |
+
|
| 28 |
+
# Build model
|
| 29 |
+
model = Sequential([
|
| 30 |
+
Embedding(VOCAB_SIZE, 16, input_length=MAX_LEN),
|
| 31 |
+
GlobalAveragePooling1D(),
|
| 32 |
+
Dense(16, activation="relu"),
|
| 33 |
+
Dropout(0.5),
|
| 34 |
+
Dense(1, activation="sigmoid")
|
| 35 |
+
])
|
| 36 |
+
|
| 37 |
+
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
|
| 38 |
+
|
| 39 |
+
# Train quickly with 1 epoch for demo
|
| 40 |
+
model.fit(x_train, y_train, epochs=1, batch_size=32, validation_split=0.2, verbose=1)
|
| 41 |
+
|
| 42 |
+
# Save model
|
| 43 |
+
os.makedirs("model", exist_ok=True)
|
| 44 |
+
model.save("model/saved_model.keras")
|
| 45 |
+
|
| 46 |
+
# Save word index
|
| 47 |
+
word_index = imdb.get_word_index()
|
| 48 |
+
word_index = {k:(v+3) for k,v in word_index.items()}
|
| 49 |
+
word_index["<PAD>"] = 0
|
| 50 |
+
word_index["<START>"] = 1
|
| 51 |
+
word_index["<UNK>"] = 2
|
| 52 |
+
|
| 53 |
+
with open("model/word_index.json", "w") as f:
|
| 54 |
+
json.dump(word_index, f)
|
| 55 |
+
|
| 56 |
+
print("β
Model trained and saved!")
|
| 57 |
+
return model, word_index
|
| 58 |
+
|
| 59 |
+
def load_or_train_model():
|
| 60 |
+
"""Load existing model or train new one"""
|
| 61 |
+
try:
|
| 62 |
+
# Try loading existing model
|
| 63 |
+
model = tf.keras.models.load_model("model/saved_model.keras")
|
| 64 |
+
with open("model/word_index.json", "r") as f:
|
| 65 |
+
word_index = json.load(f)
|
| 66 |
+
print("β
Model loaded successfully!")
|
| 67 |
+
return model, word_index
|
| 68 |
+
except:
|
| 69 |
+
# Train new model
|
| 70 |
+
print("β Model not found. Training new model...")
|
| 71 |
+
return train_model()
|
| 72 |
+
|
| 73 |
+
# Load or train model
|
| 74 |
+
model, word_index = load_or_train_model()
|
| 75 |
+
|
| 76 |
+
def preprocess_text(text, max_len=MAX_LEN):
|
| 77 |
+
"""Convert text to sequence"""
|
| 78 |
+
text = text.lower().replace('<br />', ' ')
|
| 79 |
+
tokens = text.split()
|
| 80 |
+
|
| 81 |
+
sequence = []
|
| 82 |
+
for word in tokens:
|
| 83 |
+
word_id = word_index.get(word, 2) # 2 = <UNK>
|
| 84 |
+
if word_id >= 4: # Only actual words
|
| 85 |
+
sequence.append(word_id)
|
| 86 |
+
|
| 87 |
+
sequence = sequence[:max_len]
|
| 88 |
+
sequence = sequence + [0] * (max_len - len(sequence))
|
| 89 |
+
return np.array([sequence])
|
| 90 |
+
|
| 91 |
+
def predict_sentiment(text):
|
| 92 |
+
"""Predict sentiment"""
|
| 93 |
+
if text.strip() == "":
|
| 94 |
+
return {"Please enter text": 0.0}
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
processed_text = preprocess_text(text)
|
| 98 |
+
prediction = model.predict(processed_text, verbose=0)[0][0]
|
| 99 |
+
|
| 100 |
+
if prediction > 0.5:
|
| 101 |
+
return {"Positive π": float(prediction)}
|
| 102 |
+
else:
|
| 103 |
+
return {"Negative π": float(1 - prediction)}
|
| 104 |
+
except Exception as e:
|
| 105 |
+
return {"Error in prediction": 0.0}
|
| 106 |
+
|
| 107 |
+
# Create Gradio interface
|
| 108 |
+
demo = gr.Interface(
|
| 109 |
+
fn=predict_sentiment,
|
| 110 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter your movie review..."),
|
| 111 |
+
outputs=gr.Label(num_top_classes=2),
|
| 112 |
+
examples=[
|
| 113 |
+
["This movie was absolutely fantastic! The acting was superb."],
|
| 114 |
+
["Terrible movie. Waste of time. Poor acting and boring storyline."],
|
| 115 |
+
["I loved this movie! The characters were well-developed."],
|
| 116 |
+
],
|
| 117 |
+
title="π¬ IMDB Sentiment Analysis",
|
| 118 |
+
description="Enter a movie review to analyze sentiment (Positive/Negative). The model will train on first run (takes ~2 minutes)."
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|