sivasanjay6264 commited on
Commit
4c60c5a
·
verified ·
1 Parent(s): e29ad10

Upload 4 files

Browse files
Files changed (4) hide show
  1. app (3).py +59 -0
  2. dataset.csv +0 -0
  3. requirements.txt +5 -0
  4. spam_detection_model.h5 +3 -0
app (3).py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """app.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1P96bh2DkRkgFJiX-Vvr3WeI6gsfDMStd
8
+ """
9
+
10
+ import tensorflow as tf
11
+ import gradio as gr
12
+ import numpy as np
13
+ from tensorflow.keras.preprocessing.text import Tokenizer
14
+ from tensorflow.keras.preprocessing.sequence import pad_sequences
15
+
16
+ # 1. Load the trained model
17
+ model = tf.keras.models.load_model("spam_detection_model.h5")
18
+
19
+ # 2. Rebuild the tokenizer inside code (no external file needed)
20
+ # These are sample training-like sentences used to refit the tokenizer
21
+ sample_messages = [
22
+ "Congratulations! You've won a prize.",
23
+ "Free entry in a contest!",
24
+ "Call now to claim your reward!",
25
+ "URGENT: Your number was selected!",
26
+ "Meeting at 10 AM tomorrow.",
27
+ "Lunch at 1 PM?",
28
+ "Please submit the report by tonight.",
29
+ "Let's catch up for the project discussion.",
30
+ "Click here for a special discount!",
31
+ "This is not spam, just a reminder.",
32
+ ]
33
+
34
+ # 3. Tokenizer setup (same as used during training)
35
+ tokenizer = Tokenizer(num_words=5000, oov_token="<OOV>")
36
+ tokenizer.fit_on_texts(sample_messages)
37
+
38
+ # 4. Set max sequence length
39
+ max_length = 100
40
+
41
+ # 5. Prediction function
42
+ def predict_spam(message):
43
+ seq = tokenizer.texts_to_sequences([message])
44
+ padded = pad_sequences(seq, maxlen=max_length, padding='post')
45
+ pred = float(model.predict(padded, verbose=0)[0])
46
+ label = "🔴 Spam" if pred > 0.5 else "🟢 Ham"
47
+ return f"{label} ({pred * 100:.2f}% confidence)"
48
+
49
+ # 6. Gradio Interface
50
+ iface = gr.Interface(
51
+ fn=predict_spam,
52
+ inputs=gr.Textbox(lines=3, placeholder="Enter a message to classify..."),
53
+ outputs="text",
54
+ title="📩 Spam or Ham Classifier",
55
+ description="This LSTM-based app classifies text messages as Spam or Ham. Built with TensorFlow and Gradio.",
56
+ theme="default"
57
+ )
58
+
59
+ iface.launch()
dataset.csv ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ pandas
2
+ numpy
3
+ tensorflow
4
+ scikit-learn
5
+ gradio
spam_detection_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e41ea0703d53b157c12ff1dcc8fb9188a197b32f4b224b3d800f6b92f2371da9
3
+ size 4301280