Titembaye commited on
Commit
960046a
·
1 Parent(s): 6a06412

Initial deployment: BERT adversarial training model

Browse files
README.md CHANGED
@@ -1,13 +1,45 @@
1
  ---
2
- title: PhiBert
3
- emoji: 🏢
4
  colorFrom: red
5
- colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Détecteur de Phishing par IA
3
+ emoji: 🛡️
4
  colorFrom: red
5
+ colorTo: blue
6
  sdk: gradio
7
  sdk_version: 5.49.1
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
+ # 🛡️ Détecteur de Phishing par Intelligence Artificielle
13
+
14
+ Application de détection de phishing utilisant **BERT fine-tuné avec adversarial training**.
15
+
16
+ ## 🎯 Objectif du Projet
17
+
18
+ Cette application fait partie d'un projet de recherche sur :
19
+ - 🎯 **Robustesse adversariale** : Résistance aux attaques de phishing générées par IA
20
+ - 🌐 **Généralisation cross-linguale** : Capacité à détecter du phishing en français et en anglais
21
+
22
+ ## 📊 Données d'Entraînement
23
+
24
+ Le modèle a été entraîné sur :
25
+ - **Enron Email Dataset** (500k emails légitimes)
26
+ - **SMS Spam Collection** (5,574 SMS)
27
+ - **Phishing Email Dataset** (18,650 emails de phishing)
28
+ - **Phishing adversariaux** générés par Ollama + Gemma3:1b (1,968 échantillons)
29
+
30
+ ## 🤖 Modèle
31
+
32
+ - **Architecture :** BERT-base-uncased (110M paramètres)
33
+ - **Fine-tuning :** Adversarial training (50% baseline + 50% adversarial)
34
+ - **Performance :** F1-Score ~95% sur phishing adversarial
35
+
36
+ ## 🚀 Utilisation
37
+
38
+ 1. Collez un email dans la zone de texte
39
+ 2. Cliquez sur "🔍 Analyser"
40
+ 3. Obtenez le verdict et les probabilités
41
+
42
+ ## ⚠️ Disclaimer
43
+
44
+ Cette application est fournie **à des fins éducatives et de recherche uniquement**.
45
+ Ne l'utilisez pas comme unique système de protection contre le phishing.
app.py ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Application Gradio - Détecteur de Phishing
3
+ Modèle: BERT fine-tuné avec adversarial training
4
+ """
5
+
6
+ import gradio as gr
7
+ import torch
8
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
9
+ import os
10
+
11
+ # Configuration
12
+ MODEL_PATH = "models/bert-base-uncased_adversarial_final"
13
+ MAX_LENGTH = 256
14
+ DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+
16
+ print("="*60)
17
+ print("🚀 Initialisation du Détecteur de Phishing")
18
+ print("="*60)
19
+
20
+ # Vérifier que le modèle existe
21
+ if not os.path.exists(MODEL_PATH):
22
+ raise FileNotFoundError(
23
+ f"❌ Modèle introuvable: {MODEL_PATH}\n"
24
+ f" Assurez-vous que le dossier existe et contient les fichiers du modèle."
25
+ )
26
+
27
+ # Charger le tokenizer et le modèle
28
+ print(f"📥 Chargement du tokenizer depuis {MODEL_PATH}...")
29
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
30
+
31
+ print(f"📥 Chargement du modèle depuis {MODEL_PATH}...")
32
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
33
+ model.to(DEVICE)
34
+ model.eval()
35
+
36
+ print(f"✅ Modèle chargé avec succès!")
37
+ print(f"🖥️ Device: {DEVICE}")
38
+ print("="*60 + "\n")
39
+
40
+
41
+ def predict_phishing(email_text):
42
+ """
43
+ Prédit si un email est du phishing ou légitime
44
+
45
+ Args:
46
+ email_text (str): Texte de l'email à analyser
47
+
48
+ Returns:
49
+ tuple: (verdict, probabilités, analyse détaillée)
50
+ """
51
+ if not email_text.strip():
52
+ return "⚠️ Veuillez entrer un email", {}, ""
53
+
54
+ # Tokenization
55
+ inputs = tokenizer(
56
+ email_text,
57
+ max_length=MAX_LENGTH,
58
+ padding='max_length',
59
+ truncation=True,
60
+ return_tensors='pt'
61
+ )
62
+
63
+ # Déplacer sur le bon device
64
+ inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
65
+
66
+ # Prédiction
67
+ with torch.no_grad():
68
+ outputs = model(**inputs)
69
+ logits = outputs.logits
70
+ probabilities = torch.softmax(logits, dim=1)[0]
71
+ predicted_class = torch.argmax(probabilities).item()
72
+ confidence = probabilities[predicted_class].item()
73
+
74
+ # Résultats
75
+ label = "🚨 Phishing Détecté" if predicted_class == 1 else "✅ Email Légitime"
76
+
77
+ prob_dict = {
78
+ "Légitime": float(probabilities[0]),
79
+ "Phishing": float(probabilities[1])
80
+ }
81
+
82
+ # Analyse détaillée
83
+ analysis = f"""
84
+ ### 📊 Résultats de l'analyse
85
+
86
+ **Verdict:** {label}
87
+ **Confiance:** {confidence * 100:.1f}%
88
+
89
+ ### 🔍 Détails des probabilités
90
+ - **Légitime:** {probabilities[0] * 100:.2f}%
91
+ - **Phishing:** {probabilities[1] * 100:.2f}%
92
+
93
+ ### 📝 Informations
94
+ - **Modèle:** BERT-base-uncased (adversarial training)
95
+ - **Longueur du texte:** {len(email_text)} caractères
96
+ - **Tokens:** {len(tokenizer.encode(email_text))} tokens
97
+
98
+ ### ⚠️ Avertissement
99
+ Cette analyse est fournie à titre éducatif uniquement. En cas de doute sur un email réel,
100
+ contactez votre service informatique ou l'expéditeur présumé par un canal sécurisé.
101
+ """
102
+
103
+ return label, prob_dict, analysis
104
+
105
+
106
+ # Exemples d'emails pour la démo
107
+ examples = [
108
+ ["""Dear valued customer,
109
+
110
+ Your account has been suspended due to unusual activity.
111
+ Please verify your identity immediately by clicking the link below:
112
+
113
+ http://secure-verify-account.com/login
114
+
115
+ You have 24 hours to verify or your account will be permanently closed.
116
+
117
+ Best regards,
118
+ Security Team"""],
119
+
120
+ ["""Hi team,
121
+
122
+ Just a reminder that our weekly meeting is scheduled for tomorrow at 2 PM in Conference Room B.
123
+
124
+ Please bring your project updates.
125
+
126
+ Thanks,
127
+ John"""],
128
+
129
+ ["""URGENT: You have won $1,000,000 in the international lottery!
130
+
131
+ To claim your prize, send us your bank details and a processing fee of $500.
132
+
133
+ Contact us immediately: winner@lottery-prize.com
134
+
135
+ Congratulations!"""],
136
+
137
+ ["""Hello,
138
+
139
+ Your package delivery failed.
140
+ Track your package here: https://trackpackage.com/xyz123
141
+
142
+ Delivery company will retry tomorrow between 9 AM - 5 PM.
143
+
144
+ Tracking ID: XYZ123456"""]
145
+ ]
146
+
147
+
148
+ # Interface Gradio
149
+ with gr.Blocks(theme=gr.themes.Soft(), title="Détecteur de Phishing") as demo:
150
+ gr.Markdown("""
151
+ # 🛡️ Détecteur de Phishing par Intelligence Artificielle
152
+
153
+ Cette application utilise un modèle **BERT fine-tuné avec adversarial training**
154
+ pour détecter les emails de phishing.
155
+
156
+ **Axes d'évaluation:**
157
+ - 🎯 Robustesse face aux attaques adversariales générées par IA
158
+ - 🌐 Généralisation cross-linguale (EN/FR)
159
+
160
+ ---
161
+ """)
162
+
163
+ with gr.Row():
164
+ with gr.Column(scale=2):
165
+ email_input = gr.Textbox(
166
+ label="📧 Collez votre email ici",
167
+ placeholder="Entrez le contenu de l'email à analyser...",
168
+ lines=10,
169
+ max_lines=20
170
+ )
171
+
172
+ with gr.Row():
173
+ analyze_btn = gr.Button("🔍 Analyser", variant="primary", size="lg")
174
+ clear_btn = gr.ClearButton([email_input], value="🗑️ Effacer")
175
+
176
+ with gr.Column(scale=1):
177
+ verdict_output = gr.Textbox(
178
+ label="🎯 Verdict",
179
+ interactive=False,
180
+ lines=2
181
+ )
182
+
183
+ prob_output = gr.Label(
184
+ label="📊 Probabilités",
185
+ num_top_classes=2
186
+ )
187
+
188
+ with gr.Row():
189
+ analysis_output = gr.Markdown(label="📈 Analyse Détaillée")
190
+
191
+ # Exemples
192
+ gr.Markdown("### 💡 Exemples à tester")
193
+ gr.Examples(
194
+ examples=examples,
195
+ inputs=email_input,
196
+ label="Cliquez sur un exemple pour le tester"
197
+ )
198
+
199
+ # Footer
200
+ gr.Markdown("""
201
+ ---
202
+
203
+ ### 📚 À propos
204
+
205
+ **Projet:** Détection de Phishing par IA - Robustesse Adversariale et Généralisation Cross-Linguale
206
+
207
+ **Datasets utilisés:**
208
+ - Enron Email Dataset (500k emails)
209
+ - SMS Spam Collection (5,574 SMS)
210
+ - Phishing Email Dataset (18,650 emails)
211
+ - Phishing adversariaux générés par Ollama + Gemma3:1b
212
+
213
+ **Modèle:**
214
+ - BERT-base-uncased (110M paramètres)
215
+ - Fine-tuné avec adversarial training (50% baseline + 50% adversarial)
216
+
217
+ ⚠️ **Disclaimer:** Cette application est fournie à des fins éducatives et de recherche uniquement.
218
+ """)
219
+
220
+ # Actions
221
+ analyze_btn.click(
222
+ fn=predict_phishing,
223
+ inputs=email_input,
224
+ outputs=[verdict_output, prob_output, analysis_output]
225
+ )
226
+
227
+
228
+ if __name__ == "__main__":
229
+ print("\n" + "="*60)
230
+ print("🚀 Lancement de l'application Gradio")
231
+ print("="*60)
232
+ print(f"📱 Device: {DEVICE}")
233
+ print(f"🤖 Modèle: {MODEL_PATH}")
234
+ print("="*60 + "\n")
235
+
236
+ demo.launch(
237
+ server_name="127.0.0.1", # Accessible localement uniquement
238
+ server_port=7860,
239
+ share=False, # Mettre True pour obtenir un lien public temporaire
240
+ show_error=True
241
+ )
models/config.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertForSequenceClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "classifier_dropout": null,
7
+ "dtype": "float32",
8
+ "gradient_checkpointing": false,
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 768,
12
+ "initializer_range": 0.02,
13
+ "intermediate_size": 3072,
14
+ "layer_norm_eps": 1e-12,
15
+ "max_position_embeddings": 512,
16
+ "model_type": "bert",
17
+ "num_attention_heads": 12,
18
+ "num_hidden_layers": 12,
19
+ "pad_token_id": 0,
20
+ "position_embedding_type": "absolute",
21
+ "problem_type": "single_label_classification",
22
+ "transformers_version": "4.57.0",
23
+ "type_vocab_size": 2,
24
+ "use_cache": true,
25
+ "vocab_size": 30522
26
+ }
models/model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f5339a24a01173741b1b9eef5bdd19022b0dfaa1a915a25c8144bc9324b7f710
3
+ size 437958648
models/special_tokens_map.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": "[CLS]",
3
+ "mask_token": "[MASK]",
4
+ "pad_token": "[PAD]",
5
+ "sep_token": "[SEP]",
6
+ "unk_token": "[UNK]"
7
+ }
models/tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
models/tokenizer_config.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "100": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "101": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "102": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "103": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": false,
45
+ "cls_token": "[CLS]",
46
+ "do_lower_case": true,
47
+ "extra_special_tokens": {},
48
+ "mask_token": "[MASK]",
49
+ "model_max_length": 512,
50
+ "pad_token": "[PAD]",
51
+ "sep_token": "[SEP]",
52
+ "strip_accents": null,
53
+ "tokenize_chinese_chars": true,
54
+ "tokenizer_class": "BertTokenizer",
55
+ "unk_token": "[UNK]"
56
+ }
models/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0fb555b3f5b186429fd7368c45115a9c45d03d7ddc5e663fdbb4dd4a46eaf9e
3
+ size 6033
models/vocab.txt ADDED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers==4.57.1
2
+ torch==2.5.1
3
+ gradio==5.49.1