Upload NavyBayes.py
Browse files- NavyBayes.py +50 -61
NavyBayes.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
import firebase_admin
|
2 |
-
from firebase_admin import credentials, firestore
|
3 |
-
from joblib import dump, load
|
4 |
import datetime
|
5 |
-
|
6 |
-
from sklearn.
|
7 |
-
import
|
8 |
-
|
9 |
|
10 |
# التهيئة مرة واحدة فقط
|
11 |
if not firebase_admin._apps:
|
@@ -25,7 +25,7 @@ except Exception as e:
|
|
25 |
vectorizer = None
|
26 |
print(f"Model and vectorizer not found. You need to train the model. Error: {e}")
|
27 |
|
28 |
-
# 1. وظيفة لتحليل
|
29 |
def classify_and_store_message(message):
|
30 |
global model, vectorizer
|
31 |
try:
|
@@ -44,19 +44,13 @@ def classify_and_store_message(message):
|
|
44 |
}
|
45 |
|
46 |
# تخزين الرسالة في مجموعة Firestore حسب التصنيف
|
47 |
-
|
48 |
-
|
49 |
-
elif classification == "news_phishing":
|
50 |
-
db.collection('news').add(message_data)
|
51 |
-
elif classification == "advertisement_phishing":
|
52 |
-
db.collection('advertisement').add(message_data)
|
53 |
-
elif classification == "social_phishing":
|
54 |
-
db.collection('social').add(message_data)
|
55 |
|
56 |
# تخزين الرسالة في مجموعة 'all_messages' لجميع الرسائل
|
57 |
db.collection('all_messages').add(message_data)
|
58 |
|
59 |
-
# تخزين الرسالة في مجموعة 'recently_analyzed_messages'
|
60 |
db.collection('recently_analyzed_messages').add(message_data)
|
61 |
|
62 |
print(f"Message classified as {classification} and stored in Firestore.")
|
@@ -66,38 +60,37 @@ def classify_and_store_message(message):
|
|
66 |
print(f"Error classifying message: {e}")
|
67 |
return None
|
68 |
|
69 |
-
# 2.
|
70 |
-
def
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
repo_id="Noufy/https-api_inference.huggingface.comodels-Noufy-naive_bayes_sms",
|
92 |
-
repo_type="model"
|
93 |
-
)
|
94 |
-
|
95 |
-
print("Model and vectorizer uploaded successfully to Hugging Face.")
|
96 |
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
# 3.
|
101 |
def update_model_with_new_data(new_messages, new_labels):
|
102 |
global model, vectorizer
|
103 |
try:
|
@@ -129,36 +122,32 @@ def update_model_with_new_data(new_messages, new_labels):
|
|
129 |
except Exception as e:
|
130 |
print(f"Error updating model: {e}")
|
131 |
|
132 |
-
# تشغيل رفع النموذج إلى Hugging Face
|
133 |
-
upload_model_to_huggingface()
|
134 |
-
|
135 |
# 4. دالة لاختبار النظام
|
136 |
def test_system():
|
137 |
test_messages = [
|
138 |
-
"Win a free vacation now!",
|
139 |
"Breaking news: Major stock updates today.",
|
140 |
"Don't forget our meeting tomorrow at 10 AM.",
|
141 |
-
"Click here to secure your bank account
|
142 |
-
"Exclusive offers just for you
|
143 |
]
|
144 |
|
145 |
for msg in test_messages:
|
146 |
-
|
147 |
-
|
148 |
|
149 |
# 5. وظيفة للتصحيح اليدوي
|
150 |
def correct_classification(message_id, correct_label):
|
151 |
try:
|
152 |
-
# جلب الرسالة من
|
153 |
-
message_ref = db.collection('
|
154 |
message_data = message_ref.get().to_dict()
|
155 |
|
156 |
if not message_data:
|
157 |
print("Message not found.")
|
158 |
return
|
159 |
|
160 |
-
# تحديث التصنيف في
|
161 |
-
message_data['classification'] = correct_label
|
162 |
message_ref.update({'classification': correct_label})
|
163 |
|
164 |
# إضافة البيانات إلى نموذج التدريب الجديد
|
@@ -167,5 +156,5 @@ def correct_classification(message_id, correct_label):
|
|
167 |
except Exception as e:
|
168 |
print(f"Error correcting classification: {e}")
|
169 |
|
170 |
-
#
|
171 |
-
|
|
|
1 |
+
import firebase_admin # type: ignore
|
2 |
+
from firebase_admin import credentials, firestore # type: ignore
|
3 |
+
from joblib import dump, load # type: ignore
|
4 |
import datetime
|
5 |
+
import re
|
6 |
+
from sklearn.feature_extraction.text import TfidfVectorizer # type: ignore
|
7 |
+
from sklearn.naive_bayes import MultinomialNB # type: ignore
|
8 |
+
import pandas as pd # type: ignore
|
9 |
|
10 |
# التهيئة مرة واحدة فقط
|
11 |
if not firebase_admin._apps:
|
|
|
25 |
vectorizer = None
|
26 |
print(f"Model and vectorizer not found. You need to train the model. Error: {e}")
|
27 |
|
28 |
+
# 1. وظيفة لتحليل النصوص وتصنيفها
|
29 |
def classify_and_store_message(message):
|
30 |
global model, vectorizer
|
31 |
try:
|
|
|
44 |
}
|
45 |
|
46 |
# تخزين الرسالة في مجموعة Firestore حسب التصنيف
|
47 |
+
collection_name = classification.split('_')[0] # استخدام الجزء الأول من التصنيف كاسم المجموعة
|
48 |
+
db.collection(collection_name).add(message_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# تخزين الرسالة في مجموعة 'all_messages' لجميع الرسائل
|
51 |
db.collection('all_messages').add(message_data)
|
52 |
|
53 |
+
# تخزين الرسالة في مجموعة 'recently_analyzed_messages'
|
54 |
db.collection('recently_analyzed_messages').add(message_data)
|
55 |
|
56 |
print(f"Message classified as {classification} and stored in Firestore.")
|
|
|
60 |
print(f"Error classifying message: {e}")
|
61 |
return None
|
62 |
|
63 |
+
# 2. وظيفة لتحليل النصوص المدخلة
|
64 |
+
def analyze_input_text():
|
65 |
+
print("\n--- SMS Classification and Link Analysis Tool ---")
|
66 |
+
while True:
|
67 |
+
user_input = input("Enter a message to classify (or type 'exit' to quit): ").strip()
|
68 |
+
if user_input.lower() == 'exit':
|
69 |
+
print("Exiting the tool. Goodbye!")
|
70 |
+
break
|
71 |
+
|
72 |
+
# استخراج الروابط من النص المدخل
|
73 |
+
links = re.findall(r'(https?://[^\s]+)', user_input)
|
74 |
+
if links:
|
75 |
+
print(f"Detected links: {links}")
|
76 |
+
# تحليل الروابط (يمكن تطوير التحليل ليشمل أدوات أو خدمات خارجية)
|
77 |
+
for link in links:
|
78 |
+
# افتراض تحليل بسيط (يمكن تحسينه لاحقًا)
|
79 |
+
if "secure" in link or "safe" in link:
|
80 |
+
print(f"Link '{link}' appears safe.")
|
81 |
+
else:
|
82 |
+
print(f"Link '{link}' might be suspicious.")
|
83 |
+
else:
|
84 |
+
print("No links detected in the message.")
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
+
# تصنيف الرسالة
|
87 |
+
classification = classify_and_store_message(user_input)
|
88 |
+
if classification:
|
89 |
+
print(f"Message classified as: {classification}")
|
90 |
+
else:
|
91 |
+
print("Unable to classify the message. Please try again.")
|
92 |
|
93 |
+
# 3. دالة لتحديث النموذج مع بيانات جديدة
|
94 |
def update_model_with_new_data(new_messages, new_labels):
|
95 |
global model, vectorizer
|
96 |
try:
|
|
|
122 |
except Exception as e:
|
123 |
print(f"Error updating model: {e}")
|
124 |
|
|
|
|
|
|
|
125 |
# 4. دالة لاختبار النظام
|
126 |
def test_system():
|
127 |
test_messages = [
|
128 |
+
"Win a free vacation now! Visit https://spam-link.com",
|
129 |
"Breaking news: Major stock updates today.",
|
130 |
"Don't forget our meeting tomorrow at 10 AM.",
|
131 |
+
"Click here to secure your bank account: https://phishing-link.com",
|
132 |
+
"Exclusive offers just for you! Buy now at https://ad-link.com"
|
133 |
]
|
134 |
|
135 |
for msg in test_messages:
|
136 |
+
print(f"\nAnalyzing message: {msg}")
|
137 |
+
analyze_input_text(msg)
|
138 |
|
139 |
# 5. وظيفة للتصحيح اليدوي
|
140 |
def correct_classification(message_id, correct_label):
|
141 |
try:
|
142 |
+
# جلب الرسالة من Firestore
|
143 |
+
message_ref = db.collection('all_messages').document(message_id)
|
144 |
message_data = message_ref.get().to_dict()
|
145 |
|
146 |
if not message_data:
|
147 |
print("Message not found.")
|
148 |
return
|
149 |
|
150 |
+
# تحديث التصنيف في Firestore
|
|
|
151 |
message_ref.update({'classification': correct_label})
|
152 |
|
153 |
# إضافة البيانات إلى نموذج التدريب الجديد
|
|
|
156 |
except Exception as e:
|
157 |
print(f"Error correcting classification: {e}")
|
158 |
|
159 |
+
# تشغيل تحليل النصوص
|
160 |
+
analyze_input_text()
|