-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
51 lines (38 loc) · 1.2 KB
/
api.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
import json
import flask
from flask import jsonify, request
from flask_cors import CORS, cross_origin
from SearchEngine import SearchEngine
# Create Flask application method
app = flask.Flask(__name__)
cors = CORS(app)
# Start debugger
app.config["DEBUG"] = True
# instantiate and initialize model
engine = SearchEngine()
@app.route('/', methods=["GET"])
def home():
return "<h1>Embeddings search Engine</h1><p>Dual Space Word Embeddings Semantic Search Engine</p>"
@app.route('/status', methods=["GET"])
@cross_origin()
def status():
"""Get search engine status"""
return jsonify(status=str(engine.get_status()))
@app.route('/train', methods=["POST"])
@cross_origin()
def train():
"""Train semantic search model on API documentation"""
data = request.get_data()
entries = json.loads(data)['entries']
engine.train(entries)
response = jsonify(status=str(engine.get_status()))
return response
@app.route('/search', methods=["GET"])
@cross_origin()
def search():
"""Search a query on trained model"""
query_parameters = request.args
query = query_parameters.get('query')
results = engine.search(query, False)
return json.dumps(results)
app.run(use_reloader=False)