4loop commited on
Commit
783f1c5
·
0 Parent(s):

Initial commit: Clean IMDB Sentiment Analysis project

Browse files
Files changed (6) hide show
  1. .dockerignore +23 -0
  2. .gitignore +61 -0
  3. Dockerfile +31 -0
  4. app.py +160 -0
  5. requirements.txt +6 -0
  6. train.py +139 -0
.dockerignore ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__
2
+ *.pyc
3
+ *.pyo
4
+ *.pyd
5
+ .Python
6
+ env/
7
+ venv/
8
+ pip-log.txt
9
+ pip-delete-this-directory.txt
10
+ .tox
11
+ .coverage
12
+ .coverage.*
13
+ .cache
14
+ nosetests.xml
15
+ coverage.xml
16
+ *.cover
17
+ *.log
18
+ .git
19
+ .mypy_cache
20
+ .pytest_cache
21
+ .hypothesis
22
+ .DS_Store
23
+ mlruns/
.gitignore ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Virtual environment
2
+ imdb/
3
+ venv/
4
+ env/
5
+ ENV/
6
+ .env/
7
+
8
+ # Python
9
+ __pycache__/
10
+ *.py[cod]
11
+ *$py.class
12
+ *.so
13
+ .Python
14
+ build/
15
+ develop-eggs/
16
+ dist/
17
+ downloads/
18
+ eggs/
19
+ .eggs/
20
+ lib/
21
+ lib64/
22
+ parts/
23
+ sdist/
24
+ var/
25
+ wheels/
26
+ pip-wheel-metadata/
27
+ share/python-wheels/
28
+ *.egg-info/
29
+ .installed.cfg
30
+ *.egg
31
+ MANIFEST
32
+
33
+ # MLflow
34
+ mlruns/
35
+ mlartifacts/
36
+
37
+ # IDE
38
+ .vscode/
39
+ .idea/
40
+ *.swp
41
+ *.swo
42
+
43
+ # OS
44
+ .DS_Store
45
+ Thumbs.db
46
+
47
+ # Large model files (if you want to exclude them)
48
+ model/saved_model.keras
49
+ model/word_index.json
50
+ model/reverse_word_index.json
51
+
52
+ # Jupyter
53
+ .ipynb_checkpoints/
54
+
55
+ # Logs
56
+ *.log
57
+ logs/
58
+
59
+ # Temporary files
60
+ *.tmp
61
+ *.temp
Dockerfile ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use Python 3.10 slim image
2
+ FROM python:3.10-slim
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Install system dependencies
8
+ RUN apt-get update && apt-get install -y \
9
+ gcc \
10
+ && rm -rf /var/lib/apt/lists/*
11
+
12
+ # Copy requirements and install Python dependencies
13
+ COPY requirements.txt .
14
+ RUN pip install --upgrade pip && pip install -r requirements.txt
15
+
16
+ # Copy application files
17
+ COPY train.py .
18
+ COPY app.py .
19
+
20
+ # Create model directory (will be populated when training)
21
+ RUN mkdir -p model
22
+
23
+ # Expose port for Gradio
24
+ EXPOSE 7860
25
+
26
+ # Health check
27
+ HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
28
+ CMD python -c "import requests; requests.get('http://localhost:7860', timeout=10)"
29
+
30
+ # Start the application
31
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,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
+ # ---------------------------
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
+ )
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ tensorflow>=2.12.0
2
+ mlflow>=2.4.0
3
+ gradio>=3.0
4
+ scikit-learn
5
+ numpy
6
+ protobuf>=3.20.0,<4.0.0
train.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import mlflow
4
+ import mlflow.keras
5
+ import tensorflow as tf
6
+ from tensorflow import keras
7
+ from tensorflow.keras.datasets import imdb
8
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
9
+
10
+ # ---------------------------
11
+ # 1. Hyperparameters
12
+ # ---------------------------
13
+ vocab_size = 10000
14
+ maxlen = 200
15
+ embedding_dim = 16
16
+ epochs = 3 # Reduced for faster training
17
+ batch_size = 64
18
+
19
+ # ---------------------------
20
+ # 2. Load and preprocess data
21
+ # ---------------------------
22
+ print("Loading IMDB dataset...")
23
+ (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=vocab_size)
24
+
25
+ print("Padding sequences...")
26
+ x_train = pad_sequences(x_train, maxlen=maxlen)
27
+ x_test = pad_sequences(x_test, maxlen=maxlen)
28
+
29
+ print("Preparing word index...")
30
+ # Get and adjust word index (IMDB specific)
31
+ word_index = imdb.get_word_index()
32
+ word_index = {k:(v+3) for k,v in word_index.items()}
33
+ word_index["<PAD>"] = 0
34
+ word_index["<START>"] = 1
35
+ word_index["<UNK>"] = 2 # Out-of-vocabulary
36
+ word_index["<UNUSED>"] = 3
37
+
38
+ # Reverse word index for decoding
39
+ reverse_word_index = {v: k for k, v in word_index.items()}
40
+
41
+ # ---------------------------
42
+ # 3. Build model
43
+ # ---------------------------
44
+ print("Building model...")
45
+ model = keras.Sequential([
46
+ keras.layers.Embedding(vocab_size, embedding_dim, input_length=maxlen),
47
+ keras.layers.GlobalAveragePooling1D(), # Better than Flatten for sequences
48
+ keras.layers.Dense(16, activation="relu"),
49
+ keras.layers.Dropout(0.5),
50
+ keras.layers.Dense(1, activation="sigmoid")
51
+ ])
52
+
53
+ model.compile(
54
+ optimizer="adam",
55
+ loss="binary_crossentropy",
56
+ metrics=["accuracy"]
57
+ )
58
+
59
+ print(model.summary())
60
+
61
+ # ---------------------------
62
+ # 4. Train + MLflow logging
63
+ # ---------------------------
64
+ print("Starting training with MLflow logging...")
65
+
66
+ # Set MLflow tracking (local by default)
67
+ mlflow.set_tracking_uri("file:./mlruns") # Local storage
68
+ mlflow.set_experiment("imdb-sentiment")
69
+
70
+ with mlflow.start_run() as run:
71
+ run_id = run.info.run_id
72
+ print(f"✅ Training started. Run ID: {run_id}")
73
+
74
+ # Log parameters
75
+ mlflow.log_params({
76
+ "vocab_size": vocab_size,
77
+ "embedding_dim": embedding_dim,
78
+ "maxlen": maxlen,
79
+ "epochs": epochs,
80
+ "batch_size": batch_size,
81
+ "model_type": "Embedding+GlobalAveragePooling"
82
+ })
83
+
84
+ # Train model
85
+ history = model.fit(
86
+ x_train, y_train,
87
+ epochs=epochs,
88
+ batch_size=batch_size,
89
+ validation_split=0.2,
90
+ verbose=1
91
+ )
92
+
93
+ # Evaluate
94
+ test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
95
+ print(f"✅ Test Loss: {test_loss:.4f}, Test Accuracy: {test_accuracy:.4f}")
96
+
97
+ # Log metrics
98
+ mlflow.log_metrics({
99
+ "test_loss": test_loss,
100
+ "test_accuracy": test_accuracy
101
+ })
102
+
103
+ # Log final training metrics
104
+ final_train_acc = history.history['accuracy'][-1]
105
+ final_val_acc = history.history['val_accuracy'][-1]
106
+ mlflow.log_metrics({
107
+ "final_train_accuracy": final_train_acc,
108
+ "final_val_accuracy": final_val_acc
109
+ })
110
+
111
+ # Log model
112
+ mlflow.keras.log_model(
113
+ model,
114
+ "model",
115
+ registered_model_name="IMDBSentimentModel"
116
+ )
117
+
118
+ # ---------------------------
119
+ # 5. Save artifacts for deployment
120
+ # ---------------------------
121
+ os.makedirs("model", exist_ok=True)
122
+
123
+ # Save model in Keras format
124
+ model.save("model/saved_model.keras")
125
+
126
+ # Save word index
127
+ with open("model/word_index.json", "w") as f:
128
+ json.dump(word_index, f)
129
+
130
+ # Save reverse word index for text generation
131
+ with open("model/reverse_word_index.json", "w") as f:
132
+ json.dump(reverse_word_index, f)
133
+
134
+ # Log artifacts
135
+ mlflow.log_artifacts("model", artifact_path="model")
136
+
137
+ print("✅ Training complete!")
138
+ print("✅ Model saved locally in './model/' directory")
139
+ print("✅ Model registered with MLflow as 'IMDBSentimentModel'")