volume.py (902B)
1 import modal 2 from functools import wraps 3 4 omniparser = modal.Volume.from_name("omniparser", create_if_missing=True) 5 6 7 def cache(func): 8 import os 9 import subprocess 10 11 CACHE_PATHS = [ 12 ".paddleocr", 13 ".EasyOCR", 14 ".config", 15 ".cache", 16 ] 17 18 @wraps(func) 19 def wrapper(*args, **kwargs): 20 for path in CACHE_PATHS: 21 if not os.path.isdir(f"/data/{path}"): 22 continue 23 subprocess.run(["cp", "-rf", "--", f"/data/{path}", "/root"], check=False) 24 25 try: 26 return func(*args, **kwargs) 27 finally: 28 for path in CACHE_PATHS: 29 os.makedirs(os.path.dirname(f"/data/{path}"), exist_ok=True) 30 31 subprocess.run( 32 ["cp", "-rf", "--", f"/root/{path}", f"/data/{path}"], check=False 33 ) 34 35 omniparser.commit() 36 37 return wrapper