-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
58 lines (46 loc) · 1.49 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
import ast
import json
import pickle
import random
import os
import numpy as np
from bson.json_util import dumps
from flask import Flask, Response, jsonify, request
from flask_cors import CORS
from flask_pymongo import PyMongo
from emotion_classify.config.config import shared_components, BASE_DIR
from services.log import (
display_page,
fetch_log,
fetch_logs,
predict,
remove_log,
update_log,
)
def init_app():
app = Flask(__name__)
app = create_route(app)
model = pickle.load(
open(os.path.join(BASE_DIR, "emotion_classify/pickle_files/model_nb.pkl"), "rb")
)
app.config.from_pyfile(os.path.join(BASE_DIR, "emotion_classify/config/config.cfg"))
CORS(app)
mongo = PyMongo(app)
# Select the database
db = mongo.db
shared_components["db"] = db
return app
def create_route(app):
"""
Adds different rules to the urls
"""
app.add_url_rule(rule="/", view_func=display_page, methods=["GET"])
app.add_url_rule(rule="/api/predict", view_func=predict, methods=["POST"])
app.add_url_rule(rule="/api/logs", view_func=fetch_logs, methods=["GET"])
app.add_url_rule(rule="/api/log/<log_id>", view_func=fetch_log, methods=["GET"])
app.add_url_rule("/api/log/<log_id>", view_func=update_log, methods=["UPDATE"])
app.add_url_rule("/api/log/<log_id>", view_func=remove_log, methods=["DELETE"])
return app
app = init_app()
if __name__ == "__main__":
app.run("0.0.0.0", 3000, debug=True) # Automatic run of script