def __new__(cls, *, name: str = None, directory: str = None, version: str = None, source: str = None, update: bool = False, branch: str = None, **kwargs): if cls.__name__ == 'Module': from paddlehub.server.server import CacheUpdater # This branch come from hub.Module(name='xxx') or hub.Module(directory='xxx') if name: module = cls.init_with_name(name=name, version=version, source=source, update=update, branch=branch, **kwargs) CacheUpdater("update_cache", module=name, version=version).start() elif directory: module = cls.init_with_directory(directory=directory, **kwargs) CacheUpdater("update_cache", module=directory, version="0.0.0").start() else: module = object.__new__(cls) return module
def start_zmq_serving_with_args(self): ''' Start one PaddleHub-Serving instance by arguments with zmq. ''' if self.modules_info is not None: for module, info in self.modules_info.items(): CacheUpdater("hub_serving_start", module=module, version=info['init_args']['version']).start() front_port = self.args.port if is_port_occupied("127.0.0.1", front_port) is True: log.logger.error("Port %s is occupied, please change it." % front_port) return False back_port = int(front_port) + 1 for index in range(100): if not is_port_occupied("127.0.0.1", back_port): break else: back_port = int(back_port) + 1 else: raise RuntimeError( "Port from %s to %s is occupied, please use another port" % (int(front_port) + 1, back_port)) self.dump_pid_file() run_all(self.modules_info, self.args.gpu, front_port, back_port) else: log.logger.error("Lack of necessary parameters!")
def execute(self, argv: List) -> bool: if not argv: print('ERROR: You must give one module to run.') return False module_name = argv[0] CacheUpdater("hub_run", module_name).start() if os.path.exists(module_name) and os.path.isdir(module_name): try: module = Module.load(module_name) except InvalidHubModule: print('{} is not a valid HubModule'.format(module_name)) return False except: print('Some exception occurred while loading the {}'.format( module_name)) return False else: module = Module(name=module_name) if not module.is_runnable: print('ERROR! Module {} is not executable.'.format(module_name)) return False if isinstance(module, ModuleV1): result = self.run_module_v1(module, argv[1:]) else: result = module._run_func(argv[1:]) print(result) return True
def stop_serving(self, port: int): ''' Stop PaddleHub-Serving by port. ''' filepath = os.path.join(CONF_HOME, "serving_" + str(port) + ".json") info = self.load_pid_file(filepath, port) if info is False: return pid = info["pid"] module = info["module"] start_time = info["start_time"] CacheUpdater("hub_serving_stop", module=module, addition={"period_time": time.time() - start_time}).start() if os.path.exists(filepath): os.remove(filepath) if not pid_is_exist(pid): log.logger.info("PaddleHub Serving has been stopped.") return log.logger.info("PaddleHub Serving will stop.") if platform.system() == "Windows": os.kill(pid, signal.SIGTERM) else: try: os.killpg(pid, signal.SIGTERM) except ProcessLookupError: os.kill(pid, signal.SIGTERM)
def execute(self, argv): args = self.parser.parse_args() if not args.module_name or not args.model_dir: ConvertCommand.show_help() return False self.module = args.module_name self.version = args.module_version if args.module_version is not None else '1.0.0' CacheUpdater("hub_convert", module=self.module, version=self.version).start() self.src = args.model_dir if not os.path.isdir(self.src): print('`{}` is not exists or not a directory path'.format(self.src)) return False self.dest = args.output_dir if args.output_dir is not None else os.path.join('{}_{}'.format( self.module, str(time.time()))) os.makedirs(self.dest) self._tmp_dir = tmp_dir self.create_module_py() self.create_init_py() self.create_serving_demo_py() self.create_module_tar() print('The converted module is stored in `{}`.'.format(self.dest)) return True
def preinstall_modules(self): ''' Install module by PaddleHub and get info of this module. ''' for key, value in self.modules_info.items(): init_args = value["init_args"] CacheUpdater("hub_serving_start", module=key, version=init_args.get("version", "0.0.0")).start() if "directory" not in init_args: init_args.update({"name": key}) m = hub.Module(**init_args) method_name = m.serving_func_name if method_name is None: raise RuntimeError("{} cannot be use for " "predicting".format(key)) exit(1) serving_method = getattr(m, method_name) category = str(m.type).split("/")[0].upper() self.modules_info[key].update({ "method_name": method_name, "version": m.version, "category": category, "module": m, "name": m.name, "serving_method": serving_method })
def list(*args, **kwargs): if _paddle_version < Version('2.1.0') and _paddle_version != Version( '0.0.0'): raise RuntimeError( '`hub.list` is only available in PaddlePaddle 2.1 and above, please upgrade the PaddlePaddle version.' ) from paddle.hub import list as phlist from paddlehub.server.server import CacheUpdater CacheUpdater("paddle.hub.list").start() return phlist(*args, **kwargs)
def execute(self, argv: List) -> bool: argv = '.*' if not argv else argv[0] widths = [20, 8, 30] if platform.is_windows() else [30, 8, 40] table = log.Table(widths=widths) table.append(*['ModuleName', 'Version', 'Summary'], aligns=['^', '^', '^'], colors=["blue", "blue", "blue"]) CacheUpdater("hub_search", argv).start() results = module_server.search_module(name=argv) for result in results: table.append(result['name'], result['version'], result['summary']) print(table) return True
def start_bert_serving(args): ''' Start bert serving server. ''' if platform.system() != "Linux": log.logger.error("Error. Bert Service only support linux.") return False if is_port_occupied("127.0.0.1", args.port) is True: log.logger.error("Port %s is occupied, please change it." % args.port) return False from paddle_gpu_serving.run import BertServer bs = BertServer(with_gpu=args.use_gpu) bs.with_model(model_name=args.modules[0]) CacheUpdater("hub_bert_service", module=args.modules[0], version="0.0.0").start() bs.run(gpu_index=args.gpu, port=int(args.port))
def execute(self, argv: List) -> bool: if not argv: print("ERROR: You must give at least one module to download.") return False for _arg in argv: result = module_server.search_module(_arg) CacheUpdater("hub_download", _arg).start() if result: url = result[0]['url'] with log.ProgressBar('Download {}'.format(url)) as bar: for file, ds, ts in utils.download_with_progress(url): bar.update(float(ds) / ts) else: print( 'ERROR: Could not find a HubModule named {}'.format(_arg)) return True
def execute(self, argv: List) -> bool: if not argv: print("ERROR: You must give at least one module to install.") return False manager = LocalModuleManager() for _arg in argv: if os.path.exists(_arg) and os.path.isdir(_arg): manager.install(directory=_arg) elif os.path.exists(_arg) and xarfile.is_xarfile(_arg): manager.install(archive=_arg) else: _arg = _arg.split('==') name = _arg[0] version = None if len(_arg) == 1 else _arg[1] CacheUpdater("hub_install", name, version).start() manager.install(name=name, version=version) return True
def execute(self, argv: List) -> bool: if not argv: print("ERROR: You must give at least one module to install.") return False options = [arg for arg in argv if arg.startswith('-')] argv = [arg for arg in argv if not arg.startswith('-')] args = self.parser.parse_args(options) manager = LocalModuleManager() for _arg in argv: if os.path.exists(_arg) and os.path.isdir(_arg): manager.install(directory=_arg) elif os.path.exists(_arg) and xarfile.is_xarfile(_arg): manager.install(archive=_arg) else: _arg = _arg.split('==') name = _arg[0] version = None if len(_arg) == 1 else _arg[1] CacheUpdater("hub_install", name, version).start() manager.install(name=name, version=version, ignore_env_mismatch=args.ignore_env_mismatch) return True