Spaces:
Build error
Build error
| # ============================================ | |
| # 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() | |
| ''' | |