def predict(): """ basic predict function for the API """ ## input checking if not request.json: print("ERROR: API (predict): did not receive request data") return jsonify([]) if 'country' not in request.json: print( "ERROR API (predict): received request, but no 'country' found within" ) return jsonify([]) ## set the test flag test = False if 'mode' in request.json and request.json['mode'] == 'test': test = True ## extract the query parameters country = request.json['country'] year = request.json['year'] month = request.json['month'] day = request.json['day'] ## load model data_dir = os.path.join("data", "cs-train") all_data, all_models = model_load(data_dir=data_dir) model = all_models[country] if not model: print("ERROR: model is not available") return jsonify([]) _result = model_predict(country, year, month, day, test=test) result = {} ## convert numpy objects to ensure they are serializable for key, item in _result.items(): if isinstance(item, np.ndarray): result[key] = item.tolist() else: result[key] = item return (jsonify(result))
def train(): """ basic predict function for the API the 'mode' give you the ability to toggle between a test version and a production verion of training """ test = False if 'mode' in request.json: if request.json['mode'] == 'test': test = True print("... training model") data_dir = os.path.join(THIS_DIR, "data", "cs-train") try: model_train(data_dir, test=test) print("... training complete") # reload models and data after re-train print("... reloading models in cache") global_data, global_models = model_load(training=False) return (jsonify(True)) except Exception as e: print("ERROR API (train): model_train returned: {}".format(str(e))) return jsonify([]), 400
import socketserver, sys from pprint import pprint import json from utils import option from utils.convert import convert_File from model.model import model_load opt = option.Options() IP = opt.host_ip PORT = int(opt.host_port) py2java_path = opt.py2java_path model = model_load() class SingleTCPHandler(socketserver.BaseRequestHandler): "One instance per connection. Override handle(self) to customize action." def handle(self): # self.request is the client connection data = self.request.recv(1024) # clip input at 1Kb text = data.decode('utf-8') # pprint(json.loads(text)) inObj = json.loads(text) # input check pprint(inObj) isSuccess = False output_path = "" try:
def startup(): global global_data, global_models print(".. loading models") global_data, global_models = model_load(training=False) print(".. all models loaded") return redirect(url_for('landing'))