Skip to content

Commit

Permalink
Adding files
Browse files Browse the repository at this point in the history
Adding files
  • Loading branch information
monicaavagyan committed Jul 17, 2024
0 parents commit 1f25368
Show file tree
Hide file tree
Showing 40 changed files with 31,556 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Message.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
**Test**

Spam Email Texts:

Congratulations! You've won a $1,000,000 prize. Claim it now!
You've been pre-approved for a credit card. Apply now!
Congratulations! You've won a luxury vacation for two. Click here to claim your prize!
You've been selected as the winner of a $500 Amazon gift card. Claim it today!
Free iPhone 11 giveaway for lucky winners like you. Enter now!
You've won a free cruise to the Caribbean. Call now to claim your ticket.
You're the lucky winner of a $1,000 shopping spree. Shop now!
Your credit score can be improved instantly. Click here for a free credit boost!
You've won a new car in our lottery! Claim your prize by clicking this link.
Congratulations! You've been selected as our grand prize winner. Claim your cash reward now!


Non-Spam (Ham) Email Texts:

Hi, it's John. Just wanted to check in and see how you're doing.
Reminder: Your meeting is scheduled for 2 PM today in Conference Room B.
Your order with Amazon has been shipped. Tracking details enclosed.
Invitation to our company's annual holiday party. RSVP requested.
Weekly newsletter: Stay updated on the latest industry news and trends.
Thank you for your recent purchase at our online store. Enjoy your products!
Weather update: Expect sunshine and clear skies this weekend.
Notification: Your monthly utility bill is now available for viewing online.
Congratulations on your work anniversary! Here's to many more years.
Happy birthday! We hope you have a fantastic day celebrating.
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Spam Mail Classification /EN
![Web page visual](image.png)

## Description

The **Spam Mail Classification** project is a web-based application that uses machine learning to classify emails as spam or ham. It features a Flask backend, a frontend created with HTML, CSS, and JavaScript, and a MySQL database for storing user data and email classifications.
![alt text](image-1.png)
### Features

- **Email Classification**: Categorizes incoming emails as spam or ham.
- **User Registration and Login**: Secure account creation and authentication.
- **Real-Time Email Classification**: Classifies emails in real time.
- **User Dashboard**: Users can view their email history and classifications.
- **Machine Learning Model**: Employs a trained model to classify emails.
- **Customization**: Users can configure spam filter settings.

## Technologies Used

- **Flask** (Python Web Framework): For the backend server.
- **HTML, CSS, and JavaScript** (Frontend): For the user interface.
- **MySQL** (Database): For storing user data and email classifications.
- **Machine Learning Libraries** (e.g., Scikit-Learn): Used to build and deploy the email classification model.

## Getting Started

To use the Spam Mail Classification app, follow these steps:

1. **Clone this Repository**: Get the project source code by cloning this repository to your local machine.

2. **Set Up the Flask Backend and MySQL Database**:
- Refer to the documentation or instructions provided in the code for setting up the Flask backend and MySQL database.

3. **Install Required Python Packages**:
- You'll need to install a few Python packages using pip. Open your terminal and run:

```bash
pip install Flask
pip install nltk
pip install mysql-connector-python
```

5. **Create a MySQL Database and Table**:
- Set up the MySQL database and table by running the following SQL commands in your MySQL server:

```sql
CREATE DATABASE smc;
```

```sql
USE smc;
```

```sql
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
full_name VARCHAR(255) NOT NULL,
username VARCHAR(255) UNIQUE NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
phone VARCHAR(15) NOT NULL,
password VARCHAR(255) NOT NULL
);
```

6. **Run the Flask App**:
- Start the Flask app by running the following command in your terminal:

```bash
python app.py
```

- Goto browser to open this website in Localhost:

```bash
http://127.0.0.1:5000/
```
The HTML template is taken from https://html.design/ .

132 changes: 132 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
from flask import Flask, render_template,request,redirect,url_for,session,flash
import pickle
import string
import nltk
from nltk.stem import PorterStemmer
import mysql.connector

app = Flask(__name__)
app.secret_key = '1c8073775dbc85a92ce20ebd44fd6a4fd832078f59ef16ec'
ps = PorterStemmer()
with open('vectorizer.pkl', 'rb') as file:
tfidf = pickle.load(file)
with open('model.pkl', 'rb') as file:
model = pickle.load(file)

nltk.download('punkt')

def transform_text(text):
text = text.lower()
text = nltk.word_tokenize(text)

y = []
for i in text:
# Check if the word is alphanumeric
if i.isalnum():
y.append(i)

text = y[:]
y.clear() #The list y is cleared to free up memory

for i in text:
y.append(ps.stem(i)) #Stemming Each Word:(արմատը)

return " ".join(y) #join the processed words back into a single string separated by spaces

#Defining database connction
db = mysql.connector.connect(
host = "localhost",
user = "root",
password = "ասդֆ",
database = "smc"
)

@app.route('/')
def home():
return render_template('home.html')

@app.route('/about')
def about():
return render_template('about.html')

@app.route('/index')
def index():
if 'user' in session: # Check if the 'user' session variable exists (i.e., the user is logged in)
return render_template('index.html')
else:
return redirect(url_for('signin')) # Redirect to the sign-in page if the user is not logged in

@app.route('/predict', methods=['POST'])
def predict():
input_email = request.form.get('message')
transformed_email =transform_text(input_email)
vector_input = tfidf.transform([transformed_email])
result = model.predict(vector_input)[0]
if result == 0:
prediction = 'Spam'
else:
prediction = 'Ham/not spam'
return render_template('result.html',prediction=prediction)

@app.route('/signin')
def signin():
if 'user' in session:
return redirect(url_for('index'))
return render_template('signin.html')

@app.route('/signup', methods=['GET'])
def register():
if request.method == 'POST':
full_name = request.form['full_name']
username = request.form['username']
email = request.form['email']
phone = request.form['phone']
password = request.form['password']

# Ensure the password and confirm_password match
confirm_password = request.form['confirm_password']
if password != confirm_password:
return "Password and confirm_password don't match."

# Insert data into MySQL
cur = db.cursor()
cur.execute("INSERT INTO users (full_name, username, emsil, phone, password) VALUES (%s, %s, %s, %s, %s)",
(full_name, username, email, phone, password))
db.commit()
cur.close()

flash('Registration successful','success')
return redirect('/signin')

return "Invalid request method"

@app.route('/login', methods=['POST'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
remember_me = request.form.get('remember_me')

# Query the database to check if the email and password match
cur = db.cursor()
cur.execute("SELECT * FROM users WHERE email = %s AND password = %s", (email,password))
user = cur.fetchone() #If a user with the given email and password is found, user will contain that user's data.
cur.close()

if user:
session['user'] = user

if remember_me:
session.permanent = True
return redirect(url_for('index'))
else:
return "Login failed.Check your email and password."
return "Invalid request method"

@app.route('/logout')
def logout():
session.pop('user',None)
return redirect(url_for('home')) # Redirect to the sign-in page after logging out

if __name__ == '__main__':
app.run(debug=True)
Binary file added image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1f25368

Please sign in to comment.