add core logic
Browse files- app.py +42 -5
- requirements.txt +0 -52
- runtime.txt +0 -1
app.py
CHANGED
|
@@ -1,7 +1,44 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Initialize Flask app
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
|
| 7 |
+
# Load language detection model from Hugging Face
|
| 8 |
+
lang_classifier = pipeline("text-classification", model="papluca/xlm-roberta-base-language-detection")
|
| 9 |
+
|
| 10 |
+
# Load hate speech detection model from Hugging Face
|
| 11 |
+
hate_classifier = pipeline("text-classification", model="Hate-speech-CNERG/dehatebert-mono-english")
|
| 12 |
+
|
| 13 |
+
# Route: Health check
|
| 14 |
+
@app.route("/", methods=["GET"])
|
| 15 |
+
def home():
|
| 16 |
+
return jsonify({"message": "Language & Hate Speech API is running!"})
|
| 17 |
+
|
| 18 |
+
# Route: Detect Language
|
| 19 |
+
@app.route("/detect-language", methods=["POST"])
|
| 20 |
+
def detect_language():
|
| 21 |
+
data = request.json
|
| 22 |
+
text = data.get("text", "").strip()
|
| 23 |
+
|
| 24 |
+
if not text:
|
| 25 |
+
return jsonify({"error": "No text provided"}), 400
|
| 26 |
+
|
| 27 |
+
result = lang_classifier(text)
|
| 28 |
+
return jsonify({"language": result[0]['label'], "confidence": result[0]['score']})
|
| 29 |
+
|
| 30 |
+
# Route: Detect Hate Speech
|
| 31 |
+
@app.route("/detect-hate-speech", methods=["POST"])
|
| 32 |
+
def detect_hate_speech():
|
| 33 |
+
data = request.json
|
| 34 |
+
text = data.get("text", "").strip()
|
| 35 |
+
|
| 36 |
+
if not text:
|
| 37 |
+
return jsonify({"error": "No text provided"}), 400
|
| 38 |
+
|
| 39 |
+
result = hate_classifier(text)
|
| 40 |
+
return jsonify({"label": result[0]['label'], "score": result[0]['score']})
|
| 41 |
+
|
| 42 |
+
# Run the server
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
app.run(host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,52 +0,0 @@
|
|
| 1 |
-
aiofiles==23.2.1
|
| 2 |
-
annotated-types==0.7.0
|
| 3 |
-
anyio==4.8.0
|
| 4 |
-
audioop-lts==0.2.1
|
| 5 |
-
certifi==2025.1.31
|
| 6 |
-
charset-normalizer==3.4.1
|
| 7 |
-
click==8.1.8
|
| 8 |
-
fastapi==0.115.8
|
| 9 |
-
ffmpy==0.5.0
|
| 10 |
-
filelock==3.17.0
|
| 11 |
-
fsspec==2025.2.0
|
| 12 |
-
gradio==5.16.0
|
| 13 |
-
gradio_client==1.7.0
|
| 14 |
-
h11==0.14.0
|
| 15 |
-
httpcore==1.0.7
|
| 16 |
-
httpx==0.28.1
|
| 17 |
-
huggingface-hub==0.28.1
|
| 18 |
-
idna==3.10
|
| 19 |
-
Jinja2==3.1.5
|
| 20 |
-
markdown-it-py==3.0.0
|
| 21 |
-
MarkupSafe==2.1.5
|
| 22 |
-
mdurl==0.1.2
|
| 23 |
-
numpy==2.2.3
|
| 24 |
-
orjson==3.10.15
|
| 25 |
-
packaging==24.2
|
| 26 |
-
pandas==2.2.3
|
| 27 |
-
pillow==11.1.0
|
| 28 |
-
pydantic==2.10.6
|
| 29 |
-
pydantic_core==2.27.2
|
| 30 |
-
pydub==0.25.1
|
| 31 |
-
Pygments==2.19.1
|
| 32 |
-
python-dateutil==2.9.0.post0
|
| 33 |
-
python-multipart==0.0.20
|
| 34 |
-
pytz==2025.1
|
| 35 |
-
PyYAML==6.0.2
|
| 36 |
-
requests==2.32.3
|
| 37 |
-
rich==13.9.4
|
| 38 |
-
ruff==0.9.6
|
| 39 |
-
safehttpx==0.1.6
|
| 40 |
-
semantic-version==2.10.0
|
| 41 |
-
shellingham==1.5.4
|
| 42 |
-
six==1.17.0
|
| 43 |
-
sniffio==1.3.1
|
| 44 |
-
starlette==0.45.3
|
| 45 |
-
tomlkit==0.13.2
|
| 46 |
-
tqdm==4.67.1
|
| 47 |
-
typer==0.15.1
|
| 48 |
-
typing_extensions==4.12.2
|
| 49 |
-
tzdata==2025.1
|
| 50 |
-
urllib3==2.3.0
|
| 51 |
-
uvicorn==0.34.0
|
| 52 |
-
websockets==14.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
runtime.txt
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
python-3.9
|
|
|
|
|
|