File size: 2,032 Bytes
a13b271
 
 
90fb56e
0475377
 
 
 
236e362
0475377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20a6a5f
 
0475377
236e362
4f398a9
 
0475377
 
20a6a5f
0475377
20a6a5f
236e362
20a6a5f
 
 
 
 
 
 
 
0475377
20a6a5f
0475377
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
os.system("pip install torch transformers gradio matplotlib")


import torch
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification

torch.set_num_threads(torch.get_num_threads())

# Load the trained model and tokenizer from Hugging Face Hub
model_path = "HyperX-Sentience/RogueBERT-Toxicity-85K"
model = AutoModelForSequenceClassification.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)

# Move the model to CUDA if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# Define toxicity labels
labels = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]

def predict_toxicity(comment):
    """Predicts the toxicity levels of a given comment."""
    inputs = tokenizer(comment, truncation=True, padding="max_length", max_length=128, return_tensors="pt")
    inputs = {key: val.to(device) for key, val in inputs.items()}
    
    with torch.no_grad():
        outputs = model(**inputs)
        probabilities = torch.sigmoid(outputs.logits).cpu().numpy()[0]
    
    return {labels[i]: float(probabilities[i]) for i in range(len(labels))}

def format_toxicity_data(comment):
    """Formats the toxicity scores for a modern bar graph."""
    scores = predict_toxicity(comment)
    df = pd.DataFrame({"Category": list(scores.keys()), "Score": list(scores.values())})
    return df

# Gradio interface
demo = gr.Interface(
    fn=format_toxicity_data,
    inputs=gr.Textbox(label="Enter a comment:"),
    outputs=gr.BarPlot(
        value=None,
        x="Category",
        y="Score",
        title="Toxicity Analysis",
        y_lim=[0, 1],
        color="blue",
        label="Toxicity Scores",
        interactive=False
    ),
    title="Toxicity Detection with RogueBERT",
    description="Enter a comment to analyze its toxicity levels. The results will be displayed as a modern bar chart."
)

demo.launch()