41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
import datetime
|
|
import json
|
|
from pathlib import Path
|
|
import spacy_w2v
|
|
|
|
from flask import Flask, make_response, render_template, request, send_file
|
|
|
|
shared_solution_key = Path(__file__).parent.joinpath(
|
|
'shared_solution_key.txt').read_text(encoding='utf-8').strip()
|
|
app = Flask(__name__, instance_relative_config=True)
|
|
|
|
|
|
@app.route('/', methods=['HEAD', 'OPTIONS', 'GET'])
|
|
def index():
|
|
return render_template('index.html', datetime=datetime)
|
|
|
|
|
|
@app.route('/favicon.ico', methods=['HEAD', 'OPTIONS', 'GET'])
|
|
def favicon():
|
|
return send_file(Path('favicon.ico'))
|
|
|
|
|
|
@app.route('/convert.json', methods=['HEAD', 'OPTIONS', 'GET'])
|
|
def convert():
|
|
key = request.args.get('key', '').strip()
|
|
if shared_solution_key != key:
|
|
raise ValueError('Wrong access key')
|
|
snt = request.args.get('sentence', '').strip()
|
|
lang = request.args.get('lang', '').strip()
|
|
if lang not in ('en', 'es', 'pt'):
|
|
raise ValueError
|
|
obj = spacy_w2v.convert(lang, snt)
|
|
resp_text = json.dumps(obj, sort_keys=True, indent=4)
|
|
resp = make_response(resp_text.encode('utf-8'), 200)
|
|
resp.mimetype = 'application/json'
|
|
resp.mimetype_params['charset'] = 'utf-8'
|
|
return resp
|