-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
64 lines (45 loc) · 1.62 KB
/
server.py
File metadata and controls
64 lines (45 loc) · 1.62 KB
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
from flask import Flask, request, jsonify
from redis import Redis
from rq import Queue
from worker import predict_task
import hashlib
import json
# Create a Flask app
app = Flask(__name__)
# Connect to Redis on host="redis" and port=6379
redis_conn = _____
queue = Queue(connection=redis_conn)
# Create a cache key from the input data
def create_cache_key(input_data):
data_str = json.dumps(input_data, sort_keys=True)
return hashlib.md5(data_str.encode("utf-8")).hexdigest()
@app.route("/predict", methods=["POST"])
def predict():
# Get the data from the request
data = request.get_json(force=True)
input_data = data["input"]
# Check the cache
cache_key = create_cache_key(input_data)
# Use the Redis connection redis_conn to attempt to get the cache_key
cached_result = _____
if cached_result:
# Result is cached
return jsonify({"prediction": int(cached_result)})
# Result is not cached
# Enqueue the task if not in cache
job = queue.enqueue(predict_task, input_data)
# Use the Redis connection redis_conn to store the result in the cache once it's done
def store_result(job, connection, result, *args, **kwargs):
_____
job.on_success = store_result
return jsonify({"job_id": job.get_id()})
@app.route("/result/<job_id>", methods=["GET"])
def get_result(job_id):
# Fetch the job from the queue using the job_id to see the status
job = _____
if job.is_finished:
return jsonify({"prediction": job.result})
else:
return jsonify({"status": job.get_status()}), 202
if __name__ == "__main__":
app.run(debug=True)