@utils.auth.requires_login
@blueprint.route('/upload_archive', methods=['POST'])
def upload_archive():
 files = flask.request.files
 archive_file = get_tempfile(files['archive'], '.archive')
 if tarfile.is_tarfile(archive_file):
  archive = tarfile.open(archive_file, 'r')
  names = archive.getnames()
 elif zipfile.is_zipfile(archive_file):
  archive = zipfile.ZipFile(archive_file, 'r')
  names = archive.namelist()
 else:
  return (flask.jsonify({'status': 'Incorrect Archive Type'}), 500)
 if ('info.json' in names):
  tempdir = tempfile.mkdtemp()
  labels_file = None
  archive.extractall(path=tempdir)
  with open(os.path.join(tempdir, 'info.json')) as data_file:
   info = json.load(data_file)
  (valid, key) = validate_archive_keys(info)
  if (valid is False):
   return (flask.jsonify({'status': (("Missing Key '" + key) + "' in info.json")}), 500)
  weights_file = os.path.join(tempdir, info['snapshot file'])
  if ('model file' in info):
   model_file = os.path.join(tempdir, info['model file'])
  elif ('network file' in info):
   model_file = os.path.join(tempdir, info['network file'])
  else:
   return (flask.jsonify({'status': 'Missing model definition in info.json'}), 500)
  if ('labels file' in info):
   labels_file = os.path.join(tempdir, info['labels file'])
  job = PretrainedModelJob(weights_file, model_file, labels_file, info['framework'], username=utils.auth.get_username(), name=info['name'])
  scheduler.add_job(job)
  job.wait_completion()
  shutil.rmtree(tempdir, ignore_errors=True)
  return (flask.jsonify({'status': 'success'}), 200)
 else:
  return (flask.jsonify({'status': 'Missing or Incorrect json file'}), 500)

@ utils . auth . requires_login @ blueprint . route ( '/upload_archive' , methods = [ 'POST' ] ) def upload_archive ( ) : files = flask . request . files archive_file = get_tempfile ( files [ 'archive' ] , '.archive' ) if tarfile . is_tarfile ( archive_file ) : archive = tarfile . open ( archive_file , 'r' ) names = archive . getnames ( ) elif zipfile . is_zipfile ( archive_file ) : archive = zipfile . ZipFile ( archive_file , 'r' ) names = archive . namelist ( ) else : return ( flask . jsonify ( { 'status' : 'Incorrect Archive Type' } ) , 500 ) if ( 'info.json' in names ) : tempdir = tempfile . mkdtemp ( ) labels_file = None archive . extractall ( path = tempdir ) with open ( os . path . join ( tempdir , 'info.json' ) ) as data_file : info = json . load ( data_file ) ( valid , key ) = validate_archive_keys ( info ) if ( valid is False ) : return ( flask . jsonify ( { 'status' : ( ( "Missing Key '" + key ) + "' in info.json" ) } ) , 500 ) weights_file = os . path . join ( tempdir , info [ 'snapshot file' ] ) if ( 'model file' in info ) : model_file = os . path . join ( tempdir , info [ 'model file' ] ) elif ( 'network file' in info ) : model_file = os . path . join ( tempdir , info [ 'network file' ] ) else : return ( flask . jsonify ( { 'status' : 'Missing model definition in info.json' } ) , 500 ) if ( 'labels file' in info ) : labels_file = os . path . join ( tempdir , info [ 'labels file' ] ) job = PretrainedModelJob ( weights_file , model_file , labels_file , info [ 'framework' ] , username = utils . auth . get_username ( ) , name = info [ 'name' ] ) scheduler . add_job ( job ) job . wait_completion ( ) shutil . rmtree ( tempdir , ignore_errors = True ) return ( flask . jsonify ( { 'status' : 'success' } ) , 200 ) else : return ( flask . jsonify ( { 'status' : 'Missing or Incorrect json file' } ) , 500 )
a wrapper around the actual upload view function to clean up the temporary file afterwards .