Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
274 changes: 54 additions & 220 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
# 🚀 Spam Detection System
🚀 Spam Detection System
A full-stack application that detects Spam / Smishing / Offensive content using Machine Learning. The system includes a Python-based ML engine, a Node.js backend, a React web interface, and a cross-platform React Native mobile application.

A full-stack application that detects **Spam / Smishing / Offensive content** using Machine Learning.
The system includes:

* 🧠 ML Model (Python)
* ⚡ Python API (Flask / FastAPI)
* 🌐 Node.js Backend
* 💻 React Web App
* 📱 React Native Mobile App (Android & iOS)

---

## 📌 Project Architecture

```
📌 Project Architecture
User Input (Web / Mobile)
React / React Native UI
Expand All @@ -23,79 +11,31 @@ Node.js Backend (API Gateway)
Python ML API (Model Inference)
Prediction (Spam / Ham / Offensive)
```

---

## 🧠 Machine Learning Model

### 📊 Dataset
⚙️ Environment Configuration
To run this project, you need to configure your environment variables for different platforms. Create a .env file in the root directory:

* CSV format:
Plaintext
# For Mobile (Expo)
EXPO_PUBLIC_ANDROIDAPI=http://<YOUR_PC_IP>:5000/predict
EXPO_PUBLIC_IOSAPI=http://<YOUR_PC_IP>:5000/predict

* `text` / `message`
* `label` (spam / ham / offensive)
# For Web
VITE_API_URI=http://localhost:5000/predict
Note: Replace <YOUR_PC_IP> with your machine's local IPv4 address (e.g., 192.168.100.50).

### ⚙️ Algorithms Used
🧠 Machine Learning Model
📊 Dataset
CSV format: text / label (spam / ham / offensive).

* Logistic Regression
* Naive Bayes
* Linear SVM (Best Accuracy)

### 📈 Performance

* Accuracy: ~97–98%
* Metrics:

* Precision
* Recall
* F1-score
* Confusion Matrix

---

### 🏋️ Model Training (Python)

```python
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
import pickle
⚙️ Algorithms Used
Logistic Regression, Naive Bayes, Linear SVM (Best Accuracy).

# Load dataset
X = df['text']
y = df['label']

# Vectorization
vectorizer = TfidfVectorizer()
X_vec = vectorizer.fit_transform(X)

# Model
model = LinearSVC()
model.fit(X_vec, y)

# Save
pickle.dump(model, open("model.pkl", "wb"))
pickle.dump(vectorizer, open("vectorizer.pkl", "wb"))
```

---

## 🐍 Python API (Flask)

### 📦 Install Dependencies

```bash
pip install flask scikit-learn
```

### 🚀 API Code

```python
🐍 Python API (Flask)
Python
from flask import Flask, request, jsonify
import pickle

app = Flask(__name__)

model = pickle.load(open("model.pkl", "rb"))
vectorizer = pickle.load(open("vectorizer.pkl", "rb"))

Expand All @@ -104,166 +44,60 @@ def predict():
data = request.json['text']
vec = vectorizer.transform([data])
prediction = model.predict(vec)[0]
return jsonify({"result": prediction})
return jsonify({"prediction": prediction})

if __name__ == "__main__":
app.run(port=5000)
```

---

## 🌐 Node.js Backend

### 📦 Install

```bash
npm install express axios cors
```

### ⚙️ Server Code

```javascript
const express = require("express");
const axios = require("axios");
const cors = require("cors");

const app = express();
app.use(cors());
app.use(express.json());
🌐 Node.js Backend
The Node.js server acts as an API gateway. Ensure your server is configured to listen on 0.0.0.0 for network accessibility.

JavaScript
// Ensure app.listen(PORT, '0.0.0.0', ...)
app.post("/predict", async (req, res) => {
try {
const response = await axios.post("http://localhost:5000/predict", {
text: req.body.text,
});
const response = await axios.post("http://localhost:5000/predict", { text: req.body.text });
res.json(response.data);
} catch (err) {
res.status(500).send("Error");
}
} catch (err) { res.status(500).send("Error"); }
});
📱 React Native App (Mobile)
We use a robust getApiUrl utility to handle dynamic environments across Web and Mobile platforms:

app.listen(3000, () => console.log("Node server running"));
```

---

## 💻 React Frontend

### 📦 Setup

```bash
npm create vite@latest
npm install axios
```

### ⚛️ Example Component

```javascript
import { useState } from "react";
import axios from "axios";

function App() {
const [text, setText] = useState("");
const [result, setResult] = useState("");

const handlePredict = async () => {
const res = await axios.post("http://localhost:3000/predict", { text });
setResult(res.data.result);
};

return (
<div>
<h1>Spam Detection</h1>
<input onChange={(e) => setText(e.target.value)} />
<button onClick={handlePredict}>Check</button>
<p>{result}</p>
</div>
);
}

export default App;
```
JavaScript
const getApiUrl = () => {
const defaultUrl = "http://192.168.100.50:5000/predict";
const androidUrl = process.env.EXPO_PUBLIC_ANDROIDAPI || defaultUrl;
const iosUrl = process.env.EXPO_PUBLIC_IOSAPI || defaultUrl;

if (Platform.OS === 'web') return (process.env as any).VITE_API_URI;
return Platform.OS === 'android' ? androidUrl : iosUrl;
};
🔐 Features
✅ Cross-Platform: Seamlessly works on Web, Android, and iOS.

---
✅ Dynamic Connectivity: Automatic API URL resolution.

## 📱 React Native App (Android & iOS)
✅ Real-time Detection: Immediate classification of input text.

### 📦 Setup
✅ Diagnostic Logging: Tracks history and classification results.

```bash
npx create-expo-app
npm install axios
```
🛠 Tech Stack
ML: Python, Scikit-learn, Flask

### 📲 Example Code
Backend: Node.js, Express, Axios

```javascript
import { useState } from "react";
import { View, Text, TextInput, Button } from "react-native";
import axios from "axios";
Frontend: React, React Native (Expo)

export default function App() {
const [text, setText] = useState("");
const [result, setResult] = useState("");
📌 Future Improvements
Use Deep Learning (LSTM / BERT).

const predict = async () => {
const res = await axios.post("http://YOUR_IP:3000/predict", { text });
setResult(res.data.result);
};
Multilingual Support.

return (
<View>
<Text>Spam Detection</Text>
<TextInput onChangeText={setText} />
<Button title="Check" onPress={predict} />
<Text>{result}</Text>
</View>
);
}
```
Database integration for persistent history.

---

## 🔐 Features

* ✅ Spam / Smishing Detection
* ✅ Offensive Content Classification
* ✅ Real-time Prediction API
* ✅ Cross-platform (Web + Mobile)
* ✅ Scalable Architecture

---

## 🛠 Tech Stack

* Python (ML + API)
* Scikit-learn
* Flask
* Node.js
* Express
* React
* React Native
* Axios

---

## 📌 Future Improvements

* Use Deep Learning (LSTM / BERT / CLIP)
* Multilingual Support
* More accuracy and advanced model
* Include Email predicton perfectly and add mobile numbers also to track

---

## 👨‍💻 Author
Enhanced tracking for phone numbers and suspicious links.

👨‍💻 Author
Aditya Sharma

---

## ⭐ Contribute

Feel free to fork, improve and contribute to this project!

---
⭐ Contribute
Feel free to fork, improve and contribute to this project!
32 changes: 21 additions & 11 deletions backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,38 @@ def home():
@app.route("/predict", methods=["POST"])
def predict():
try:
data = request.get_json()
data = request.get_json(silent=True)
if not data:
return jsonify({"error": "Invalid JSON layout structure"}), 400

text = data.get("text")
text = data.get("text", "").strip()
if not text:
return jsonify({"error": "No text provided"}), 400


# Feature transformations and predictions
text_vector = vectorizer.transform([text])


prediction = model.predict(text_vector)


final_output = label_encoder.inverse_transform(prediction)[0]

# Dynamic mapping for smishing pattern mutations
if str(final_output).lower() == "spam" and ("http" in text.lower() or "www." in text.lower()):
final_output = "smishing"

# IMPORTANT: Make sure these keys match what server.js expects!
return jsonify({
"status": "success",
"engine": "SVM Linear Kernel Engine",
"input": text,
"prediction": final_output
})
"prediction": str(final_output)
}), 200

except Exception as e:
return jsonify({"error": str(e)})
print(f"!!! CRASH IN FLASK PREDICT !!!: {str(e)}")
# Return proper JSON structural format with 500 error code
return jsonify({
"error": "Model prediction computation failed",
"details": str(e)
}), 500

if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000,debug=True)
app.run(host="127.0.0.1", port=8000, debug=True)
Loading