|
import firebase_admin
|
|
from firebase_admin import credentials, firestore
|
|
from joblib import dump, load
|
|
import datetime
|
|
import re
|
|
from sklearn.feature_extraction.text import TfidfVectorizer
|
|
from sklearn.naive_bayes import MultinomialNB
|
|
import pandas as pd
|
|
|
|
|
|
if not firebase_admin._apps:
|
|
|
|
cred = credentials.Certificate("D:/app-sentinel-7qnr19-firebase-adminsdk-kjmbe-f38e16a432.json")
|
|
firebase_admin.initialize_app(cred)
|
|
|
|
db = firestore.client()
|
|
|
|
|
|
try:
|
|
model = load('model.joblib')
|
|
vectorizer = load('vectorizer.joblib')
|
|
print("Model and vectorizer loaded successfully.")
|
|
except Exception as e:
|
|
model = None
|
|
vectorizer = None
|
|
print(f"Model and vectorizer not found. You need to train the model. Error: {e}")
|
|
|
|
|
|
def classify_and_store_message(message):
|
|
global model, vectorizer
|
|
try:
|
|
if not model or not vectorizer:
|
|
raise ValueError("Model or vectorizer not loaded. Train or load the model first.")
|
|
|
|
|
|
message_vector = vectorizer.transform([message])
|
|
classification = model.predict(message_vector)[0]
|
|
|
|
|
|
message_data = {
|
|
'text': message,
|
|
'classification': classification,
|
|
'timestamp': datetime.datetime.now()
|
|
}
|
|
|
|
|
|
collection_name = classification.split('_')[0]
|
|
db.collection(collection_name).add(message_data)
|
|
|
|
|
|
db.collection('all_messages').add(message_data)
|
|
|
|
|
|
db.collection('recently_analyzed_messages').add(message_data)
|
|
|
|
print(f"Message classified as {classification} and stored in Firestore.")
|
|
return classification
|
|
|
|
except Exception as e:
|
|
print(f"Error classifying message: {e}")
|
|
return None
|
|
|
|
|
|
def analyze_input_text():
|
|
print("\n--- SMS Classification and Link Analysis Tool ---")
|
|
while True:
|
|
user_input = input("Enter a message to classify (or type 'exit' to quit): ").strip()
|
|
if user_input.lower() == 'exit':
|
|
print("Exiting the tool. Goodbye!")
|
|
break
|
|
|
|
|
|
links = re.findall(r'(https?://[^\s]+)', user_input)
|
|
if links:
|
|
print(f"Detected links: {links}")
|
|
|
|
for link in links:
|
|
|
|
if "secure" in link or "safe" in link:
|
|
print(f"Link '{link}' appears safe.")
|
|
else:
|
|
print(f"Link '{link}' might be suspicious.")
|
|
else:
|
|
print("No links detected in the message.")
|
|
|
|
|
|
classification = classify_and_store_message(user_input)
|
|
if classification:
|
|
print(f"Message classified as: {classification}")
|
|
else:
|
|
print("Unable to classify the message. Please try again.")
|
|
|
|
|
|
def update_model_with_new_data(new_messages, new_labels):
|
|
global model, vectorizer
|
|
try:
|
|
|
|
data = {
|
|
'message': new_messages,
|
|
'label': new_labels
|
|
}
|
|
df_new = pd.DataFrame(data)
|
|
|
|
|
|
if vectorizer is None or model is None:
|
|
vectorizer = TfidfVectorizer()
|
|
X_new = vectorizer.fit_transform(df_new['message'])
|
|
else:
|
|
X_new = vectorizer.transform(df_new['message'])
|
|
|
|
|
|
y_new = df_new['label']
|
|
if model is None:
|
|
model = MultinomialNB()
|
|
model.partial_fit(X_new, y_new, classes=['spam_phishing', 'social_phishing', 'news_phishing', 'advertisement_phishing'])
|
|
|
|
|
|
dump(model, 'model.joblib')
|
|
dump(vectorizer, 'vectorizer.joblib')
|
|
print("Model updated and saved successfully.")
|
|
|
|
except Exception as e:
|
|
print(f"Error updating model: {e}")
|
|
|
|
|
|
def test_system():
|
|
test_messages = [
|
|
"Win a free vacation now! Visit https://spam-link.com",
|
|
"Breaking news: Major stock updates today.",
|
|
"Don't forget our meeting tomorrow at 10 AM.",
|
|
"Click here to secure your bank account: https://phishing-link.com",
|
|
"Exclusive offers just for you! Buy now at https://ad-link.com"
|
|
]
|
|
|
|
for msg in test_messages:
|
|
print(f"\nAnalyzing message: {msg}")
|
|
analyze_input_text(msg)
|
|
|
|
|
|
def correct_classification(message_id, correct_label):
|
|
try:
|
|
|
|
message_ref = db.collection('all_messages').document(message_id)
|
|
message_data = message_ref.get().to_dict()
|
|
|
|
if not message_data:
|
|
print("Message not found.")
|
|
return
|
|
|
|
|
|
message_ref.update({'classification': correct_label})
|
|
|
|
|
|
update_model_with_new_data([message_data['text']], [correct_label])
|
|
print(f"Message classification corrected to {correct_label} and model updated.")
|
|
except Exception as e:
|
|
print(f"Error correcting classification: {e}")
|
|
|
|
|
|
analyze_input_text()
|
|
|