def model_predict(function, name, version=1): ctx.request = request #userip=request.remote_addr if not does_model_exist(name, version): return jsonify({ 'success': False, 'message': 'The specified model is not deployed.' }) model_info = validation_model(name, version) if request.headers['Content-Type'] == 'text/plain': req_data = request.data elif request.headers['Content-Type'] == 'application/json': req_data = json.dumps(request.json) else: req_data = 'some thing wrong!' if model_info: if model_info['activated'] == True: fun_name = ('/%s') % (str(function).decode('utf-8')) fun_path = fun_name result = algo_engine.call_func(fun_path, **request.json) return jsonify({'success': True, 'response': result}) else: return jsonify({ 'success': False, 'message': 'The specified model is deactivated.' }) else: return jsonify({ 'success': False, 'message': 'The specified model is not deployed.' }) ctx.request = None
def activate(name, version=1): """ Expose a model via the API model server. """ if not does_model_exist(name, version): print 'The specified model/version doesn\'t exist!' return jsonify({ 'success': False, 'message': 'The specified model/version doesn\'t exist!' }) update_model(name, version, {'activated': True}) return jsonify({ 'success': True, 'message': 'Successfully activated the model\'s API.\nPlease restart your server for these changes to take effect.' })
def delete(name, version=1): """ Delete a specific model version. """ if not does_model_exist(name, version): print 'The specified model/version doesn\'t exist!' return jsonify({ 'success': False, 'message': 'The specified model/version doesn\'t exist!' }) # delete the model from the db delete_model_from_db(name, version) # delete the model from the file storage delete_model_storage(name, version) return jsonify({ 'success': True, 'message': 'The model (v%d) and its files were successfully deleted.' % version })
def save_model_to_db(name, model_function, version=1, activated=True, last_deployed=None, model_desc='defalut description'): #model = self.model model_obj = { 'name': name, 'version': version, 'activated': activated, 'path': get_model_dir(name, version) } model_obj['last_deployed'] = last_deployed model_obj['model_desc'] = model_desc model_obj['model_func'] = model_function # Generate an input spec for this model # Check if this model/version already exists in the db. # If it doesn't, store this model's metadata in the db. if not does_model_exist(name, version): create_new_model(model_obj) else: return (False, 'Model/version already exists.') return (True, None)