File size: 1,681 Bytes
e73553d
 
 
02d4487
 
88ee86c
 
02d4487
e73553d
02d4487
 
 
e73553d
02d4487
e73553d
 
 
 
 
 
 
 
 
 
 
 
02d4487
 
88ee86c
02d4487
e73553d
 
88ee86c
e73553d
88ee86c
 
 
 
 
e73553d
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import gradio as gr
from transformers import pipeline

# Load the models
MODEL_PATHS = {
    "Toxic Bert-based model": "unitary/toxic-bert",
    "Martin-HA-toxic-comment-model": "martin-ha/toxic-comment-model"
}

classifiers = {name: pipeline("text-classification", model=path, tokenizer=path) for name, path in MODEL_PATHS.items()}

def predict_toxicity(text, model_choice):
    # Get predictions
    classifier = classifiers[model_choice]
    predictions = classifier(text, return_all_scores=True)[0]
    
    # Format results
    results = {}
    for pred in predictions:
        results[pred['label']] = f"{pred['score']:.4f}"
    
    return results

# Create the Gradio interface
iface = gr.Interface(
    fn=predict_toxicity,
    inputs=[
        gr.Textbox(lines=5, label="Enter text to analyze"),
        gr.Radio(choices=list(MODEL_PATHS.keys()), label="Choose a model", value="Toxic Bert-based model")
    ],
    outputs=gr.Label(num_top_classes=6, label="Toxicity Scores"),
    title="Toxicity Prediction",
    description="This POC uses trained & pre-trained models to predict toxicity in text. Choose between two models: 'Toxic Bert-based model' for multi-class labeling and 'Martin-HA-toxic-comment-model' for binary clasification.",
    examples=[
        ["Great game everyone!", "Toxic Bert-based model"],
        ["You're such a noob, uninstall please.", "Martin-HA-toxic-comment-model"],
        ["I hope you die in real life, loser.", "Toxic Bert-based model"],
        ["Nice move! How did you do that?", "Martin-HA-toxic-comment-model"],
        ["Go back to the kitchen where you belong.", "Toxic Bert-based model"],
    ]
)

# Launch the app
iface.launch()