4loop commited on
Commit
d6b5e52
Β·
verified Β·
1 Parent(s): 4e04a65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -160
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
- # 1. Configuration
11
- # ---------------------------
12
- MAX_LEN = 200
13
- VOCAB_SIZE = 10000
14
-
15
- # ---------------------------
16
- # 2. Load model and artifacts
17
- # ---------------------------
18
- def load_resources():
19
- """Load model and word index with error handling"""
20
- try:
21
- # Try loading the local model first
22
- print("Loading model from local directory...")
23
- model = tf.keras.models.load_model("model/saved_model.keras")
24
- print("βœ… Model loaded successfully!")
25
- except Exception as e:
26
- print(f"❌ Error loading local model: {e}")
27
- print("Please run train.py first to train and save the model.")
28
- return None, None
29
-
30
- try:
31
- # Load word index
32
- with open("model/word_index.json", "r") as f:
33
- word_index = json.load(f)
34
- print("βœ… Word index loaded successfully!")
35
- return model, word_index
36
- except Exception as e:
37
- print(f"❌ Error loading word index: {e}")
38
- return model, None
39
-
40
- model, word_index = load_resources()
41
-
42
- if model is None:
43
- print("❌ Failed to load model. Please train the model first.")
44
- exit(1)
45
-
46
- # ---------------------------
47
- # 3. Text preprocessing
48
- # ---------------------------
49
- def decode_review(encoded_review):
50
- """Decode encoded review back to text (for debugging)"""
51
- if not word_index:
52
- return "Word index not available"
53
-
54
- reverse_word_index = {v: k for k, v in word_index.items()}
55
- return ' '.join([reverse_word_index.get(i, '?') for i in encoded_review if i not in [0, 1, 2, 3]])
56
-
57
- def preprocess_text(text, max_len=MAX_LEN):
58
- """Convert text to sequence of integers"""
59
- if word_index is None:
60
- raise ValueError("Word index not loaded")
61
-
62
- # Clean text
63
- text = text.lower().replace('<br />', ' ').replace('/', ' ')
64
-
65
- # Simple tokenization
66
- tokens = text.split()
67
-
68
- # Convert words to integers using word_index
69
- # Note: IMDB dataset uses specific indexing (words start from 4)
70
- sequence = []
71
- for word in tokens:
72
- # IMDB specific: word indices are offset by 3
73
- word_id = word_index.get(word, 2) # 2 = <UNK> token
74
- if word_id >= 4: # Only include actual words (not special tokens)
75
- sequence.append(word_id)
76
-
77
- # Truncate or pad sequence
78
- sequence = sequence[:max_len]
79
- sequence = sequence + [0] * (max_len - len(sequence))
80
-
81
- return np.array([sequence])
82
-
83
- # ---------------------------
84
- # 4. Prediction function
85
- # ---------------------------
86
- def predict_sentiment(text):
87
- """Predict sentiment for given text"""
88
- if text.strip() == "":
89
- return {"Please enter text": 0.0}
90
-
91
- try:
92
- # Preprocess text
93
- processed_text = preprocess_text(text)
94
-
95
- # Make prediction
96
- prediction = model.predict(processed_text, verbose=0)[0][0]
97
-
98
- # Format output
99
- if prediction > 0.5:
100
- return {"Positive 😊": float(prediction)}
101
- else:
102
- return {"Negative 😞": float(1 - prediction)}
103
-
104
- except Exception as e:
105
- print(f"Prediction error: {e}")
106
- return {"Error in prediction": 0.0}
107
-
108
- # ---------------------------
109
- # 5. Gradio Interface
110
- # ---------------------------
111
- def create_interface():
112
- """Create and return Gradio interface"""
113
-
114
- # Example reviews
115
- examples = [
116
- ["This movie was absolutely fantastic! The acting was superb and the story was engaging from start to finish."],
117
- ["Terrible movie. Waste of time. Poor acting and boring storyline."],
118
- ["The film had some good moments but overall it was quite average. Not bad, but not great either."],
119
- ["I loved this movie! The characters were well-developed and the cinematography was stunning."],
120
- ["Boring and predictable. I fell asleep halfway through."]
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()