Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
|
| 5 |
+
model_name = "gravitee-io/bert-small-toxicity"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
model.eval()
|
| 10 |
+
labels = model.config.id2label
|
| 11 |
+
|
| 12 |
+
def detecter_toxicite(texte):
|
| 13 |
+
"""
|
| 14 |
+
Détecte les types de toxicité dans un texte donné en utilisant le modèle BERT.
|
| 15 |
+
Retourne un dictionnaire avec chaque label de toxicité et son score de probabilité.
|
| 16 |
+
"""
|
| 17 |
+
# 3. Préparer l'entrée (Tokenization)
|
| 18 |
+
# Le modèle BERT attend des identifiants de tokens et un masque d'attention
|
| 19 |
+
inputs = tokenizer(texte, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 20 |
+
|
| 21 |
+
# 4. Exécuter l'inférence (sur CPU par défaut)
|
| 22 |
+
with torch.no_grad(): # Désactiver le calcul des gradients pour l'inférence
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
|
| 25 |
+
# 5. Post-traitement des sorties
|
| 26 |
+
# Les sorties sont des logits (scores bruts). Nous devons les convertir en probabilités.
|
| 27 |
+
logits = outputs.logits
|
| 28 |
+
probabilities = F.softmax(logits, dim=1)
|
| 29 |
+
|
| 30 |
+
resultats = {}
|
| 31 |
+
output = "Scores de toxicité :\n"
|
| 32 |
+
for i, label_id in enumerate(model.config.id2label):
|
| 33 |
+
label_name = model.config.id2label[label_id]
|
| 34 |
+
score = probabilities[0][i].item() # .item() pour obtenir la valeur Python standard
|
| 35 |
+
|
| 36 |
+
output += f" - {label_name}: {score:.4f}\n"
|
| 37 |
+
resultats[label_name.lower()] = score
|
| 38 |
+
|
| 39 |
+
# Exemple de décision basée sur un seuil
|
| 40 |
+
seuil_toxicite = 0.2 # Définissez votre propre seuil
|
| 41 |
+
est_toxique = resultats["toxic"] > resultats["not-toxic"]
|
| 42 |
+
if est_toxique:
|
| 43 |
+
output += f"Verdict : TOXIQUE (score > {seuil_toxicite})\n"
|
| 44 |
+
else:
|
| 45 |
+
output += f"Verdict : NON TOXIQUE (aucun score > {seuil_toxicite})\n"
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("# Hugging Face Demo")
|
| 50 |
+
|
| 51 |
+
with gr.Tab("Modération de texte"):
|
| 52 |
+
text_input = gr.Textbox(label="Prompt", placeholder="Je suis le text a modérer", lines=4, scale=2)
|
| 53 |
+
text_output = gr.Textbox(label="Résultat Modération", lines=4, scale=2)
|
| 54 |
+
text_btn = gr.Button("Générer")
|
| 55 |
+
text_btn.click(detecter_toxicite, inputs=text_input, outputs=text_output)
|