def separate_into_parts(self, input_path: str, output_path: Path): # Check if we downloaded a webpage instead of the actual model file file_exists = self.model_file_path.is_file() mime = None if file_exists: mime = magic.from_file(str(self.model_file_path), mime=True) download_and_verify(MODEL_URL, self.model_dir, self.model_file_path, force=(file_exists and mime == 'text/html')) parts = { 'vocals': True, 'drums': True, 'bass': True, 'other': True } estimates = self.get_estimates(input_path, parts) # Export all source MP3s in parallel pool = Pool() tasks = [] output_path = Path(output_path) for name, estimate in estimates.items(): filename = f'{name}.mp3' print(f'Exporting {name} MP3...') task = pool.apply_async(self.audio_adapter.save, (output_path / filename, estimate, self.sample_rate, 'mp3', self.bitrate)) tasks.append(task) pool.close() pool.join()
def separate_into_parts(self, input_path: str, output_path: str): """Creates a dynamic mix :param input_path: Input path :param output_path: Output path """ input_path = Path(input_path) output_path = Path(output_path) model = self.get_model() raw_sources = self.apply_model(model, input_path) # Export all source MP3s in parallel pool = Pool() tasks = [] for source, name in zip(raw_sources, ['drums', 'bass', 'other', 'vocals']): source = source.cpu().transpose(0, 1).numpy() filename = f'{name}.mp3' print(f'Exporting {name} MP3...') task = pool.apply_async(self.audio_adapter.save, (output_path / filename, source, self.sample_rate, 'mp3', self.bitrate)) tasks.append(task) try: pool.close() pool.join() except SoftTimeLimitExceeded as e: pool.terminate() raise e
def separate_into_parts(self, input_path: str, output_path: str): """Creates a dynamic mix :param input_path: Input path :param output_path: Output path """ input_path = Path(input_path) output_path = Path(output_path) self.download_and_verify() raw_sources = self.apply_model(input_path) # Export all source MP3s in parallel pool = Pool() tasks = [] for source, name in zip(raw_sources, ['drums', 'bass', 'other', 'vocals']): source = (source * 2**15).clamp_(-2**15, 2**15 - 1).short() source = source.cpu().transpose(0, 1).numpy() filename = f'{name}.mp3' print(f'Exporting {name} MP3...') task = pool.apply_async(encode_mp3, (source, str(output_path / filename), self.bitrate, self.verbose)) tasks.append(task) try: pool.close() pool.join() except SoftTimeLimitExceeded as e: pool.terminate() raise e
def separate_into_parts(self, input_path: str, output_path: Path): self.download_and_verify() ctx = get_extension_context(self.context) nn.set_default_context(ctx) nn.set_auto_forward(True) audio, _ = self.audio_adapter.load(input_path, sample_rate=self.sample_rate) if audio.shape[1] > 2: warnings.warn('Channel count > 2! ' 'Only the first two channels will be processed!') audio = audio[:, :2] if audio.shape[1] == 1: print('received mono file, so duplicate channels') audio = np.repeat(audio, 2, axis=1) print('Separating...') estimates = separate(audio, model_path=str(self.model_file_path), niter=self.iterations, alpha=self.alpha, softmask=self.softmask, residual_model=self.residual_model) output_path = Path(output_path) # Export all source MP3s in parallel pool = Pool() tasks = [] for name, estimate in estimates.items(): filename = f'{name}.mp3' print(f'Exporting {name} MP3...') task = pool.apply_async(self.audio_adapter.save, (os.path.join( output_path, filename), estimate, self.sample_rate, 'mp3', self.bitrate)) tasks.append(task) pool.close() pool.join()
def separate_into_parts(self, input_path: str, output_path: Path): self.download_and_verify() estimates = self.get_estimates(input_path) # Export all source MP3s in parallel pool = Pool() tasks = [] output_path = Path(output_path) for name, estimate in estimates.items(): filename = f'{name}.mp3' print(f'Exporting {name} MP3...') task = pool.apply_async(self.audio_adapter.save, (os.path.join( output_path, filename), estimate, self.sample_rate, 'mp3', self.bitrate)) tasks.append(task) pool.close() pool.join()
class RenderAndSave(): def __init__(self, async_mode=True): """ :param async_mode: False - используется для теста """ self.async_mode = async_mode @staticmethod def get_available_sites(news_portal=None, news_dep=None) -> list: """ Получение списка доступных сайтов :param news_portal: новостной портал, по активным тематическим разделам которого будет выполнен поиск, если не задан - поиск ведется по всем активным :param news_dep: новостной раздел портала :return: query """ if news_portal: return [ Site.objects.filter(is_active=True, news_portal=news_portal, news_department=news_dep) ] return [site for site in Site.objects.filter(is_active=True)] @staticmethod def _save_postgr(results): """ Сохранение в Postgresql результатов парсинга :param results: список с корежами, содержащими словарь, описывающий результат парсинга отдельной статьи :return: """ for part in results: for result in part: Article.objects.get_or_create( link=result['news_link'], has_prices=get_has_prices(result['main_text']), has_percents=get_has_percents(result['main_text']), frequent_words=get_frequent_words(result['main_text']), content=result) def _async_worker(self, sites_list) -> tuple: """ Запуск парсера в асинхронном режиме. :param sites_list: список сайтов для анализа :return: спискок кортежей с резульатами парсинга """ self.process_pool = Pool(processes=settings.PROCESS_AMOUNT) results = [ self.process_pool.apply_async( settings.AVAILABLE_RENDERS[site.news_portal], args=(site.target_url, )) for site in sites_list ] clean_data = [i.get() for i in results] self.process_pool.close() self.process_pool.join() return clean_data def _sync_worker(self, site) -> list: """ Запуск парсера в синхронном режиме. :param sites_list: сайт для анализа :return: спискок кортежей с резульатами парсинга """ try: site = site[0] return [ settings.AVAILABLE_RENDERS[site.news_portal].__call__( site.target_url) ] except Exception as err: logger.error(err) def run_parser(self) -> list: """ Запуск парсера. :return: """ sites_list = self.get_available_sites() if self.async_mode: data_from_site = self._async_worker(sites_list) else: data_from_site = self._sync_worker(sites_list) if not data_from_site: logger.info("Ошибка рабзора сайта") raise Exception("Ошибка рабзора сайта") self._save_postgr(data_from_site) return [i.target_url for i in sites_list]
def crawl_keywords(what): print(what) p = Pool(3) p.apply_async(crawl) p.close() p.join()