Spaces:
Sleeping
Sleeping
Commit ·
fe5e888
1
Parent(s): 9908faa
Add HuggingFace token
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ from flask import Flask, request, jsonify, render_template
|
|
| 2 |
from flask_wtf import FlaskForm
|
| 3 |
from wtforms import StringField, SubmitField
|
| 4 |
from wtforms.validators import DataRequired
|
| 5 |
-
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
app.config["SECRET_KEY"] = "secret123"
|
|
@@ -11,10 +11,12 @@ class CommentForm(FlaskForm):
|
|
| 11 |
comment = StringField("Comment", validators=[DataRequired()])
|
| 12 |
submit = SubmitField("Predict")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
@app.route("/", methods=["GET", "POST"])
|
| 20 |
def home():
|
|
@@ -24,17 +26,20 @@ def home():
|
|
| 24 |
|
| 25 |
if form.validate_on_submit():
|
| 26 |
text = form.comment.data
|
| 27 |
-
result =
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
else:
|
| 34 |
-
prediction = "
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
prediction_prob = round(score * 100, 2)
|
| 38 |
|
| 39 |
return render_template("index.html", form=form, prediction=prediction, prediction_prob=prediction_prob)
|
| 40 |
|
|
@@ -42,33 +47,35 @@ def home():
|
|
| 42 |
def predict():
|
| 43 |
data = request.get_json()
|
| 44 |
text = data["comment"]
|
| 45 |
-
result =
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
@app.route("/prediction_by_api", methods=["GET", "POST"])
|
| 58 |
def prediction_by_api():
|
| 59 |
data = request.get_json()
|
| 60 |
text = data["comment"]
|
| 61 |
-
result =
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
app.run(debug=True)
|
|
|
|
| 2 |
from flask_wtf import FlaskForm
|
| 3 |
from wtforms import StringField, SubmitField
|
| 4 |
from wtforms.validators import DataRequired
|
| 5 |
+
import requests
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
app.config["SECRET_KEY"] = "secret123"
|
|
|
|
| 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 |
+
response = requests.post(API_URL, headers=HEADERS, json={"inputs": text})
|
| 19 |
+
return response.json()
|
| 20 |
|
| 21 |
@app.route("/", methods=["GET", "POST"])
|
| 22 |
def home():
|
|
|
|
| 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 |
|
|
|
|
| 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 |
+
score = 1 - score
|
| 77 |
+
return jsonify({"prediction": prediction, "probability": round(score, 4)})
|
| 78 |
+
return jsonify({"error": "model loading"})
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
app.run(debug=True)
|