Skip to content

Commit

Permalink
Add caching for classification
Browse files Browse the repository at this point in the history
  • Loading branch information
schollz committed Mar 27, 2018
1 parent f2e6c94 commit 5420665
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions server/ai/src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import base58
import logging

from expiringdict import ExpiringDict


# create logger with 'spam_application'
logger = logging.getLogger('server')
Expand All @@ -25,7 +27,7 @@

from learn import AI

cache = {}
ai_cache = ExpiringDict(max_len=100000,max_age_seconds=60)


def to_base58(family):
Expand All @@ -47,12 +49,15 @@ def classify():
fname = os.path.join(data_folder, to_base58(
payload['sensor_data']['f']) + ".find3.ai")

ai = AI(to_base58(payload['sensor_data']['f']), data_folder)
logger.debug("loading {}".format(fname))
try:
ai.load(fname)
except FileNotFoundError:
return jsonify({"success": False, "message": "could not find '{p}'".format(p=fname)})
ai = ai_cache.get(payload['sensor_data']['f'])
if ai == None:
ai = AI(to_base58(payload['sensor_data']['f']), data_folder)
logger.debug("loading {}".format(fname))
try:
ai.load(fname)
except FileNotFoundError:
return jsonify({"success": False, "message": "could not find '{p}'".format(p=fname)})
ai_cache[payload['sensor_data']['f']] = ai

classified = ai.classify(payload['sensor_data'])

Expand Down Expand Up @@ -84,6 +89,7 @@ def learn():

ai.save(os.path.join(data_folder, to_base58(
payload['family']) + ".find3.ai"))
ai_cache[payload['family']] = ai
return jsonify({"success": True, "message": "calibrated data"})


Expand Down

0 comments on commit 5420665

Please sign in to comment.