-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
108 lines (88 loc) · 3.68 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from flask import Flask, jsonify, render_template, request
from flask_cors import CORS
from flask_caching import Cache
import redis
from core.autocomplete import AutoComplete
app = Flask(__name__)
CORS(app)
cache = Cache(app, config={
'CACHE_TYPE': 'redis',
'CACHE_KEY_PREFIX': 'server1',
'CACHE_REDIS_HOST': 'localhost',
'CACHE_REDIS_PORT': '6379',
'CACHE_REDIS_URL': 'redis://localhost:6379/'
})
app.config["MONGO_URI"] = "mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.0.2"
@app.route('/')
def get_index_html_template():
""" Standard implementation of Flask render template
While the plan of the software is to be decoupled
and work with any frontend, this is a roughly built
frontend that helps to test the app.
"""
return render_template("index.html")
@app.route('/train', methods=['POST'])
def train_corpus():
"""
Returns a map (dict) of [str, str]
This calls the train method in `autocomplete` on the input corpus
The function performs the following steps:
- receives the request from the client
- calls the train method of the AutoComplete class of which,
if successful, will add the corpus in the appropriate
format to the MaxWordGraph and CorpusGraph collections
- it returns these result of the train operation as a json
object to the client
Args (retrieved via the POST method):
corpus: str - the string intended for adding
Returns
Dict: a map/dict[str,List[str]] with 'result' as key and
response as value. This response is the status of training
the corpus
"""
if request.method == 'POST':
data = request.get_json()['input_corpus']
response = AutoComplete( app.config["MONGO_URI"] ).train(data)
return jsonify({'result': response})
@app.route('/dropdb')
def drop_db():
"""
Returns a map (dict) of [str, str]
This function drops both collections from the db
The function performs the following steps:
- receives the request from the client
- calls the drop method of the AutoComplete class of which,
if successful, will drop the MaxWordGraph and CorpusGraph
collections
- it returns these documents as a json object to the client
Args:
None
Returns
Dict: a map/dict[str,List[str]] with 'result' as key and
response as value. This response is the status of dropping
the two collections from the db
"""
response = AutoComplete( app.config["MONGO_URI"] ).drop_db()
return jsonify({'result': response})
@app.route('/complete/<phrase>')
@cache.cached(timeout=10)
def complete(phrase):
"""
Returns a map (dict) of [str, str]
This function drops both collections from the db
The function performs the following steps:
- receives the request data `question` from the client
- calls the get_answer method of the Info which will
fetch the answer to the question and return it
- it returns the answer as a json object to the client
Args:
question: str - Question asked from the client
Returns
Dict: [str,List[str]] with 'result' as key and response
as value. This response is the answer to the question
received from the client
"""
response = AutoComplete( app.config["MONGO_URI"] ).complete( phrase )
return jsonify({'result': response})
if __name__ == "__main__":
app.run()