Noufy commited on
Commit
0d3352f
·
verified ·
1 Parent(s): 2041b70

Upload NavyBayes.py

Browse files
Files changed (1) hide show
  1. NavyBayes.py +171 -0
NavyBayes.py ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import firebase_admin
2
+ from firebase_admin import credentials, firestore
3
+ from joblib import dump, load
4
+ import datetime
5
+ from sklearn.feature_extraction.text import TfidfVectorizer
6
+ from sklearn.naive_bayes import MultinomialNB
7
+ import pandas as pd
8
+ from huggingface_hub import HfApi
9
+
10
+ # التهيئة مرة واحدة فقط
11
+ if not firebase_admin._apps:
12
+ # تأكد من وضع المسار الصحيح لملف التوثيق Firebase
13
+ cred = credentials.Certificate("D:/app-sentinel-7qnr19-firebase-adminsdk-kjmbe-f38e16a432.json")
14
+ firebase_admin.initialize_app(cred)
15
+
16
+ db = firestore.client()
17
+
18
+ # تحميل النموذج الحالي والمحول
19
+ try:
20
+ model = load('model.joblib')
21
+ vectorizer = load('vectorizer.joblib')
22
+ print("Model and vectorizer loaded successfully.")
23
+ except Exception as e:
24
+ model = None
25
+ vectorizer = None
26
+ print(f"Model and vectorizer not found. You need to train the model. Error: {e}")
27
+
28
+ # 1. وظيفة لتحليل وتصنيف الرسائل وتخزينها في Firestore
29
+ def classify_and_store_message(message):
30
+ global model, vectorizer
31
+ try:
32
+ if not model or not vectorizer:
33
+ raise ValueError("Model or vectorizer not loaded. Train or load the model first.")
34
+
35
+ # تحويل الرسالة إلى سمات رقمية
36
+ message_vector = vectorizer.transform([message])
37
+ classification = model.predict(message_vector)[0]
38
+
39
+ # إعداد البيانات للتخزين
40
+ message_data = {
41
+ 'text': message,
42
+ 'classification': classification,
43
+ 'timestamp': datetime.datetime.now()
44
+ }
45
+
46
+ # تخزين الرسالة في مجموعة Firestore حسب التصنيف
47
+ if classification == "spam_phishing":
48
+ db.collection('spam').add(message_data)
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.")
63
+ return classification
64
+
65
+ except Exception as e:
66
+ print(f"Error classifying message: {e}")
67
+ return None
68
+
69
+ # 2. رفع النموذج إلى Hugging Face Hub
70
+ def upload_model_to_huggingface():
71
+ try:
72
+ # استخدام HfApi لرفع النموذج
73
+ api = HfApi()
74
+
75
+ # تحديد اسم النموذج والملفات
76
+ model_name = "Noufy/https-api_inference.huggingface.comodels-Noufy-naive_bayes_sms"
77
+ model_path = "model.joblib"
78
+ vectorizer_path = "vectorizer.joblib"
79
+
80
+ # رفع النموذج إلى Hugging Face
81
+ api.upload_file(
82
+ path_or_fileobj=model_path,
83
+ path_in_repo=f"{model_name}/model.joblib",
84
+ repo_id="Noufy/https-api_inference.huggingface.comodels-Noufy-naive_bayes_sms", # ضع اسم المستخدم واسم المشروع
85
+ repo_type="model"
86
+ )
87
+
88
+ api.upload_file(
89
+ path_or_fileobj=vectorizer_path,
90
+ path_in_repo=f"{model_name}/vectorizer.joblib",
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
+ except Exception as e:
98
+ print(f"Error uploading model: {e}")
99
+
100
+ # 3. تحديث النموذج مع بيانات جديدة
101
+ def update_model_with_new_data(new_messages, new_labels):
102
+ global model, vectorizer
103
+ try:
104
+ # تحميل البيانات الحالية
105
+ data = {
106
+ 'message': new_messages,
107
+ 'label': new_labels
108
+ }
109
+ df_new = pd.DataFrame(data)
110
+
111
+ # تحديث المحول والنموذج
112
+ if vectorizer is None or model is None:
113
+ vectorizer = TfidfVectorizer()
114
+ X_new = vectorizer.fit_transform(df_new['message'])
115
+ else:
116
+ X_new = vectorizer.transform(df_new['message'])
117
+
118
+ # جمع البيانات الجديدة مع القديمة وإعادة التدريب
119
+ y_new = df_new['label']
120
+ if model is None:
121
+ model = MultinomialNB()
122
+ model.partial_fit(X_new, y_new, classes=['spam_phishing', 'social_phishing', 'news_phishing', 'advertisement_phishing'])
123
+
124
+ # حفظ النموذج ا��جديد
125
+ dump(model, 'model.joblib')
126
+ dump(vectorizer, 'vectorizer.joblib')
127
+ print("Model updated and saved successfully.")
128
+
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 now!",
142
+ "Exclusive offers just for you, buy now!"
143
+ ]
144
+
145
+ for msg in test_messages:
146
+ classification = classify_and_store_message(msg)
147
+ print(f"Message: '{msg}' -> Classified as: {classification}")
148
+
149
+ # 5. وظيفة للتصحيح اليدوي
150
+ def correct_classification(message_id, correct_label):
151
+ try:
152
+ # جلب الرسالة من Firebase
153
+ message_ref = db.collection('view_history').document(message_id)
154
+ message_data = message_ref.get().to_dict()
155
+
156
+ if not message_data:
157
+ print("Message not found.")
158
+ return
159
+
160
+ # تحديث التصنيف في Firebase
161
+ message_data['classification'] = correct_label
162
+ message_ref.update({'classification': correct_label})
163
+
164
+ # إضافة البيانات إلى نموذج التدريب الجديد
165
+ update_model_with_new_data([message_data['text']], [correct_label])
166
+ print(f"Message classification corrected to {correct_label} and model updated.")
167
+ except Exception as e:
168
+ print(f"Error correcting classification: {e}")
169
+
170
+ # اختبار
171
+ test_system()