File size: 1,547 Bytes
e73553d
 
 
02d4487
 
 
 
 
e73553d
02d4487
 
 
e73553d
02d4487
e73553d
 
 
 
 
 
 
 
 
 
 
 
02d4487
 
 
 
e73553d
 
02d4487
e73553d
02d4487
 
 
 
 
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 = {
    "Unitary Toxic BERT": "unitary/toxic-bert",
    "Martin HA Toxic Comment": "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")
    ],
    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 and 'Martin HA Toxic Comment'.",
    examples=[
        ["Great game everyone!", "Toxic BERT"],
        ["You're such a noob, uninstall please.", "Martin HA Toxic Comment"],
        ["I hope you die in real life, loser.", "Toxic BERT"],
        ["Nice move! How did you do that?", "Martin HA Toxic Comment"],
        ["Go back to the kitchen where you belong.", "Toxic BERT"],
    ]
)

# Launch the app
iface.launch()