Spaces:
Sleeping
Sleeping
First true version
Browse files- app.py +70 -4
- requirements.txt +8 -0
app.py
CHANGED
|
@@ -1,7 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import logging
|
| 4 |
+
import nltk
|
| 5 |
+
import torch
|
| 6 |
import gradio as gr
|
| 7 |
+
from transformers import pipeline, AutoConfig
|
| 8 |
+
from nltk.tokenize import word_tokenize
|
| 9 |
+
from nltk.stem import WordNetLemmatizer
|
| 10 |
+
from textblob import TextBlob
|
| 11 |
|
| 12 |
+
# Configuration du logging
|
| 13 |
+
logging.basicConfig(level=logging.DEBUG)
|
| 14 |
|
| 15 |
+
# Vérifier la disponibilité du GPU
|
| 16 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 17 |
+
|
| 18 |
+
# Charger le modèle et sa configuration
|
| 19 |
+
model_name = "AgentPublic/camembert-base-toxic-fr-user-prompts"
|
| 20 |
+
config = AutoConfig.from_pretrained(model_name)
|
| 21 |
+
classifier = pipeline('text-classification', model=model_name, device=device)
|
| 22 |
+
|
| 23 |
+
# Chargement des ressources NLTK
|
| 24 |
+
nltk.download('punkt')
|
| 25 |
+
try:
|
| 26 |
+
nltk.data.find('corpora/wordnet')
|
| 27 |
+
except LookupError:
|
| 28 |
+
nltk.download('wordnet')
|
| 29 |
+
|
| 30 |
+
lemmatizer = WordNetLemmatizer()
|
| 31 |
+
insult_words = [
|
| 32 |
+
"con", "cons", "connard", "connards", "enculé", "enculés",
|
| 33 |
+
"pute", "putes", "putain", "merde", "idiot"
|
| 34 |
+
]
|
| 35 |
+
insult_pattern = re.compile(r'\b(?:' + '|'.join(insult_words) + r')\b', re.IGNORECASE)
|
| 36 |
+
|
| 37 |
+
def analyze_text(text, threshold=0.5):
|
| 38 |
+
"""
|
| 39 |
+
Analyse un texte pour détecter la toxicité avec un seuil de confiance.
|
| 40 |
+
Retourne True si la toxicité détectée est supérieure ou égale au seuil.
|
| 41 |
+
"""
|
| 42 |
+
result = classifier(text, truncation=True)[0]
|
| 43 |
+
label_map = {v: k for k, v in config.label2id.items()}
|
| 44 |
+
toxic_label = label_map.get(1, "toxic") # Sécurisation de l'accès
|
| 45 |
+
logging.debug(f"Texte: {text} -> Score: {result['score']}")
|
| 46 |
+
return result['label'] == toxic_label and result['score'] >= threshold
|
| 47 |
+
|
| 48 |
+
def detect_toxicity(message):
|
| 49 |
+
"""
|
| 50 |
+
Vérifie si un message est toxique selon l'IA et les règles heuristiques.
|
| 51 |
+
"""
|
| 52 |
+
words = [lemmatizer.lemmatize(word) for word in word_tokenize(message.lower())]
|
| 53 |
+
blob = TextBlob(" ".join(words))
|
| 54 |
+
sentiment = blob.sentiment.polarity
|
| 55 |
+
|
| 56 |
+
# Conversion en booléen pour éviter de renvoyer un objet re.Match
|
| 57 |
+
return analyze_text(message) or bool(insult_pattern.search(message)) or sentiment < -0.5
|
| 58 |
+
|
| 59 |
+
def predict(text):
|
| 60 |
+
is_toxic = detect_toxicity(text)
|
| 61 |
+
return f"Is toxic: {is_toxic}"
|
| 62 |
+
|
| 63 |
+
# Création de l'interface Gradio
|
| 64 |
+
iface = gr.Interface(
|
| 65 |
+
fn=predict,
|
| 66 |
+
inputs=gr.inputs.Textbox(lines=5, label="Texte en français"),
|
| 67 |
+
outputs="text",
|
| 68 |
+
title="Détecteur de Toxicité",
|
| 69 |
+
description="Entrez un texte en français pour vérifier s'il est toxique."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
torchvision
|
| 5 |
+
torchaudio
|
| 6 |
+
nltk
|
| 7 |
+
textblob
|
| 8 |
+
pydantic
|