File size: 1,872 Bytes
bc90a78
 
 
 
 
 
 
 
 
 
 
 
12246ed
 
bc90a78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ============================================
# FILE: app.py (main application file)
# ============================================
APP_PY = '''
import gradio as gr
from transformers import pipeline
import json

# Load your model
try:
    classifier = pipeline(
        "text-classification",
        model="archich/hate-speech-detectorr",
        tokenizer="archich/hate-speech-detectorr"
    )
    print("✅ Model loaded successfully!")
except Exception as e:
    print(f"❌ Error loading model: {e}")
    classifier = None

def predict_hate_speech(text):
    """Predict if text contains hate speech"""
    if not text or not text.strip():
        return {"error": "Please provide text to analyze"}
    
    try:
        # Get predictions
        results = classifier(text)
        
        # Format response
        response = {
            "input": text,
            "predictions": results,
            "is_hate_speech": results[0]["label"] in ["LABEL_1", "hate_speech", "HATE"],
            "confidence": results[0]["score"]
        }
        
        return json.dumps(response, indent=2)
    
    except Exception as e:
        return {"error": str(e)}

# Create Gradio interface
demo = gr.Interface(
    fn=predict_hate_speech,
    inputs=gr.Textbox(
        lines=3,
        placeholder="Enter text to analyze...",
        label="Input Text"
    ),
    outputs=gr.JSON(label="Analysis Result"),
    title="🛡️ Hate Speech Detector API",
    description="""
    Analyze text for hate speech using the archich/hate-speech-detector model.
    
    **API Endpoint:** Use the API tab above or call this Space via API.
    """,
    examples=[
        ["I love this community! Everyone is so kind."],
        ["You are terrible and I hate you."],
        ["This is a neutral statement about technology."]
    ]
)

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