Spaces:
Sleeping
Sleeping
Afdal Bouraima commited on
Commit ·
e6eb50f
1
Parent(s): cd7e587
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr, spaces, torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
|
| 4 |
+
MODEL_ID = "Dalfaxy/mt0_xl_french_detox_v3"
|
| 5 |
+
PREFIX = "Detoxify: " # identique à l'entraînement (accent, espace)
|
| 6 |
+
|
| 7 |
+
tok = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
|
| 9 |
+
model.to("cuda")
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@spaces.GPU(duration=30)
|
| 13 |
+
def detox(text):
|
| 14 |
+
if not text.strip():
|
| 15 |
+
return ""
|
| 16 |
+
inputs = tok(
|
| 17 |
+
PREFIX + text, return_tensors="pt", truncation=True, max_length=256
|
| 18 |
+
).to("cuda")
|
| 19 |
+
out = model.generate(**inputs, max_new_tokens=128, num_beams=4)
|
| 20 |
+
return tok.decode(out[0], skip_special_tokens=True)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
gr.Interface(
|
| 24 |
+
fn=detox,
|
| 25 |
+
inputs=gr.Textbox(lines=3, label="Texte toxique"),
|
| 26 |
+
outputs=gr.Textbox(label="Version détoxifiée"),
|
| 27 |
+
title="French Detox — mT0-XL fine-tuné",
|
| 28 |
+
description="Réécrit un texte toxique en français en conservant son sens.",
|
| 29 |
+
examples=[
|
| 30 |
+
["Ferme-la, ton avis est complètement débile."],
|
| 31 |
+
["T'es vraiment un incapable, tu sers à rien."],
|
| 32 |
+
["Ce film est une daube, faut être con pour aimer ça."],
|
| 33 |
+
],
|
| 34 |
+
flagging_mode="never",
|
| 35 |
+
).launch()
|