-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·85 lines (69 loc) · 2.62 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
from flask import Flask, request
from flask_cors import CORS
from flask_restplus import Api, Resource, reqparse
from services.find_folder import find_folders, change_document_folder, setup_all, find_user_folder_representation, add_document
from services.convert_file_to_text import read_file
setup_all()
def default_doc():
return {
'responses': {
'200': 'Success',
'500': 'Internal Failure',
'503': 'Service Unavailable'
}}
app = Flask(__name__, static_url_path='/app', static_folder='web/drop')
CORS(app)
api = Api(
app,
version='1.0',
title='Our awesome api',
)
api_ns = api.namespace('api', description='Api operations')
classification_parser = reqparse.RequestParser()
classification_parser.add_argument('text', type=str, default=None)
@api_ns.route('/main')
class OurResource(Resource):
@classmethod
@api.doc(**default_doc())
@api.expect(classification_parser)
def get(cls):
arguments = classification_parser.parse_args()
if arguments['text'] is None:
return {'error': 'missing text'}, 503
content = arguments['text']
user_id = 3
folder_names, stored_document_vectors = find_user_folder_representation(user_id)
paths, vector = find_folders(content, folder_names, stored_document_vectors)
return {'input': content, 'paths': paths}, 200
@api_ns.route('/file')
class FileResource(Resource):
@classmethod
@api.doc(**default_doc())
def post(cls):
file = request.files.get('file')
if file is None:
return {'error': 'missing file'}, 503
content = read_file(file)
user_id = 3
folder_names, stored_document_vectors = find_user_folder_representation(user_id)
paths, vector = find_folders(content, folder_names, stored_document_vectors)
document_id = add_document(user_id, file.filename, content, vector)
return {'input': content, 'paths': paths, 'id': document_id}, 200
add_path_parser = reqparse.RequestParser()
add_path_parser.add_argument('path', type=str, default=None)
add_path_parser.add_argument('id', type=int, default=None)
@api_ns.route('/add_path')
class AddPath(Resource):
@classmethod
@api.doc(**default_doc())
@api.expect(add_path_parser)
def post(cls):
arguments = add_path_parser.parse_args()
id_ = arguments.get('id')
path = arguments.get('path')
if id_ is None:
return {'error': 'missing id'}, 503
if path is None:
return {'error': 'missing path'}, 503
change_document_folder(id_, path)
return {'result': True}, 200