Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load the model
|
| 5 |
+
MODEL_PATH = "unitary/toxic-bert"
|
| 6 |
+
classifier = pipeline("text-classification", model=MODEL_PATH, tokenizer=MODEL_PATH)
|
| 7 |
+
|
| 8 |
+
def predict_toxicity(text):
|
| 9 |
+
# Get predictions
|
| 10 |
+
predictions = classifier(text, return_all_scores=True)[0]
|
| 11 |
+
|
| 12 |
+
# Format results
|
| 13 |
+
results = {}
|
| 14 |
+
for pred in predictions:
|
| 15 |
+
results[pred['label']] = f"{pred['score']:.4f}"
|
| 16 |
+
|
| 17 |
+
return results
|
| 18 |
+
|
| 19 |
+
# Create the Gradio interface
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
+
fn=predict_toxicity,
|
| 22 |
+
inputs=gr.Textbox(lines=5, label="Enter text to analyze"),
|
| 23 |
+
outputs=gr.Label(num_top_classes=6, label="Toxicity Scores"),
|
| 24 |
+
title="Toxicity Prediction",
|
| 25 |
+
description="This POC uses the model based onm toxic-bert to predict toxicity in text. Multi-class response.",
|
| 26 |
+
examples=[
|
| 27 |
+
["Great game everyone!"],
|
| 28 |
+
["You're such a noob, uninstall please."],
|
| 29 |
+
["I hope you die in real life, loser."],
|
| 30 |
+
["Nice move! How did you do that?"],
|
| 31 |
+
["Go back to the kitchen where you belong."],
|
| 32 |
+
]
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Launch the app
|
| 36 |
+
iface.launch()
|