-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dependancy upgrades-update on pickle model-cython code updates-error-handling #11
Open
tim-githaiga
wants to merge
5
commits into
vaibhavbichave:master
Choose a base branch
from
tim-githaiga:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cache |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#importing required libraries | ||
|
||
from flask import Flask, request, render_template | ||
import numpy as np | ||
import pandas as pd | ||
|
@@ -9,31 +7,36 @@ | |
warnings.filterwarnings('ignore') | ||
from feature import FeatureExtraction | ||
|
||
file = open("pickle/model.pkl","rb") | ||
gbc = pickle.load(file) | ||
file.close() | ||
|
||
# Load the model | ||
try: | ||
with open("pickle/model.pkl", "rb") as file: | ||
gbc = pickle.load(file) | ||
except Exception as e: | ||
raise Exception(f"Error loading model: {e}") | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route("/", methods=["GET", "POST"]) | ||
def index(): | ||
if request.method == "POST": | ||
|
||
url = request.form["url"] | ||
obj = FeatureExtraction(url) | ||
x = np.array(obj.getFeaturesList()).reshape(1,30) | ||
|
||
y_pred =gbc.predict(x)[0] | ||
#1 is safe | ||
#-1 is unsafe | ||
y_pro_phishing = gbc.predict_proba(x)[0,0] | ||
y_pro_non_phishing = gbc.predict_proba(x)[0,1] | ||
# if(y_pred ==1 ): | ||
pred = "It is {0:.2f} % safe to go ".format(y_pro_phishing*100) | ||
return render_template('index.html',xx =round(y_pro_non_phishing,2),url=url ) | ||
return render_template("index.html", xx =-1) | ||
|
||
try: | ||
obj = FeatureExtraction(url) | ||
features = obj.getFeaturesList() | ||
x = np.array(features).reshape(1, -1) # Adjust reshape if needed | ||
|
||
if not hasattr(gbc, 'predict'): | ||
raise ValueError("Loaded object is not a valid model.") | ||
|
||
y_pred = gbc.predict(x)[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment for safe and unsafe |
||
y_pro_phishing = gbc.predict_proba(x)[0, 0] | ||
y_pro_non_phishing = gbc.predict_proba(x)[0, 1] | ||
|
||
pred = "It is {0:.2f} % safe to go ".format(y_pro_phishing * 100) | ||
return render_template('index.html', xx=round(y_pro_non_phishing, 2), url=url) | ||
except Exception as e: | ||
return str(e), 500 | ||
return render_template("index.html", xx=-1) | ||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) | ||
app.run(debug=True) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ <h3 id="prediction"></h3> | |
</div> | ||
</div> | ||
<br> | ||
<p>©2021 VAIBHAV BICHAVE</p> | ||
<p>©2024 TIM</p> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Vaibhav Bichave |
||
</div> | ||
|
||
<!-- JavaScript --> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# train.py | ||
|
||
import pandas as pd | ||
from sklearn.model_selection import train_test_split | ||
from sklearn.ensemble import RandomForestClassifier | ||
from sklearn.metrics import accuracy_score | ||
import joblib # To save the trained model | ||
|
||
def load_data(csv_file): | ||
"""Load the dataset from CSV file.""" | ||
df = pd.read_csv(csv_file) | ||
return df | ||
|
||
def preprocess_data(df): | ||
"""Preprocess the dataset, separating features and target.""" | ||
X = df.drop(columns=['class']) # Features | ||
y = df['class'] # Target | ||
return X, y | ||
|
||
def train_model(X_train, y_train): | ||
"""Train a RandomForestClassifier model.""" | ||
model = RandomForestClassifier(n_estimators=100, random_state=42) | ||
model.fit(X_train, y_train) | ||
return model | ||
|
||
def evaluate_model(model, X_test, y_test): | ||
"""Evaluate the model on test data.""" | ||
y_pred = model.predict(X_test) | ||
accuracy = accuracy_score(y_test, y_pred) | ||
print(f"Accuracy: {accuracy}") | ||
|
||
def save_model(model, model_file='pickle/model.pkl'): | ||
"""Save the trained model to a file.""" | ||
joblib.dump(model, model_file) | ||
print(f"Model saved as {model_file}") | ||
|
||
if __name__ == "__main__": | ||
# Load data | ||
csv_file = 'phishing.csv' | ||
df = load_data(csv_file) | ||
|
||
# Preprocess data | ||
X, y = preprocess_data(df) | ||
|
||
# Split data into train and test sets | ||
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | ||
|
||
# Train model | ||
model = train_model(X_train, y_train) | ||
|
||
# Evaluate model | ||
evaluate_model(model, X_test, y_test) | ||
|
||
# Save model | ||
save_model(model) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x = np.array(obj.getFeaturesList()).reshape(1, -1)