4loop commited on
Commit
9f907b6
Β·
verified Β·
1 Parent(s): 38293b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -78
app.py CHANGED
@@ -6,90 +6,35 @@ 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
 
@@ -104,18 +49,17 @@ def predict_sentiment(text):
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__":
 
6
 
7
  print("πŸš€ Starting IMDB Sentiment Analysis App...")
8
 
9
+ # Load pre-trained model and artifacts
10
+ try:
11
+ model = tf.keras.models.load_model('model/saved_model.keras')
12
+ with open('model/word_index.json', 'r') as f:
13
+ word_index = json.load(f)
14
+ print("βœ… Pre-trained model loaded successfully!")
15
+ except Exception as e:
16
+ print(f"❌ Error loading model: {e}")
17
+ exit(1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ MAX_LEN = 200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ def preprocess_text(text):
22
+ """Convert text to sequence using the trained model's preprocessing"""
23
+ text = text.lower().replace('<br />', ' ').replace('/', ' ')
24
  tokens = text.split()
25
 
26
  sequence = []
27
  for word in tokens:
28
+ word_id = word_index.get(word, 2) # 2 = UNK
29
+ if word_id >= 4:
30
  sequence.append(word_id)
31
 
32
+ sequence = sequence[:MAX_LEN]
33
+ sequence = sequence + [0] * (MAX_LEN - len(sequence))
34
  return np.array([sequence])
35
 
36
  def predict_sentiment(text):
37
+ """Predict sentiment using pre-trained model"""
38
  if text.strip() == "":
39
  return {"Please enter text": 0.0}
40
 
 
49
  except Exception as e:
50
  return {"Error in prediction": 0.0}
51
 
52
+ # Gradio interface
53
  demo = gr.Interface(
54
  fn=predict_sentiment,
55
+ inputs=gr.Textbox(lines=3, placeholder="Enter movie review..."),
56
  outputs=gr.Label(num_top_classes=2),
57
  examples=[
58
  ["This movie was absolutely fantastic! The acting was superb."],
59
  ["Terrible movie. Waste of time. Poor acting and boring storyline."],
 
60
  ],
61
+ title="🎬 IMDB Sentiment Analysis (Pre-trained Model)",
62
+ description="Uses a neural network pre-trained on IMDB reviews. Predictions are instant!"
63
  )
64
 
65
  if __name__ == "__main__":