|
|
@ -17,17 +17,18 @@
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
Blueprints for Senpy
|
|
|
|
Blueprints for Senpy
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
from flask import Blueprint, request, current_app, render_template
|
|
|
|
from flask import Blueprint, request, current_app, render_template, url_for, jsonify
|
|
|
|
from .models import Error, Response, Plugins
|
|
|
|
from .models import Error, Response, Plugins, read_schema
|
|
|
|
from future.utils import iteritems
|
|
|
|
from future.utils import iteritems
|
|
|
|
|
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
nif_blueprint = Blueprint("NIF Sentiment Analysis Server", __name__)
|
|
|
|
api_blueprint = Blueprint("api", __name__)
|
|
|
|
demo_blueprint = Blueprint("Demo of the service. It includes an HTML+Javascript playground to test senpy", __name__)
|
|
|
|
demo_blueprint = Blueprint("demo", __name__)
|
|
|
|
|
|
|
|
|
|
|
|
API_PARAMS = {
|
|
|
|
API_PARAMS = {
|
|
|
|
"algorithm": {
|
|
|
|
"algorithm": {
|
|
|
@ -47,7 +48,7 @@ API_PARAMS = {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BASIC_PARAMS = {
|
|
|
|
NIF_PARAMS = {
|
|
|
|
"algorithm": {
|
|
|
|
"algorithm": {
|
|
|
|
"aliases": ["algorithm", "a", "algo"],
|
|
|
|
"aliases": ["algorithm", "a", "algo"],
|
|
|
|
"required": False,
|
|
|
|
"required": False,
|
|
|
@ -104,7 +105,7 @@ BASIC_PARAMS = {
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_params(req, params=BASIC_PARAMS):
|
|
|
|
def update_params(req, params=NIF_PARAMS):
|
|
|
|
if req.method == 'POST':
|
|
|
|
if req.method == 'POST':
|
|
|
|
indict = req.form
|
|
|
|
indict = req.form
|
|
|
|
elif req.method == 'GET':
|
|
|
|
elif req.method == 'GET':
|
|
|
@ -136,64 +137,73 @@ def get_params(req, params=BASIC_PARAMS):
|
|
|
|
parameters=outdict,
|
|
|
|
parameters=outdict,
|
|
|
|
errors={param: error for param, error in
|
|
|
|
errors={param: error for param, error in
|
|
|
|
iteritems(wrong_params)})
|
|
|
|
iteritems(wrong_params)})
|
|
|
|
raise Error(message=message)
|
|
|
|
raise message
|
|
|
|
|
|
|
|
if hasattr(request, 'params'):
|
|
|
|
|
|
|
|
request.params.update(outdict)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
request.params = outdict
|
|
|
|
return outdict
|
|
|
|
return outdict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def basic_analysis(params):
|
|
|
|
|
|
|
|
response = {"@context":
|
|
|
|
|
|
|
|
[("http://demos.gsi.dit.upm.es/"
|
|
|
|
|
|
|
|
"eurosentiment/static/context.jsonld"),
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
"@base": "{}#".format(request.url.encode('utf-8'))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
"analysis": [{"@type": "marl:SentimentAnalysis"}],
|
|
|
|
|
|
|
|
"entries": []
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if "language" in params:
|
|
|
|
|
|
|
|
response["language"] = params["language"]
|
|
|
|
|
|
|
|
for idx, sentence in enumerate(params["input"].split(".")):
|
|
|
|
|
|
|
|
response["entries"].append({
|
|
|
|
|
|
|
|
"@id": "Sentence{}".format(idx),
|
|
|
|
|
|
|
|
"nif:isString": sentence
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@demo_blueprint.route('/')
|
|
|
|
@demo_blueprint.route('/')
|
|
|
|
def index():
|
|
|
|
def index():
|
|
|
|
return render_template("index.html")
|
|
|
|
return render_template("index.html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_blueprint.route('/contexts/<entity>.jsonld')
|
|
|
|
|
|
|
|
def context(entity="context"):
|
|
|
|
|
|
|
|
return jsonify({"@context": Response.context})
|
|
|
|
|
|
|
|
|
|
|
|
@nif_blueprint.route('/', methods=['POST', 'GET'])
|
|
|
|
@api_blueprint.route('/schemas/<schema>')
|
|
|
|
def api():
|
|
|
|
def schema(schema="definitions"):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
params = get_params(request)
|
|
|
|
return jsonify(read_schema(schema))
|
|
|
|
algo = params.get("algorithm", None)
|
|
|
|
except Exception: # Should be FileNotFoundError, but it's missing from py2
|
|
|
|
specific_params = current_app.senpy.parameters(algo)
|
|
|
|
return Error(message="Schema not found", status=404).flask()
|
|
|
|
logger.debug(
|
|
|
|
|
|
|
|
"Specific params: %s", json.dumps(specific_params, indent=4))
|
|
|
|
def basic_api(f):
|
|
|
|
params.update(get_params(request, specific_params))
|
|
|
|
@wraps(f)
|
|
|
|
response = current_app.senpy.analyse(**params)
|
|
|
|
def decorated_function(*args, **kwargs):
|
|
|
|
in_headers = params["inHeaders"] != "0"
|
|
|
|
print('Getting request:')
|
|
|
|
prefix = params["prefix"]
|
|
|
|
print(request)
|
|
|
|
return response.flask(in_headers=in_headers, prefix=prefix)
|
|
|
|
update_params(request, params=API_PARAMS)
|
|
|
|
except Error as ex:
|
|
|
|
print('Params: %s' % request.params)
|
|
|
|
return ex.message.flask()
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = f(*args, **kwargs)
|
|
|
|
|
|
|
|
except Error as ex:
|
|
|
|
@nif_blueprint.route('/plugins/', methods=['POST', 'GET'])
|
|
|
|
response = ex
|
|
|
|
|
|
|
|
in_headers = request.params["inHeaders"] != "0"
|
|
|
|
|
|
|
|
prefix = request.params["prefix"]
|
|
|
|
|
|
|
|
headers = {'X-ORIGINAL-PARAMS': request.params}
|
|
|
|
|
|
|
|
return response.flask(in_headers=in_headers,
|
|
|
|
|
|
|
|
prefix=prefix,
|
|
|
|
|
|
|
|
headers=headers,
|
|
|
|
|
|
|
|
context_uri=url_for('api.context', entity=type(response).__name__,
|
|
|
|
|
|
|
|
_external=True))
|
|
|
|
|
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_blueprint.route('/', methods=['POST', 'GET'])
|
|
|
|
|
|
|
|
@basic_api
|
|
|
|
|
|
|
|
def api():
|
|
|
|
|
|
|
|
algo = request.params.get("algorithm", None)
|
|
|
|
|
|
|
|
specific_params = current_app.senpy.parameters(algo)
|
|
|
|
|
|
|
|
update_params(request, params=NIF_PARAMS)
|
|
|
|
|
|
|
|
logger.debug("Specific params: %s", json.dumps(specific_params, indent=4))
|
|
|
|
|
|
|
|
update_params(request, specific_params)
|
|
|
|
|
|
|
|
response = current_app.senpy.analyse(**request.params)
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@api_blueprint.route('/plugins/', methods=['POST', 'GET'])
|
|
|
|
|
|
|
|
@basic_api
|
|
|
|
def plugins():
|
|
|
|
def plugins():
|
|
|
|
in_headers = get_params(request, API_PARAMS)["inHeaders"] != "0"
|
|
|
|
|
|
|
|
sp = current_app.senpy
|
|
|
|
sp = current_app.senpy
|
|
|
|
dic = Plugins(plugins=list(sp.plugins.values()))
|
|
|
|
dic = Plugins(plugins=list(sp.plugins.values()))
|
|
|
|
return dic.flask(in_headers=in_headers)
|
|
|
|
return dic
|
|
|
|
|
|
|
|
|
|
|
|
@nif_blueprint.route('/plugins/<plugin>/', methods=['POST', 'GET'])
|
|
|
|
@api_blueprint.route('/plugins/<plugin>/', methods=['POST', 'GET'])
|
|
|
|
@nif_blueprint.route('/plugins/<plugin>/<action>', methods=['POST', 'GET'])
|
|
|
|
@api_blueprint.route('/plugins/<plugin>/<action>', methods=['POST', 'GET'])
|
|
|
|
|
|
|
|
@basic_api
|
|
|
|
def plugin(plugin=None, action="list"):
|
|
|
|
def plugin(plugin=None, action="list"):
|
|
|
|
params = get_params(request, API_PARAMS)
|
|
|
|
|
|
|
|
filt = {}
|
|
|
|
filt = {}
|
|
|
|
sp = current_app.senpy
|
|
|
|
sp = current_app.senpy
|
|
|
|
plugs = sp.filter_plugins(name=plugin)
|
|
|
|
plugs = sp.filter_plugins(name=plugin)
|
|
|
@ -203,21 +213,19 @@ def plugin(plugin=None, action="list"):
|
|
|
|
elif plugin in sp.plugins:
|
|
|
|
elif plugin in sp.plugins:
|
|
|
|
response = sp.plugins[plugin]
|
|
|
|
response = sp.plugins[plugin]
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return Error(message="Plugin not found", status=404).flask()
|
|
|
|
return Error(message="Plugin not found", status=404)
|
|
|
|
if action == "list":
|
|
|
|
if action == "list":
|
|
|
|
in_headers = params["inHeaders"] != "0"
|
|
|
|
return response
|
|
|
|
prefix = params['prefix']
|
|
|
|
|
|
|
|
return response.flask(in_headers=in_headers, prefix=prefix)
|
|
|
|
|
|
|
|
method = "{}_plugin".format(action)
|
|
|
|
method = "{}_plugin".format(action)
|
|
|
|
if(hasattr(sp, method)):
|
|
|
|
if(hasattr(sp, method)):
|
|
|
|
getattr(sp, method)(plugin)
|
|
|
|
getattr(sp, method)(plugin)
|
|
|
|
return Response(message="Ok").flask()
|
|
|
|
return Response(message="Ok")
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return Error(message="action '{}' not allowed".format(action)).flask()
|
|
|
|
return Error(message="action '{}' not allowed".format(action))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import config
|
|
|
|
import config
|
|
|
|
|
|
|
|
|
|
|
|
app.register_blueprint(nif_blueprint)
|
|
|
|
app.register_blueprint(api_blueprint)
|
|
|
|
app.debug = config.DEBUG
|
|
|
|
app.debug = config.DEBUG
|
|
|
|
app.run(host='0.0.0.0', port=5000)
|
|
|
|
app.run(host='0.0.0.0', port=5000)
|
|
|
|