File size: 4,239 Bytes
6731b88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f582f88
6731b88
 
 
 
 
69a1198
 
6731b88
 
 
e13456a
 
 
6731b88
f582f88
 
 
e13456a
 
f582f88
 
6731b88
f582f88
 
 
e13456a
 
f582f88
 
6731b88
52d1b38
6731b88
f582f88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6731b88
f582f88
 
 
e13456a
 
 
 
f582f88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6731b88
 
f582f88
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

MODEL_PATH = "." 

try:
    print(f"Loading model from {MODEL_PATH}...")
    tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
    model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
    model.eval()
    print("Model loaded successfully!")
except Exception as e:
    print(f"Error loading model: {e}")
    print("Falling back to default DistilBERT (untrained) for interface demo purpose only.")
    model_name = "distilbert-base-uncased"
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)

def predict_toxicity(text):
    if not text or not text.strip():
        return "<h3 style='color: grey;'>Waiting for input...</h3>", {}

    inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
    
    with torch.no_grad():
        logits = model(**inputs).logits
        temperature = 2.0 
        probs = torch.softmax(logits / temperature, dim=1)[0].cpu().numpy()

    non_toxic_prob = float(probs[0])
    toxic_prob = float(probs[1])
    toxic_pct = f"{toxic_prob:.2%}"
    normal_pct = f"{non_toxic_prob:.2%}"

    if toxic_prob > 0.5:
        result_html = f"""
        <div style="background-color: #ffcccc; border: 2px solid red; border-radius: 10px; padding: 20px; text-align: center;">
            <h2 style="color: red; margin: 0;">πŸ”΄ TOXIC DETECTED</h2>
            <p style="margin: 5px 0 0 0; color: darkred; font-weight: bold;">Confidence: {toxic_pct}</p>
            <p style="margin: 5px 0 0 0; color: #cc0000; font-size: 0.9em;">This comment violates community guidelines.</p>
        </div>
        """
    else:
        result_html = f"""
        <div style="background-color: #ccffcc; border: 2px solid green; border-radius: 10px; padding: 20px; text-align: center;">
            <h2 style="color: green; margin: 0;">🟒 CONTENT IS SAFE</h2>
            <p style="margin: 5px 0 0 0; color: darkgreen; font-weight: bold;">Confidence: {normal_pct}</p>
            <p style="margin: 5px 0 0 0; color: #006600; font-size: 0.9em;">No harmful language detected.</p>
        </div>
        """

    return result_html, {"Toxic": toxic_prob, "Normal": non_toxic_prob}

with gr.Blocks(theme=gr.themes.Soft(), title="AI Toxic Comment Detector") as demo:
    
    gr.Markdown(
        """
        # πŸ›‘οΈ AI Toxic Comment Detector
        ### Ensuring Digital Safety with Artificial Intelligence
        This tool supports **SDG 16 (Peace, Justice and Strong Institutions)** by reducing online hate speech 
        and **SDG 3 (Good Health and Well-being)** by protecting mental health.
        """
    )
    
    with gr.Row():
        with gr.Column(scale=1):
            input_text = gr.Textbox(
                lines=5, 
                placeholder="Type a comment here to analyze...", 
                label="πŸ“ Input Text",
                info="Supports English text analysis."
            )
            
            with gr.Row():
                clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
                submit_btn = gr.Button("πŸš€ Analyze", variant="primary")

        with gr.Column(scale=1):
            output_html = gr.HTML(label="Verdict")
            output_chart = gr.Label(num_top_classes=2, label="Confidence Score πŸ“Š")

    gr.Markdown("### πŸ§ͺ Try these examples:")
    examples = gr.Examples(
        examples=[
            ["I really appreciate your perspective on this article, well done!"],
            ["You are absolute garbage, nobody wants you here."],
            ["I love how you just don't care about looking professional."], 
            ["This movie was crazy good! I killed it at the gym today."]
        ],
        inputs=input_text
    )

    submit_btn.click(
        fn=predict_toxicity, 
        inputs=input_text, 
        outputs=[output_html, output_chart]
    )
    
    clear_btn.click(
        lambda: ("", "<h3 style='color: grey;'>Waiting for input...</h3>", {}),
        inputs=None,
        outputs=[input_text, output_html, output_chart]
    )

if __name__ == "__main__":
    demo.launch()