def delete_tasks(self): """ Deletes all tasks & completions from filesystem, then reloads clean project :return: """ delete_dir_content(self.config['output_dir']) with io.open(self.config['input_path'], mode='w') as f: json.dump([], f) self.reload()
def remove_all(self, ids=None): if ids is None: self.cache.clear() delete_dir_content(self.path) else: for i in ids: self.cache.pop(i, None) path = os.path.join(self.path, str(i) + '.json') try: remove_file_or_dir(path) except OSError: logger.warning('Storage file already removed: ' + path)
def delete_tasks(self): """ Deletes all tasks & completions from filesystem, then reloads clean project :return: """ delete_dir_content(self.config['output_dir']) if os.path.exists(self.config['input_path']) and os.path.isfile(self.config['input_path']): with io.open(self.config['input_path'], mode='w') as f: json.dump({}, f) # reload everything related to tasks self.load_tasks() self.load_derived_schemas()
def create_project_dir(cls, project_name, args): """ Create project directory in args.root_dir/project_name, and initialize there all required files If some files are missed, restore them from defaults. If config files are specified by args, copy them in project directory :param project_name: :param args: :return: """ dir = cls.get_project_dir(project_name, args) if args.force: delete_dir_content(dir) os.makedirs(dir, exist_ok=True) config = (json_load(args.config_path) if args.config_path else json_load(find_file("default_config.json"))) def already_exists_error(what, path): raise RuntimeError( '{path} {what} already exists. Use "--force" option to recreate it.' .format(path=path, what=what)) input_path = args.input_path or config.get("input_path") # save label config config_xml = "config.xml" config_xml_path = os.path.join(dir, config_xml) label_config_file = args.label_config or config.get("label_config") if label_config_file: copy2(label_config_file, config_xml_path) print(label_config_file + " label config copied to " + config_xml_path) else: if os.path.exists(config_xml_path) and not args.force: already_exists_error("label config", config_xml_path) if not input_path: # create default config with polygons only if input data is not set default_label_config = find_file( "examples/image_polygons/config.xml") copy2(default_label_config, config_xml_path) print(default_label_config + " label config copied to " + config_xml_path) else: with io.open(config_xml_path, mode="w") as fout: fout.write("<View></View>") print("Empty config has been created in " + config_xml_path) config["label_config"] = config_xml if args.source: config["source"] = { "type": args.source, "path": args.source_path, "params": args.source_params, } else: # save tasks.json tasks_json = "tasks.json" tasks_json_path = os.path.join(dir, tasks_json) if input_path: tasks = cls._load_tasks(input_path, args, config_xml_path) else: tasks = {} with io.open(tasks_json_path, mode="w") as fout: json.dump(tasks, fout, indent=2) config["input_path"] = tasks_json config["source"] = { "name": "Tasks", "type": "tasks-json", "path": os.path.abspath(tasks_json_path), } logger.debug( "{tasks_json_path} input file with {n} tasks has been created from {input_path}" .format(tasks_json_path=tasks_json_path, n=len(tasks), input_path=input_path)) if args.target: config["target"] = { "type": args.target, "path": args.target_path, "params": args.target_params, } else: completions_dir = os.path.join(dir, "completions") if os.path.exists(completions_dir) and not args.force: already_exists_error("output dir", completions_dir) if os.path.exists(completions_dir): delete_dir_content(completions_dir) print(completions_dir + " output dir already exists. Clear it.") else: os.makedirs(completions_dir, exist_ok=True) print(completions_dir + " output dir has been created.") config["output_dir"] = "completions" config["target"] = { "name": "Completions", "type": "completions-dir", "path": os.path.abspath(completions_dir), } if "ml_backends" not in config or not isinstance( config["ml_backends"], list): config["ml_backends"] = [] if args.ml_backends: for url in args.ml_backends: config["ml_backends"].append( cls._create_ml_backend_params(url, project_name)) if args.sampling: config["sampling"] = args.sampling if args.port: config["port"] = args.port if args.host: config["host"] = args.host if args.allow_serving_local_files: config["allow_serving_local_files"] = True if args.key_file and args.cert_file: config["protocol"] = "https://" config["cert"] = args.cert_file config["key"] = args.key_file if (hasattr(args, "web_gui_project_desc") and args.web_gui_project_desc) or args.project_desc: config[ "description"] = args.web_gui_project_desc or args.project_desc # create config.json config_json = "config.json" config_json_path = os.path.join(dir, config_json) if os.path.exists(config_json_path) and not args.force: already_exists_error("config", config_json_path) with io.open(config_json_path, mode="w") as f: json.dump(config, f, indent=2) print("") print( "Label Studio has been successfully initialized. Check project states in " + dir) print("Start the server: label-studio start " + dir) return dir
def create_project_dir(cls, project_name, args): """ Create project directory in args.root_dir/project_name, and initialize there all required files If some files are missed, restore them from defaults. If config files are specified by args, copy them in project directory :param project_name: :param args: :return: """ dir = cls.get_project_dir(project_name, args) os.makedirs(dir, exist_ok=True) config = json_load( args.config_path) if args.config_path else json_load( find_file('default_config.json')) def already_exists_error(what, path): raise RuntimeError( '{path} {what} already exists. Use "--force" option to recreate it.' .format(path=path, what=what)) input_path = args.input_path or config.get('input_path') # save label config config_xml = 'config.xml' config_xml_path = os.path.join(dir, config_xml) label_config_file = args.label_config or config.get('label_config') if label_config_file: copy2(label_config_file, config_xml_path) print(label_config_file + ' label config copied to ' + config_xml_path) else: if os.path.exists(config_xml_path) and not args.force: already_exists_error('label config', config_xml_path) if not input_path: # create default config with polygons only if input data is not set default_label_config = find_file( 'examples/image_polygons/config.xml') copy2(default_label_config, config_xml_path) print(default_label_config + ' label config copied to ' + config_xml_path) else: with io.open(config_xml_path, mode='w') as fout: fout.write('<View></View>') print('Empty config has been created in ' + config_xml_path) config['label_config'] = config_xml # save tasks.json tasks_json = 'tasks.json' tasks_json_path = os.path.join(dir, tasks_json) if input_path: tasks = cls._load_tasks(input_path, args, config_xml_path) with io.open(tasks_json_path, mode='w') as fout: json.dump(tasks, fout, indent=2) print(tasks_json_path + ' input path has been created from ' + input_path) else: if os.path.exists(tasks_json_path) and not args.force: already_exists_error('input path', tasks_json_path) with io.open(tasks_json_path, mode='w') as fout: json.dump({}, fout) print(tasks_json_path + ' input path has been created with empty tasks.') config['input_path'] = tasks_json # create completions dir completions_dir = os.path.join(dir, 'completions') if os.path.exists(completions_dir) and not args.force: already_exists_error('output dir', completions_dir) if os.path.exists(completions_dir): delete_dir_content(completions_dir) print(completions_dir + ' output dir already exists. Clear it.') else: os.makedirs(completions_dir, exist_ok=True) print(completions_dir + ' output dir has been created.') config['output_dir'] = 'completions' if args.ml_backend_url: if 'ml_backend' not in config or not isinstance( config['ml_backend'], dict): config['ml_backend'] = {} config['ml_backend']['url'] = args.ml_backend_url if args.ml_backend_name: config['ml_backend']['name'] = args.ml_backend_name else: config['ml_backend']['name'] = str(uuid4()) # create config.json config_json = 'config.json' config_json_path = os.path.join(dir, config_json) if os.path.exists(config_json_path) and not args.force: already_exists_error('config', config_json_path) with io.open(config_json_path, mode='w') as f: json.dump(config, f, indent=2) print('') print( 'Label Studio has been successfully initialized. Check project states in ' + dir) print('Start the server: label-studio start ' + dir) return dir
def remove_all(self): delete_dir_content(self.path)
def create_project_dir(cls, project_name, args): """ Create project directory in args.root_dir/project_name, and initialize there all required files If some files are missed, restore them from defaults. If config files are specified by args, copy them in project directory :param project_name: :param args: :return: """ dir = cls.get_project_dir(project_name, args) if args.force: delete_dir_content(dir) os.makedirs(dir, exist_ok=True) config = json_load( args.config_path) if args.config_path else json_load( find_file('default_config.json')) def already_exists_error(what, path): raise RuntimeError( '{path} {what} already exists. Use "--force" option to recreate it.' .format(path=path, what=what)) input_path = args.input_path or config.get('input_path') # save label config config_xml = 'config.xml' config_xml_path = os.path.join(dir, config_xml) label_config_file = args.label_config or config.get('label_config') if label_config_file: copy2(label_config_file, config_xml_path) print(label_config_file + ' label config copied to ' + config_xml_path) else: if os.path.exists(config_xml_path) and not args.force: already_exists_error('label config', config_xml_path) if not input_path: # create default config with polygons only if input data is not set default_label_config = find_file( 'examples/image_polygons/config.xml') copy2(default_label_config, config_xml_path) print(default_label_config + ' label config copied to ' + config_xml_path) else: with io.open(config_xml_path, mode='w') as fout: fout.write('<View></View>') print('Empty config has been created in ' + config_xml_path) config['label_config'] = config_xml if args.source: config['source'] = { 'type': args.source, 'path': args.source_path, 'params': args.source_params } else: # save tasks.json tasks_json = 'tasks.json' tasks_json_path = os.path.join(dir, tasks_json) if input_path: tasks = cls._load_tasks(input_path, args, config_xml_path) else: tasks = {} with io.open(tasks_json_path, mode='w') as fout: json.dump(tasks, fout, indent=2) config['input_path'] = tasks_json config['source'] = { 'name': 'Tasks', 'type': 'tasks-json', 'path': os.path.abspath(tasks_json_path) } logger.debug( '{tasks_json_path} input file with {n} tasks has been created from {input_path}' .format(tasks_json_path=tasks_json_path, n=len(tasks), input_path=input_path)) if args.target: config['target'] = { 'type': args.target, 'path': args.target_path, 'params': args.target_params } else: completions_dir = os.path.join(dir, 'completions') if os.path.exists(completions_dir) and not args.force: already_exists_error('output dir', completions_dir) if os.path.exists(completions_dir): delete_dir_content(completions_dir) print(completions_dir + ' output dir already exists. Clear it.') else: os.makedirs(completions_dir, exist_ok=True) print(completions_dir + ' output dir has been created.') config['output_dir'] = 'completions' config['target'] = { 'name': 'Completions', 'type': 'completions-dir', 'path': os.path.abspath(completions_dir) } if 'ml_backends' not in config or not isinstance( config['ml_backends'], list): config['ml_backends'] = [] if args.ml_backends: for url in args.ml_backends: config['ml_backends'].append( cls._create_ml_backend_params(url, project_name)) if args.sampling: config['sampling'] = args.sampling if args.port: config['port'] = args.port if args.host: config['host'] = args.host if args.allow_serving_local_files: config['allow_serving_local_files'] = True if args.key_file and args.cert_file: config['protocol'] = 'https://' config['cert'] = args.cert_file config['key'] = args.key_file # create config.json config_json = 'config.json' config_json_path = os.path.join(dir, config_json) if os.path.exists(config_json_path) and not args.force: already_exists_error('config', config_json_path) with io.open(config_json_path, mode='w') as f: json.dump(config, f, indent=2) print('') print( 'Label Studio has been successfully initialized. Check project states in ' + dir) print('Start the server: label-studio start ' + dir) return dir