-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (57 loc) · 1.79 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
# General
import os
import json
import requests
# Flask
from flask import Flask, render_template, flash, request, url_for, jsonify
# Celery
from celery import Celery
# API
import api
# Configure app
app = Flask(__name__)
app.config.update(
CELERY_BROKER_URL='redis://localhost:6379',
CELERY_RESULT_BACKEND='redis://localhost:6379'
)
# Configure Celery
celery = Celery(
app.name,
broker=app.config['CELERY_BROKER_URL']
)
celery.conf.update(app.config)
@celery.task
def APICall(data={}):
""" Asynchronous call to ChompChap API """
# print(data)
# result = api.Predict(data)
result = requests.get("https://9c23bd01.ngrok.io/getrec",params={"words":" ".join(data["words"]), "latitude": data["coords"]["lat"], "longitude": data["coords"]["lng"]})
return { "result": result.json(), "status": "SUCCESS" }
@app.route("/", methods=['GET', 'POST'])
def Index():
""" Main page """
return render_template("index.html")
@app.route("/features", methods=['GET'])
def Features():
""" Feature page """
return render_template("features.html")
@app.route("/query", methods=['POST'])
def Query():
""" Handle requests """
parsed = json.loads(request.data)
task = APICall.apply_async(kwargs={ "data": parsed })
return jsonify({}), 202, { "Location": url_for("Status", taskID=task.id) }
@app.route("/status/<taskID>")
def Status(taskID):
task = APICall.AsyncResult(taskID)
response = { "state": task.state }
if task.state != "FAILURE" and task.info and "result" in task.info:
response["result"] = task.info["result"]
return jsonify(response)
if __name__ == "__main__":
# Load model
# api.Start()
# Start App
app.debug = False
port = int(os.environ.get("PORT", 3000))
app.run(host="0.0.0.0", port=port, use_reloader=True)