2023-02-28 15:50:39 +01:00
|
|
|
import importlib
|
2022-10-04 18:51:11 +05:00
|
|
|
import json
|
2023-02-28 15:50:39 +01:00
|
|
|
from functools import wraps
|
|
|
|
from typing import Dict, List, Optional, Union
|
2022-10-04 18:51:11 +05:00
|
|
|
|
|
|
|
|
|
|
|
def save_as_jsonl(data: List[Dict], filename: str) -> None:
|
|
|
|
with open(filename, "w+") as output_file:
|
2023-02-27 17:30:54 +01:00
|
|
|
output_file.writelines(json.dumps(datum) + "\n" for datum in data)
|
2022-10-04 18:51:11 +05:00
|
|
|
|
|
|
|
|
|
|
|
def read_from_jsonl(filename: str) -> List[Dict]:
|
2023-02-27 17:30:54 +01:00
|
|
|
with open(filename) as input_file:
|
2022-10-04 18:51:11 +05:00
|
|
|
return [json.loads(line) for line in input_file]
|
2023-02-28 15:50:39 +01:00
|
|
|
|
|
|
|
|
2023-02-28 21:21:59 +01:00
|
|
|
def requires_dependencies(
|
|
|
|
dependencies: Union[str, List[str]],
|
|
|
|
extras: Optional[str] = None,
|
|
|
|
):
|
2023-02-28 15:50:39 +01:00
|
|
|
if isinstance(dependencies, str):
|
|
|
|
dependencies = [dependencies]
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
missing_deps = []
|
|
|
|
for dep in dependencies:
|
2023-03-10 22:16:05 -05:00
|
|
|
if not dependency_exists(dep):
|
2023-02-28 15:50:39 +01:00
|
|
|
missing_deps.append(dep)
|
|
|
|
if len(missing_deps) > 0:
|
|
|
|
raise ImportError(
|
2023-02-28 21:21:59 +01:00
|
|
|
f"Following dependencies are missing: {', '.join(missing_deps)}. "
|
|
|
|
+ (
|
|
|
|
f"Please install them using `pip install unstructured[{extras}]`."
|
|
|
|
if extras
|
|
|
|
else f"Please install them using `pip install {' '.join(missing_deps)}`."
|
|
|
|
),
|
2023-02-28 15:50:39 +01:00
|
|
|
)
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return decorator
|
2023-03-10 22:16:05 -05:00
|
|
|
|
|
|
|
|
|
|
|
def dependency_exists(dependency):
|
|
|
|
try:
|
|
|
|
importlib.import_module(dependency)
|
|
|
|
except ImportError:
|
|
|
|
return False
|
|
|
|
return True
|