Commit ·
478bb6f
1
Parent(s): 9ea0894
Switch to gradio app
Browse files- app.py +45 -65
- requirements.txt +2 -84
app.py
CHANGED
|
@@ -1,81 +1,61 @@
|
|
| 1 |
-
|
| 2 |
-
from flask_wtf import FlaskForm
|
| 3 |
-
from wtforms import StringField, SubmitField
|
| 4 |
-
from wtforms.validators import DataRequired
|
| 5 |
import requests
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
class CommentForm(FlaskForm):
|
| 11 |
-
comment = StringField("Comment", validators=[DataRequired()])
|
| 12 |
-
submit = SubmitField("Predict")
|
| 13 |
|
| 14 |
API_URL = "https://api-inference.huggingface.co/models/Hate-speech-CNERG/dehatebert-mono-english"
|
| 15 |
-
HEADERS = {"Authorization": "Bearer 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"}
|
| 16 |
|
| 17 |
def query(text):
|
| 18 |
-
|
|
|
|
| 19 |
return response.json()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
if form.validate_on_submit():
|
| 28 |
-
text = form.comment.data
|
| 29 |
-
result = query(text)
|
| 30 |
-
if isinstance(result, list):
|
| 31 |
-
result = result[0][0]
|
| 32 |
-
label = result["label"]
|
| 33 |
-
score = result["score"]
|
| 34 |
-
if label == "HATE":
|
| 35 |
-
prediction = "Hate Speech"
|
| 36 |
-
else:
|
| 37 |
-
prediction = "No Hate Speech"
|
| 38 |
-
score = 1 - score
|
| 39 |
-
prediction_prob = round(score * 100, 2)
|
| 40 |
-
else:
|
| 41 |
-
prediction = "Error - try again"
|
| 42 |
-
prediction_prob = 0
|
| 43 |
-
|
| 44 |
-
return render_template("index.html", form=form, prediction=prediction, prediction_prob=prediction_prob)
|
| 45 |
-
|
| 46 |
-
@app.route("/predict", methods=["GET", "POST"])
|
| 47 |
-
def predict():
|
| 48 |
-
data = request.get_json()
|
| 49 |
-
text = data["comment"]
|
| 50 |
-
result = query(text)
|
| 51 |
-
if isinstance(result, list):
|
| 52 |
-
result = result[0][0]
|
| 53 |
-
label = result["label"]
|
| 54 |
-
score = result["score"]
|
| 55 |
-
if label == "HATE":
|
| 56 |
-
prediction = "Hate Speech"
|
| 57 |
-
else:
|
| 58 |
-
prediction = "No Hate Speech"
|
| 59 |
-
score = 1 - score
|
| 60 |
-
return jsonify({"prediction": prediction, "probability": round(score, 4)})
|
| 61 |
-
return jsonify({"error": "model loading"})
|
| 62 |
-
|
| 63 |
-
@app.route("/prediction_by_api", methods=["GET", "POST"])
|
| 64 |
-
def prediction_by_api():
|
| 65 |
-
data = request.get_json()
|
| 66 |
-
text = data["comment"]
|
| 67 |
-
result = query(text)
|
| 68 |
if isinstance(result, list):
|
| 69 |
result = result[0][0]
|
| 70 |
label = result["label"]
|
| 71 |
score = result["score"]
|
|
|
|
| 72 |
if label == "HATE":
|
| 73 |
-
prediction = "Hate Speech"
|
|
|
|
| 74 |
else:
|
| 75 |
-
prediction = "No Hate Speech"
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Load API token from environment variable
|
| 6 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
API_URL = "https://api-inference.huggingface.co/models/Hate-speech-CNERG/dehatebert-mono-english"
|
|
|
|
| 9 |
|
| 10 |
def query(text):
|
| 11 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 12 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": text})
|
| 13 |
return response.json()
|
| 14 |
|
| 15 |
+
def predict(comment):
|
| 16 |
+
if not comment.strip():
|
| 17 |
+
return "⚠️ Please enter some text.", ""
|
| 18 |
+
|
| 19 |
+
result = query(comment)
|
| 20 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
if isinstance(result, list):
|
| 22 |
result = result[0][0]
|
| 23 |
label = result["label"]
|
| 24 |
score = result["score"]
|
| 25 |
+
|
| 26 |
if label == "HATE":
|
| 27 |
+
prediction = "🚨 Hate Speech Detected"
|
| 28 |
+
probability = round(score * 100, 2)
|
| 29 |
else:
|
| 30 |
+
prediction = "✅ No Hate Speech"
|
| 31 |
+
probability = round((1 - score) * 100, 2)
|
| 32 |
+
|
| 33 |
+
return prediction, f"{probability}%"
|
| 34 |
+
|
| 35 |
+
return "❌ Error - Model loading, try again.", ""
|
| 36 |
+
|
| 37 |
+
# Gradio UI
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=predict,
|
| 40 |
+
inputs=gr.Textbox(
|
| 41 |
+
lines=4,
|
| 42 |
+
placeholder="Enter a comment to analyze...",
|
| 43 |
+
label="Comment"
|
| 44 |
+
),
|
| 45 |
+
outputs=[
|
| 46 |
+
gr.Textbox(label="Prediction"),
|
| 47 |
+
gr.Textbox(label="Confidence")
|
| 48 |
+
],
|
| 49 |
+
title="🛡️ Hate Speech Detector",
|
| 50 |
+
description="Enter any text to check if it contains hate speech.",
|
| 51 |
+
examples=[
|
| 52 |
+
["I love everyone and believe in equality!"],
|
| 53 |
+
["I hate those people, they should be banned."],
|
| 54 |
+
["This is a great day to be alive!"],
|
| 55 |
+
["Those people are disgusting and should leave."]
|
| 56 |
+
],
|
| 57 |
+
theme=gr.themes.Soft()
|
| 58 |
+
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,84 +1,2 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
astunparse==1.6.3
|
| 4 |
-
backcall==0.2.0
|
| 5 |
-
cachetools==5.3.1
|
| 6 |
-
certifi==2023.5.7
|
| 7 |
-
charset-normalizer==3.2.0
|
| 8 |
-
click==8.1.4
|
| 9 |
-
cloudpickle==2.2.1
|
| 10 |
-
colorama==0.4.6
|
| 11 |
-
comm==0.1.3
|
| 12 |
-
debugpy==1.6.7
|
| 13 |
-
decorator==5.1.1
|
| 14 |
-
executing==1.2.0
|
| 15 |
-
Flask==1.1.2
|
| 16 |
-
Flask-WTF==0.14.3
|
| 17 |
-
flatbuffers==23.5.26
|
| 18 |
-
gast==0.4.0
|
| 19 |
-
google-auth==2.22.0
|
| 20 |
-
google-auth-oauthlib==0.4.6
|
| 21 |
-
google-pasta==0.2.0
|
| 22 |
-
grpcio==1.56.0
|
| 23 |
-
gunicorn==20.1.0
|
| 24 |
-
h5py==3.8.0
|
| 25 |
-
idna==3.4
|
| 26 |
-
importlib-metadata==6.7.0
|
| 27 |
-
ipykernel==6.16.2
|
| 28 |
-
ipython==7.34.0
|
| 29 |
-
itsdangerous==2.0.1
|
| 30 |
-
jedi==0.18.2
|
| 31 |
-
Jinja2==3.0.0
|
| 32 |
-
jupyter_client==8.0.0a1
|
| 33 |
-
jupyter_core==5.0.0rc2
|
| 34 |
-
keras==2.10.0
|
| 35 |
-
Keras-Preprocessing==1.1.2
|
| 36 |
-
libclang==16.0.0
|
| 37 |
-
Markdown==3.4.3
|
| 38 |
-
MarkupSafe==2.1.3
|
| 39 |
-
matplotlib-inline==0.1.6
|
| 40 |
-
nest-asyncio==1.5.6
|
| 41 |
-
numpy==1.21.6
|
| 42 |
-
oauthlib==3.2.2
|
| 43 |
-
opt-einsum==3.3.0
|
| 44 |
-
packaging==23.1
|
| 45 |
-
parso==0.8.3
|
| 46 |
-
pickleshare==0.7.5
|
| 47 |
-
platformdirs==3.8.1
|
| 48 |
-
prompt-toolkit==3.0.39
|
| 49 |
-
protobuf==3.19.6
|
| 50 |
-
psutil==5.9.5
|
| 51 |
-
pure-eval==0.2.2
|
| 52 |
-
pyasn1==0.5.0
|
| 53 |
-
pyasn1-modules==0.3.0
|
| 54 |
-
Pygments==2.15.1
|
| 55 |
-
python-dateutil==2.8.2
|
| 56 |
-
python-dotenv==0.21.1
|
| 57 |
-
pyzmq==25.1.0
|
| 58 |
-
requests==2.31.0
|
| 59 |
-
requests-oauthlib==1.3.1
|
| 60 |
-
rsa==4.9
|
| 61 |
-
six==1.16.0
|
| 62 |
-
spyder-kernels==2.2.0
|
| 63 |
-
stack-data==0.6.2
|
| 64 |
-
tensorboard==2.10.1
|
| 65 |
-
tensorboard-data-server==0.6.1
|
| 66 |
-
tensorboard-plugin-wit==1.8.1
|
| 67 |
-
tensorflow==2.10.0
|
| 68 |
-
tensorflow-estimator==2.10.0
|
| 69 |
-
tensorflow-hub==0.13.0
|
| 70 |
-
tensorflow-io-gcs-filesystem==0.31.0
|
| 71 |
-
tensorflow-text==2.10.0
|
| 72 |
-
termcolor==2.3.0
|
| 73 |
-
tornado==6.2
|
| 74 |
-
traitlets==5.9.0
|
| 75 |
-
typing_extensions==4.7.1
|
| 76 |
-
urllib3==1.26.16
|
| 77 |
-
wcwidth==0.2.6
|
| 78 |
-
Werkzeug==2.0.3
|
| 79 |
-
wrapt==1.15.0
|
| 80 |
-
WTForms==2.3.3
|
| 81 |
-
zipp==3.15.0
|
| 82 |
-
transformers
|
| 83 |
-
torch
|
| 84 |
-
sentencepiece
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|