def bot(): response = {"success": False} if flask.request.method == "POST": data = flask.request.get_json() if "path" not in data: return "missing path" print(data) try: sound_file_path = download_remote_file(data["path"]) prediction = get_prediction(sound_file_path) # indicate that the request was a success response["success"] = True response["predictions"] = prediction print(response) except Exception as e: return flask.jsonify({"error": e}) # upload prediction to cloud storage storage = StorageFactory.cloud() storage.upload_prediction(source=sound_file_path, model_type=MODEL_TYPE, model_num=MODEL_NUM, status=response["predictions"]) return flask.jsonify(response)
def predict(): # initialize the data dictionary that will be returned from the # view response = {"success": False} # ensure an image was properly uploaded to our endpoint if flask.request.method == "POST": # make sure we received a file in the request if not flask.request.files.get("file"): response["error"] = "missing file key: 'file'" return flask.jsonify(response) # read the sound file status, sound_file = save_file_from_request(flask.request) prediction = get_prediction(sound_file) response["predictions"] = prediction # indicate that the request was a success response["success"] = True # upload prediction to cloud storage storage = StorageFactory.cloud() storage.upload_prediction(source=sound_file, model_type=MODEL_TYPE, model_num=MODEL_NUM, status=response["predictions"]) # return the data dictionary as a JSON response return flask.jsonify(response)
def in_cloud_cache(file_path): storage = StorageFactory.cloud() # get the file from the storage if possible if storage.exists(file_path): storage.save_file() return file_path
def cloud_upload(self, model_type_path, exp_name, exp_id): """ Uploads the local models folder to the cloud :param model_type_path: :param exp_name: :param exp_id: :return: """ storage = StorageFactory.cloud() storage.save_models_folder(model_type_path, exp_name, exp_id)
def load(from_cloud=True): # The current served model based on the experiment type global model storage = StorageFactory.default() file_path = storage.load_model(MODEL_TYPE, MODEL_NUM) model = load_model(file_path) # BUG fix - initializing the model with an empty vector model.predict(np.zeros((1, 13, 30, 1)))
def download(self, url, output_file, upload=True): # create the output file path sound_file_path = os.path.join(self.destination_folder, "{}.wav".format(output_file)) if self.debug: print('downloading {} to {}'.format(output_file, sound_file_path)) (filename, headers) = urllib.request.urlretrieve(url) sound = AudioSegment.from_mp3(filename) # saving the file in wav format converted from mp3 sound.export(sound_file_path, format="wav") # upload to cloud storage if upload: storage = StorageFactory.cloud() storage.upload_wav(sound_file_path)
def save_file_from_request(request): """ Get a file object in the request and save it locally for predicition :param request: :return: """ file = request.files.get('file') if not file: full_path = None file_uploaded = False return file_uploaded, full_path storage = StorageFactory.local() full_path = storage.save_file(file, root=app.config['UPLOAD_FOLDER']) file_uploaded = True return file_uploaded, full_path
def test_connect_to_cloud_storage(): storage = StorageFactory.cloud() print(storage)