Noufy commited on
Commit
8da6361
1 Parent(s): 6893491

Upload NavyBayes.py

Browse files
Files changed (1) hide show
  1. NavyBayes.py +48 -46
NavyBayes.py CHANGED
@@ -1,5 +1,6 @@
1
- from sklearn.feature_extraction.text import TfidfVectorizer
2
  from sklearn.naive_bayes import MultinomialNB
 
3
  from joblib import dump, load
4
  import firebase_admin
5
  from firebase_admin import credentials, firestore
@@ -8,13 +9,13 @@ import datetime
8
  import re
9
  import pandas as pd
10
  import os
 
11
 
12
  # إعداد السجلات
13
  logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
14
 
15
  # Firebase Initialization
16
  try:
17
- # استخدم المسار الذي قدمته
18
  cred_path = r"D:\app-sentinel-7qnr19-firebase-adminsdk-kjmbe-533749ec1a.json"
19
  if not firebase_admin._apps:
20
  cred = credentials.Certificate(cred_path)
@@ -25,54 +26,55 @@ except Exception as e:
25
  logging.error(f"Error initializing Firebase: {e}")
26
  db = None
27
 
28
- # Load or Train Model
29
  try:
30
- model_path = os.path.join(os.getcwd(), "model.joblib")
31
- vectorizer_path = os.path.join(os.getcwd(), "vectorizer.joblib")
32
- model = load(model_path)
33
- vectorizer = load(vectorizer_path)
34
- logging.info("Model and vectorizer loaded successfully.")
35
  except Exception as e:
36
- logging.warning(f"Model and vectorizer not found. Training new ones. Error: {e}")
37
- # Train new model and vectorizer
38
- messages = ["example message 1", "example message 2"]
39
- labels = ["label1", "label2"]
40
- vectorizer = TfidfVectorizer()
41
- X = vectorizer.fit_transform(messages)
42
- model = MultinomialNB()
43
- model.fit(X, labels)
44
- dump(model, model_path)
45
- dump(vectorizer, vectorizer_path)
46
- logging.info("New model and vectorizer trained and saved.")
47
-
48
- # Classify Message
49
- def classify_and_store_message(message):
50
- """
51
- Classify a message and store the result in Firestore.
52
- """
53
- global model, vectorizer
54
  try:
55
- if not message.strip():
56
- raise ValueError("Input message cannot be empty.")
 
 
 
 
 
 
 
 
 
 
57
 
58
- # Transform the message using the vectorizer
59
- message_vector = vectorizer.transform([message])
60
- classification = model.predict(message_vector)[0]
61
 
62
- # Prepare data for Firestore
63
- message_data = {
64
- "text": message,
65
- "classification": classification,
66
- "timestamp": datetime.datetime.now(),
67
- }
 
 
 
 
 
 
 
 
 
 
 
68
 
69
- if db:
70
- db.collection("all_messages").add(message_data)
71
- logging.info(f"Message classified as {classification} and stored.")
72
- else:
73
- logging.warning("Firestore is not initialized. Data not stored.")
 
 
74
 
75
- return classification
76
- except Exception as e:
77
- logging.error(f"Error in classification: {e}")
78
- return None
 
1
+ from sklearn.pipeline import Pipeline
2
  from sklearn.naive_bayes import MultinomialNB
3
+ from sklearn.feature_extraction.text import TfidfVectorizer
4
  from joblib import dump, load
5
  import firebase_admin
6
  from firebase_admin import credentials, firestore
 
9
  import re
10
  import pandas as pd
11
  import os
12
+ from flask import Flask, request, jsonify
13
 
14
  # إعداد السجلات
15
  logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")
16
 
17
  # Firebase Initialization
18
  try:
 
19
  cred_path = r"D:\app-sentinel-7qnr19-firebase-adminsdk-kjmbe-533749ec1a.json"
20
  if not firebase_admin._apps:
21
  cred = credentials.Certificate(cred_path)
 
26
  logging.error(f"Error initializing Firebase: {e}")
27
  db = None
28
 
29
+ # Load or Train Model with Pipeline
30
  try:
31
+ model_path = os.path.join(os.getcwd(), "model_pipeline.joblib")
32
+ model_pipeline = load(model_path)
33
+ logging.info("Model pipeline loaded successfully.")
 
 
34
  except Exception as e:
35
+ logging.warning(f"Model pipeline not found. Training new one. Error: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  try:
37
+ # Train new model and vectorizer as part of pipeline
38
+ messages = ["example message 1", "example message 2"]
39
+ labels = ["label1", "label2"]
40
+ model_pipeline = Pipeline([
41
+ ('vectorizer', TfidfVectorizer()), # تحويل النصوص إلى تمثيل رقمي
42
+ ('classifier', MultinomialNB()) # تصنيف باستخدام Naive Bayes
43
+ ])
44
+ model_pipeline.fit(messages, labels)
45
+ dump(model_pipeline, model_path)
46
+ logging.info("New model pipeline trained and saved.")
47
+ except Exception as e:
48
+ logging.error(f"Error training new model: {e}")
49
 
50
+ # Flask API
51
+ app = Flask(__name__)
 
52
 
53
+ @app.route('/classify', methods=['POST'])
54
+ def classify_message():
55
+ data = request.json
56
+ message = data.get("message", "")
57
+
58
+ # التصنيف باستخدام الـ pipeline
59
+ if message.strip() == "":
60
+ return jsonify({"error": "Message cannot be empty"}), 400
61
+
62
+ classification = model_pipeline.predict([message])[0]
63
+
64
+ # تخزين الرسالة في Firestore
65
+ message_data = {
66
+ "text": message,
67
+ "classification": classification,
68
+ "timestamp": datetime.datetime.now(),
69
+ }
70
 
71
+ if db:
72
+ db.collection("all_messages").add(message_data)
73
+ logging.info(f"Message classified as {classification} and stored in Firestore.")
74
+ else:
75
+ logging.warning("Firestore is not initialized. Data not stored.")
76
+
77
+ return jsonify({"classification": classification})
78
 
79
+ if __name__ == "__main__":
80
+ app.run(debug=True)