Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================
|
| 2 |
+
# FILE: app.py (main application file)
|
| 3 |
+
# ============================================
|
| 4 |
+
APP_PY = '''
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# Load your model
|
| 10 |
+
try:
|
| 11 |
+
classifier = pipeline(
|
| 12 |
+
"text-classification",
|
| 13 |
+
model="archich/hate-speech-detector",
|
| 14 |
+
tokenizer="archich/hate-speech-detector"
|
| 15 |
+
)
|
| 16 |
+
print("✅ Model loaded successfully!")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"❌ Error loading model: {e}")
|
| 19 |
+
classifier = None
|
| 20 |
+
|
| 21 |
+
def predict_hate_speech(text):
|
| 22 |
+
"""Predict if text contains hate speech"""
|
| 23 |
+
if not text or not text.strip():
|
| 24 |
+
return {"error": "Please provide text to analyze"}
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
# Get predictions
|
| 28 |
+
results = classifier(text)
|
| 29 |
+
|
| 30 |
+
# Format response
|
| 31 |
+
response = {
|
| 32 |
+
"input": text,
|
| 33 |
+
"predictions": results,
|
| 34 |
+
"is_hate_speech": results[0]["label"] in ["LABEL_1", "hate_speech", "HATE"],
|
| 35 |
+
"confidence": results[0]["score"]
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
return json.dumps(response, indent=2)
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return {"error": str(e)}
|
| 42 |
+
|
| 43 |
+
# Create Gradio interface
|
| 44 |
+
demo = gr.Interface(
|
| 45 |
+
fn=predict_hate_speech,
|
| 46 |
+
inputs=gr.Textbox(
|
| 47 |
+
lines=3,
|
| 48 |
+
placeholder="Enter text to analyze...",
|
| 49 |
+
label="Input Text"
|
| 50 |
+
),
|
| 51 |
+
outputs=gr.JSON(label="Analysis Result"),
|
| 52 |
+
title="🛡️ Hate Speech Detector API",
|
| 53 |
+
description="""
|
| 54 |
+
Analyze text for hate speech using the archich/hate-speech-detector model.
|
| 55 |
+
|
| 56 |
+
**API Endpoint:** Use the API tab above or call this Space via API.
|
| 57 |
+
""",
|
| 58 |
+
examples=[
|
| 59 |
+
["I love this community! Everyone is so kind."],
|
| 60 |
+
["You are terrible and I hate you."],
|
| 61 |
+
["This is a neutral statement about technology."]
|
| 62 |
+
]
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch()
|
| 67 |
+
'''
|