/
rest.py
157 lines (134 loc) · 4.73 KB
/
rest.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import logging
import os
import sys
import traceback
import tensorflow as tf
import tensorflow_hub as hub
from flask import Flask, jsonify, abort
from flask_cors import CORS
from flask_restx import Api, reqparse, Resource
from langid.langid import LanguageIdentifier, model
from numpy import dot
from language_tool import LanguageChecker
from numpy.linalg import norm
lang_checker = LanguageChecker()
language_identifier = LanguageIdentifier.from_modelstring(model, norm_probs=True)
sys.path.append(os.getcwd())
app = Flask(__name__)
CORS(app)
api = Api(app,
default="APIs",
version="0.1.1",
title="ParaQuality",
description="A service to quality assess crowdsourced paraphrases")
query_parser = reqparse.RequestParser()
query_parser.add_argument('sentence', type=str, help="sentence to be checked")
pair_parser = reqparse.RequestParser()
pair_parser.add_argument('sentence_1', type=str, help="a sentence to be checked")
pair_parser.add_argument('sentence_2', type=str, help="a sentence to be checked")
g = tf.Graph()
with g.as_default(), tf.device('/cpu:0'):
text_input = tf.placeholder(dtype=tf.string, shape=[None])
embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3")
my_result = embed(text_input)
init_op = tf.group([tf.global_variables_initializer(), tf.tables_initializer()])
g.finalize()
session = tf.Session(graph=g)
session.run(init_op)
@api.route("/linguistic/check-spelling")
class Spelling(Resource):
@api.response(200, "Success")
@api.expect(query_parser)
def get(self):
"""
spell checks a given sentence
"""
args = query_parser.parse_args()
sentence = args.get("sentence")
try:
return jsonify({
"original": sentence,
"corrected": lang_checker.spelling_corrector(sentence),
"errors": [a.__dict__ for a in lang_checker.check(sentence, categories=["TYPOS", "CASING"])]
})
except Exception as e:
abort(501, message="Server is not able to process the request; {}".format(e))
traceback.print_stack()
@api.route("/linguistic/check-grammar")
class Grammar(Resource):
@api.response(200, "Success")
@api.expect(query_parser)
def get(self):
"""
spell checks a given sentence
"""
args = query_parser.parse_args()
sentence = args.get("sentence")
try:
return jsonify({
"original": sentence,
"corrected": lang_checker.spelling_corrector(sentence),
"errors": [a.__dict__ for a in lang_checker.check(sentence, categories=['MISC', 'GRAMMAR'])]
})
except Exception as e:
abort(501, message="Server is not able to process the request; {}".format(e))
traceback.print_stack()
@api.route("/linguistic/detect-language")
class LanguageDetect(Resource):
@api.expect(query_parser)
def get(self):
"""
detects the language of the given sentence
"""
try:
args = query_parser.parse_args()
sentence = args.get("sentence")
misspelling = lang_checker.misspellings(sentence)
lang = "en"
if len(misspelling) > 1:
lang, _ = language_identifier.classify(sentence)
return jsonify({
"language": lang
})
except Exception as e:
abort(400, message=e)
traceback.print_stack()
@api.route("/linguistic/semantic-similarity")
class Similarity(Resource):
@api.expect(pair_parser)
def get(self):
def __vec(text):
embedding = session.run(my_result, feed_dict={text_input: [text]})[0].tolist()
return embedding
"""
generates canonical sentences for a list of operations
"""
try:
args = pair_parser.parse_args()
a = __vec(args.get("sentence_1"))
b = __vec(args.get("sentence_2"))
return jsonify({
"USE": dot(a, b) / (norm(a) * norm(b))
})
except Exception as e:
abort(400, message=e)
traceback.print_stack()
@api.route("/linguistic/cheating")
class Cheating(Resource):
@api.expect(query_parser)
def get(self):
"""
generates canonical sentences for a list of operations
"""
try:
return jsonify([])
except Exception as e:
abort(400, message=e)
traceback.print_stack()
if __name__ == '__main__':
port = 8080
if len(sys.argv) > 1:
port = int(sys.argv[1])
LOGGER = logging.getLogger("artemis.fileman.disk_memoize")
LOGGER.setLevel(logging.WARN)
app.run("0.0.0.0", port=port)