-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
92 lines (65 loc) · 2.55 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
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
# app.py
from flask import Flask # import flask
from flask import jsonify
from flask import request # import flask
from orchestrator import Orchestrator
from settings.config import Config
from settings.factory import Factory
# from vectorizers.img_to_vec import Img2Vec
app = Flask(__name__) # create an app instance
# img2vec = Img2Vec()
port = 8082
host = '0.0.0.0'
debug = True
config = Config()
components_factory = Factory(config)
indexer = components_factory.get_indexer()
content_vectors = components_factory.get_content_vector_store()
result_mapper = components_factory.get_result_mapper()
writer = components_factory.get_writer()
reader = components_factory.get_reader()
global_store = components_factory.get_global_store()
# image_utils = ImageUtils(img2vec)
@app.route("/api/v1/train", methods=['POST']) # at the end point /
def training(): # call method training
indexer.build_index(content_vectors)
return "created indexes successfully"
@app.route("/api/v1/query", methods=['POST']) # at the end point /
def query(): # call method training
rq = request.json
if "n" in rq:
nn_article_count = rq["n"]
else:
nn_article_count = config.default_nn()
if rq.get("vector"):
result = indexer.find_NN_by_vector(rq.get("vector"), nn_article_count)
result = result_mapper.map(result)
elif "id" in rq:
ids = rq.get("id")
results = []
for id in ids:
result = indexer.find_NN_by_id(id, nn_article_count)
results.append(result)
result = [result_mapper.map(i) for i in results]
result = {key: val for i in result for key, val in i.items()}
else:
return "Bad request. Either 'id' or 'vector' should be present"
response = jsonify(result)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
# @app.route("/api/v1/content", methods=['POST']) # at the end point /
# def vectorize_and_add():
# content_list = request.json
# content_vector_list = image_utils.vectorize_images(content_list)
# content_vectors.add_content_vectors(content_vector_list)
# indexer.build_index(content_vectors)
# return "created indexes successfully"
@app.route("/api/v1/content-vectors", methods=['POST']) # at the end point /
def add_vectors():
content_list = request.json
content_vectors.add_content_vectors(content_list)
return "created indexes successfully"
orchestrator = Orchestrator(indexer, content_vectors, global_store, writer, reader, config)
orchestrator.start()
if __name__ == "__main__": # on running python app.py
app.run(host=host, port=port, debug=debug, use_reloader=False)