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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
SecureX - Version Gradio
|
| 4 |
+
Détection de fraude téléphonique avec interface web
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import re
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
class FraudDetector:
|
| 12 |
+
def __init__(self):
|
| 13 |
+
self.patterns = [
|
| 14 |
+
{
|
| 15 |
+
"name": "fake_advisor",
|
| 16 |
+
"keywords": ["conseiller", "banque", "urgent", "code", "confirmer"],
|
| 17 |
+
"weight": 0.9,
|
| 18 |
+
"description": "Imitation de conseiller bancaire"
|
| 19 |
+
},
|
| 20 |
+
{
|
| 21 |
+
"name": "fake_refund",
|
| 22 |
+
"keywords": ["remboursement", "argent", "erreur", "virement"],
|
| 23 |
+
"weight": 0.8,
|
| 24 |
+
"description": "Promesse de remboursement suspecte"
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"name": "credential_theft",
|
| 28 |
+
"keywords": ["mot de passe", "identifiant", "code secret", "pin"],
|
| 29 |
+
"weight": 0.95,
|
| 30 |
+
"description": "Demande d'informations sensibles"
|
| 31 |
+
}
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
def analyze(self, text):
|
| 35 |
+
"""Analyse le texte et retourne les résultats"""
|
| 36 |
+
if not text.strip():
|
| 37 |
+
return 0.0, [], "Erreur: Veuillez entrer un texte à analyser"
|
| 38 |
+
|
| 39 |
+
text_lower = text.lower()
|
| 40 |
+
total_score = 0.0
|
| 41 |
+
detected_patterns = []
|
| 42 |
+
|
| 43 |
+
# Analyse des motifs
|
| 44 |
+
for pattern in self.patterns:
|
| 45 |
+
matches = [word for word in pattern["keywords"] if word in text_lower]
|
| 46 |
+
if matches:
|
| 47 |
+
score = (len(matches) / len(pattern["keywords"])) * pattern["weight"]
|
| 48 |
+
total_score += score
|
| 49 |
+
detected_patterns.append({
|
| 50 |
+
"pattern": pattern["name"],
|
| 51 |
+
"description": pattern["description"],
|
| 52 |
+
"keywords": matches,
|
| 53 |
+
"score": f"+{score:.2f}"
|
| 54 |
+
})
|
| 55 |
+
|
| 56 |
+
# Détection d'urgence
|
| 57 |
+
urgency_words = ["urgent", "immédiat", "vite", "maintenant"]
|
| 58 |
+
urgency_count = sum(1 for word in urgency_words if word in text_lower)
|
| 59 |
+
if urgency_count >= 2:
|
| 60 |
+
urgency_score = min(0.3, urgency_count * 0.1)
|
| 61 |
+
total_score += urgency_score
|
| 62 |
+
detected_patterns.append({
|
| 63 |
+
"pattern": "high_urgency",
|
| 64 |
+
"description": f"Langage urgent ({urgency_count} mots)",
|
| 65 |
+
"keywords": [],
|
| 66 |
+
"score": f"+{urgency_score:.2f}"
|
| 67 |
+
})
|
| 68 |
+
|
| 69 |
+
total_score = min(total_score, 1.0)
|
| 70 |
+
|
| 71 |
+
# Génération du rapport
|
| 72 |
+
report = self.generate_report(total_score, detected_patterns)
|
| 73 |
+
|
| 74 |
+
return total_score, detected_patterns, report
|
| 75 |
+
|
| 76 |
+
def generate_report(self, score, patterns):
|
| 77 |
+
"""Génère un rapport détaillé"""
|
| 78 |
+
report = f"# 📊 Rapport d'analyse - Score: {score:.0%}\n\n"
|
| 79 |
+
|
| 80 |
+
if score > 0.75:
|
| 81 |
+
report += "## 🚨 ALERTE FRAUDE ÉLEVÉE\n"
|
| 82 |
+
report += "**Action recommandée:**\n"
|
| 83 |
+
report += "- Terminez immédiatement l'appel\n"
|
| 84 |
+
report += "- Contactez votre banque au numéro officiel\n"
|
| 85 |
+
report += "- Ne donnez aucune information\n"
|
| 86 |
+
elif score > 0.5:
|
| 87 |
+
report += "## ⚠️ RISQUE MODÉRÉ\n"
|
| 88 |
+
report += "**Soyez vigilant:**\n"
|
| 89 |
+
report += "- Vérifiez l'identité de l'interlocuteur\n"
|
| 90 |
+
report += "- Ne confirmez pas d'informations sensibles\n"
|
| 91 |
+
else:
|
| 92 |
+
report += "## ✅ RISQUE FAIBLE\n"
|
| 93 |
+
report += "Aucun indicateur de fraude majeur détecté\n"
|
| 94 |
+
|
| 95 |
+
if patterns:
|
| 96 |
+
report += "\n## 🔍 Détections spécifiques:\n"
|
| 97 |
+
for p in patterns:
|
| 98 |
+
report += f"\n- **{p['description']}** {p['score']}\n"
|
| 99 |
+
if p['keywords']:
|
| 100 |
+
report += f" - Mots-clés: {', '.join(p['keywords'])}\n"
|
| 101 |
+
|
| 102 |
+
report += f"\n_Analyse effectuée le {datetime.now().strftime('%d/%m/%Y à %H:%M')}_"
|
| 103 |
+
|
| 104 |
+
return report
|
| 105 |
+
|
| 106 |
+
# Création de l'instance
|
| 107 |
+
detector = FraudDetector()
|
| 108 |
+
|
| 109 |
+
# Exemples prédéfinis
|
| 110 |
+
examples = [
|
| 111 |
+
["Bonjour je suis votre conseiller bancaire. Confirmez votre code secret URGENT pour éviter le blocage!"],
|
| 112 |
+
["Félicitations! Vous avez droit à un remboursement. Donnez votre numéro de carte pour le traitement."],
|
| 113 |
+
["Rappel: votre rendez-vous en agence est confirmé pour demain 14h."]
|
| 114 |
+
]
|
| 115 |
+
|
| 116 |
+
# Interface Gradio
|
| 117 |
+
with gr.Blocks(title="SecureX - Détection de Fraude", theme=gr.themes.Soft()) as app:
|
| 118 |
+
gr.Markdown("# 🛡️ SecureX - Détection de Fraude Téléphonique")
|
| 119 |
+
|
| 120 |
+
with gr.Row():
|
| 121 |
+
with gr.Column():
|
| 122 |
+
input_text = gr.Textbox(
|
| 123 |
+
label="Transcript de l'appel",
|
| 124 |
+
placeholder="Collez le contenu de l'appel ici...",
|
| 125 |
+
lines=7,
|
| 126 |
+
max_lines=15
|
| 127 |
+
)
|
| 128 |
+
with gr.Row():
|
| 129 |
+
analyze_btn = gr.Button("Analyser", variant="primary")
|
| 130 |
+
clear_btn = gr.Button("Effacer")
|
| 131 |
+
|
| 132 |
+
gr.Examples(
|
| 133 |
+
examples=examples,
|
| 134 |
+
inputs=[input_text],
|
| 135 |
+
label="Exemples rapides"
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
with gr.Column():
|
| 139 |
+
risk_score = gr.Label(
|
| 140 |
+
label="Score de risque",
|
| 141 |
+
value={"Score": "0%", "Niveau": "Non analysé"}
|
| 142 |
+
)
|
| 143 |
+
risk_gauge = gr.Gauge(
|
| 144 |
+
label="Niveau de risque",
|
| 145 |
+
minimum=0,
|
| 146 |
+
maximum=1,
|
| 147 |
+
value=0,
|
| 148 |
+
interactive=False
|
| 149 |
+
)
|
| 150 |
+
report = gr.Markdown(
|
| 151 |
+
value="## Résultats s'afficheront ici",
|
| 152 |
+
label="Rapport détaillé"
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
# Fonctions de mise à jour
|
| 156 |
+
def update_results(text):
|
| 157 |
+
score, patterns, report_text = detector.analyze(text)
|
| 158 |
+
|
| 159 |
+
# Mise à jour du score
|
| 160 |
+
if score > 0.75:
|
| 161 |
+
level = "Élevé"
|
| 162 |
+
elif score > 0.5:
|
| 163 |
+
level = "Modéré"
|
| 164 |
+
else:
|
| 165 |
+
level = "Faible"
|
| 166 |
+
|
| 167 |
+
score_label = {
|
| 168 |
+
"Score": f"{score:.0%}",
|
| 169 |
+
"Niveau": level
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
return {
|
| 173 |
+
risk_score: score_label,
|
| 174 |
+
risk_gauge: score,
|
| 175 |
+
report: report_text
|
| 176 |
+
}
|
| 177 |
+
|
| 178 |
+
# Liaisons
|
| 179 |
+
analyze_btn.click(
|
| 180 |
+
fn=update_results,
|
| 181 |
+
inputs=input_text,
|
| 182 |
+
outputs=[risk_score, risk_gauge, report]
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
clear_btn.click(
|
| 186 |
+
fn=lambda: [
|
| 187 |
+
{"Score": "0%", "Niveau": "Non analysé"},
|
| 188 |
+
0,
|
| 189 |
+
"## Résultats s'afficheront ici"
|
| 190 |
+
],
|
| 191 |
+
outputs=[risk_score, risk_gauge, report]
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
# Lancement de l'application
|
| 195 |
+
if __name__ == "__main__":
|
| 196 |
+
app.launch(
|
| 197 |
+
server_name="0.0.0.0",
|
| 198 |
+
server_port=7860,
|
| 199 |
+
share=False
|
| 200 |
+
)
|