def authorize(self): """Shows basic usage of the Drive v3 API. Prints the names and ids of the first 10 files the user has access to. """ creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) else: error("token.pickle doesn't exist") # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: # notify("Login again", crashed=True) error("Creds expired. Refreshing....") creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) info("Authorized with Drive API") self.service = build('drive', 'v3', credentials=creds)
def delete_duplicate(self): info("Deleting duplicate files in drive") try: self.authorize() pageToken = None songs = [] dups = {} while True: results = self.service.files().list( pageToken=pageToken, fields="nextPageToken, files(id,name,modifiedTime)", orderBy="modifiedTime asc").execute() items = results.get('files', []) pageToken = results.get('nextPageToken', None) songs += items if pageToken is None: break found_dups = False for song in songs: if song['name'] in dups: found_dups = True dups[song["name"]].append({ 'name': song['name'], 'id': song['id'], 'modifiedTime': song['modifiedTime'] }) else: dups[song["name"]] = [{ 'name': song['name'], 'id': song['id'], 'modifiedTime': song['modifiedTime'] }] if found_dups: info("No duplicate files found") for dup in dups: if len(dups[dup]) > 1: others = sorted(dups[dup], key=lambda x: x['modifiedTime'])[1:] for inst in others: info(("Deleting duplicate song: {}".format( inst['name']))) self.service.files().delete( fileId=inst['id']).execute() except Exception: import traceback traceback.print_exc() stacktrace = traceback.format_exc() error(stacktrace)
def upload(self, key, name, file_path): try: self.authorize() file_metadata = {'name': name, 'key': key} media = MediaFileUpload(file_path, mimetype='audio/mpeg') result_file = self.service.files().create(body=file_metadata, media_body=media, fields='id').execute() info("Song {} uploaded to drive".format(name)) self.delete_duplicate() return True if result_file else False except Exception: import traceback traceback.print_exc() stacktrace = traceback.format_exc() error(stacktrace)
def help(name): error("usage: {} <dataset_path>".format(name)) exit()
def save_hook(weights_path): return tf.keras.callbacks.ModelCheckpoint(weights_path, 'accuracy', save_best_only=True, save_weights_only=True, mode='max') ####################################################### if __name__ == '__main__': help(argv[0]) if len(argv) < 2 else None (training_ds, validation_ds, test_ds) = dataset_load(argv[1]) if (training_ds, validation_ds, test_ds) == (None, None, None): error('Error in datasets loading: check if the paths are correct!') exit() weights_path = './weights_result' training_model = MCModel(training_ds.num_classes, 200, 1) (TB_logs_dir, TB_callback) = TB_hooks() checkpoint_callback = save_hook(weights_path) if os.path.exists('{}.index'.format(weights_path)): debug('weights file exists at {}'.format(weights_path)) try: training_model.load_weights(weights_path) training_model.compile( optimizer=tf.keras.optimizers.Adam(), loss=tf.keras.losses.CategoricalCrossentropy(),
info("{} songs added".format(len(added))) check_old_songs_count += 1 delete_log_file_count += 1 if check_old_songs_count == check_old_songs_interval: info("Checking old songs for new links...") check_old_songs() check_old_songs_count = 0 if delete_log_file_count == delete_log_file_interval: info("Resetting log file...") time.sleep(0.5) delete_log_file_count = 0 info("Waiting for 10 seconds before polling...") time.sleep(poll_interval) except Exception as err: import traceback traceback.print_exc() stacktrace = traceback.format_exc() error(stacktrace) msg = "Crashed ... Restarting in 10 seconds" check_old_songs_count = 0 delete_log_file_count = 0 print(msg) # if not stacktrace.__contains__("Temporary failure in name resolution"): # notify(msg, crashed=True) time.sleep(10)
def help(name): error("usage: {} <image_path> [<model_weights>]".format(name)) exit()
] if __name__ == '__main__': help(argv[0]) if len(argv) < 2 else None weigths_path = argv[2] if argv[2] else './weights_result' classificator = MalwareClassificator(6, 200, family_labels) try: if len(argv) >= 3: classificator.load_weights(argv[2]) else: classificator.load_weights() except IOError as e: error(str(e)) exit() if not '-q' in argv: debug('model correctly loaded') debug('loading image {}'.format(argv[1])) try: prediction = classificator.predict(argv[1]) except Exception as e: error(str(e)) exit() classificator.model.model().summary() exit()