forked from sudhof/politeness-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
55 lines (38 loc) · 1.19 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import math
from flask import Flask
from flask import render_template, request, jsonify
from politeness_model import score_politeness
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
"""
@app.route("/")
def hello():
return "Hello world, it's the Politeness Classifier!"
"""
@app.route("/")
def text_input_form():
return render_template("politeness-form.html")
@app.route("/get-politeness")
def text_input_form2():
return render_template("politeness-form.html")
@app.route("/score-politeness", methods=['POST'])
def score_text():
text = request.form['text']
probs = score_politeness(text)
# Based on probs, determine label and confidence
if probs['polite'] > 0.6:
l = "polite"
confidence = probs['polite']
elif probs['impolite'] > 0.6:
l = "impolite"
confidence = probs['impolite']
else:
l = "neutral"
confidence = 1.0 - math.fabs(probs['polite'] - 0.5)
confidence = "%.2f" % confidence
# Return JSON:
return jsonify(text=text, label=l, confidence=confidence)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)