|
|
|
""" |
|
SecureX - Version Gradio |
|
Détection de fraude téléphonique avec interface web |
|
""" |
|
|
|
import re |
|
from datetime import datetime |
|
import gradio as gr |
|
|
|
class FraudDetector: |
|
def __init__(self): |
|
self.patterns = [ |
|
{ |
|
"name": "fake_advisor", |
|
"keywords": ["conseiller", "banque", "urgent", "code", "confirmer"], |
|
"weight": 0.9, |
|
"description": "Imitation de conseiller bancaire" |
|
}, |
|
{ |
|
"name": "fake_refund", |
|
"keywords": ["remboursement", "argent", "erreur", "virement"], |
|
"weight": 0.8, |
|
"description": "Promesse de remboursement suspecte" |
|
}, |
|
{ |
|
"name": "credential_theft", |
|
"keywords": ["mot de passe", "identifiant", "code secret", "pin"], |
|
"weight": 0.95, |
|
"description": "Demande d'informations sensibles" |
|
} |
|
] |
|
|
|
def analyze(self, text): |
|
"""Analyse le texte et retourne les résultats""" |
|
if not text.strip(): |
|
return 0.0, [], "Erreur: Veuillez entrer un texte à analyser" |
|
|
|
text_lower = text.lower() |
|
total_score = 0.0 |
|
detected_patterns = [] |
|
|
|
|
|
for pattern in self.patterns: |
|
matches = [word for word in pattern["keywords"] if word in text_lower] |
|
if matches: |
|
score = (len(matches) / len(pattern["keywords"])) * pattern["weight"] |
|
total_score += score |
|
detected_patterns.append({ |
|
"pattern": pattern["name"], |
|
"description": pattern["description"], |
|
"keywords": matches, |
|
"score": f"+{score:.2f}" |
|
}) |
|
|
|
|
|
urgency_words = ["urgent", "immédiat", "vite", "maintenant"] |
|
urgency_count = sum(1 for word in urgency_words if word in text_lower) |
|
if urgency_count >= 2: |
|
urgency_score = min(0.3, urgency_count * 0.1) |
|
total_score += urgency_score |
|
detected_patterns.append({ |
|
"pattern": "high_urgency", |
|
"description": f"Langage urgent ({urgency_count} mots)", |
|
"keywords": [], |
|
"score": f"+{urgency_score:.2f}" |
|
}) |
|
|
|
total_score = min(total_score, 1.0) |
|
|
|
|
|
report = self.generate_report(total_score, detected_patterns) |
|
|
|
return total_score, detected_patterns, report |
|
|
|
def generate_report(self, score, patterns): |
|
"""Génère un rapport détaillé""" |
|
report = f"# 📊 Rapport d'analyse - Score: {score:.0%}\n\n" |
|
|
|
if score > 0.75: |
|
report += "## 🚨 ALERTE FRAUDE ÉLEVÉE\n" |
|
report += "**Action recommandée:**\n" |
|
report += "- Terminez immédiatement l'appel\n" |
|
report += "- Contactez votre banque au numéro officiel\n" |
|
report += "- Ne donnez aucune information\n" |
|
elif score > 0.5: |
|
report += "## ⚠️ RISQUE MODÉRÉ\n" |
|
report += "**Soyez vigilant:**\n" |
|
report += "- Vérifiez l'identité de l'interlocuteur\n" |
|
report += "- Ne confirmez pas d'informations sensibles\n" |
|
else: |
|
report += "## ✅ RISQUE FAIBLE\n" |
|
report += "Aucun indicateur de fraude majeur détecté\n" |
|
|
|
if patterns: |
|
report += "\n## 🔍 Détections spécifiques:\n" |
|
for p in patterns: |
|
report += f"\n- **{p['description']}** {p['score']}\n" |
|
if p['keywords']: |
|
report += f" - Mots-clés: {', '.join(p['keywords'])}\n" |
|
|
|
report += f"\n_Analyse effectuée le {datetime.now().strftime('%d/%m/%Y à %H:%M')}_" |
|
|
|
return report |
|
|
|
|
|
detector = FraudDetector() |
|
|
|
|
|
examples = [ |
|
["Bonjour je suis votre conseiller bancaire. Confirmez votre code secret URGENT pour éviter le blocage!"], |
|
["Félicitations! Vous avez droit à un remboursement. Donnez votre numéro de carte pour le traitement."], |
|
["Rappel: votre rendez-vous en agence est confirmé pour demain 14h."] |
|
] |
|
|
|
|
|
with gr.Blocks(title="SecureX - Détection de Fraude", theme=gr.themes.Soft()) as app: |
|
gr.Markdown("# 🛡️ SecureX - Détection de Fraude Téléphonique") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
input_text = gr.Textbox( |
|
label="Transcript de l'appel", |
|
placeholder="Collez le contenu de l'appel ici...", |
|
lines=7, |
|
max_lines=15 |
|
) |
|
with gr.Row(): |
|
analyze_btn = gr.Button("Analyser", variant="primary") |
|
clear_btn = gr.Button("Effacer") |
|
|
|
gr.Examples( |
|
examples=examples, |
|
inputs=[input_text], |
|
label="Exemples rapides" |
|
) |
|
|
|
with gr.Column(): |
|
risk_score = gr.Label( |
|
label="Score de risque", |
|
value=0 |
|
) |
|
risk_level = gr.Textbox( |
|
label="Niveau de risque", |
|
value="Non analysé", |
|
interactive=False |
|
) |
|
risk_gauge = gr.Slider( |
|
label="Niveau de risque (Jauge)", |
|
minimum=0, |
|
maximum=1, |
|
value=0, |
|
step=0.01, |
|
interactive=False |
|
) |
|
report = gr.Markdown( |
|
value="## Résultats s'afficheront ici" |
|
) |
|
|
|
|
|
def update_results(text): |
|
score, patterns, report_text = detector.analyze(text) |
|
|
|
|
|
if score > 0.75: |
|
level = "Élevé" |
|
elif score > 0.5: |
|
level = "Modéré" |
|
else: |
|
level = "Faible" |
|
|
|
return score, level, score, report_text |
|
|
|
analyze_btn.click( |
|
fn=update_results, |
|
inputs=input_text, |
|
outputs=[risk_score, risk_level, risk_gauge, report] |
|
) |
|
|
|
clear_btn.click( |
|
fn=lambda: [ |
|
0, |
|
"Non analysé", |
|
0, |
|
"## Résultats s'afficheront ici" |
|
], |
|
outputs=[risk_score, risk_level, risk_gauge, report] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
app.launch( |
|
server_name="0.0.0.0", |
|
server_port=7860, |
|
share=False |
|
) |