Pavankumar9026 commited on
Commit
fe5e888
·
1 Parent(s): 9908faa

Add HuggingFace token

Browse files
Files changed (1) hide show
  1. app.py +44 -37
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
- from transformers import pipeline
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
- # Load pre-trained hate speech model from Hugging Face
15
- print("Loading model, please wait...")
16
- classifier = pipeline("text-classification", model="Hate-speech-CNERG/dehatebert-mono-english")
17
- print("Model loaded!")
 
 
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 = classifier(text)[0]
28
- label = result["label"]
29
- score = result["score"]
30
-
31
- if label == "HATE":
32
- prediction = "Hate Speech"
 
 
 
 
 
33
  else:
34
- prediction = "No Hate Speech"
35
- score = 1 - score
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 = classifier(text)[0]
46
- label = result["label"]
47
- score = result["score"]
48
-
49
- if label == "HATE":
50
- prediction = "Hate Speech"
51
- else:
52
- prediction = "No Hate Speech"
53
- score = 1 - score
54
-
55
- return jsonify({"prediction": prediction, "probability": round(score, 4)})
 
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 = classifier(text)[0]
62
- label = result["label"]
63
- score = result["score"]
64
-
65
- if label == "HATE":
66
- prediction = "Hate Speech"
67
- else:
68
- prediction = "No Hate Speech"
69
- score = 1 - score
70
-
71
- return jsonify({"prediction": prediction, "probability": round(score, 4)})
 
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)