замени в этом коде: import torch from pathlib import Path from functools import partial…
- User
замени в этом коде: import torch from pathlib import Path from functools import partial from typing import Iterator, List, Tuple from PIL import Image from unittest.mock import patch from transformers import AutoModelForCausalLM, AutoProcessor from huggingface_hub import snapshot_download from transformers.dynamic_module_utils import get_imports import time from tqdm import tqdm
torch.set_float32_matmul_precision("high")
Configuration options
OVERWRITE = True # Boolean option to allow overwriting existing caption files PREPEND_STRING = "" # Prefix string to prepend to the generated caption APPEND_STRING = "" # Suffix string to append to the generated caption BATCH_SIZE = 1 # How many images to process at one time. A 24gb VRAM 3090 can handle 7. A 6gb VRAM GPU can handle a batch size of 1. PRINT_PROCESSING_STATUS = False # Option to print processing status of images PRINT_CAPTIONS = False # Option to print captions to the console detailing = input("Введите детализацию: ") DETAIL_MODE = int(detailing)
print(f"Captioning with batch size: {BATCH_SIZE}")
def fixed_get_imports(filename: str | Path) -> List[str]: imports = get_imports(filename) return [imp for imp in imports if imp != "flash_attn"] if str(filename).endswith("modeling_florence2.py") else imports
def download_and_load_model(model_name: str) -> Tuple[AutoModelForCausalLM, AutoProcessor]: device = 'cuda' if torch.cuda.is_available() else 'cpu' print(f'Device available: {device}')
codemodel_path = Path("models") / model_name.replace('/', '_') if not model_path.exists(): print(f"Downloading {model_name} model to: {model_path}") snapshot_download(repo_id=model_name, local_dir=model_path, local_dir_use_symlinks=False) print(f"Loading model {model_name}...") with patch("transformers.dynamic_module_utils.get_imports", fixed_get_imports): model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, torch_dtype=torch.bfloat16).to(device) processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True) print("Model loaded.") model = torch.compile(model, mode="reduce-overhead") return model, processordef load_image_paths_recursive(folder_path: str) -> Iterator[Path]: valid_extensions = {".jpg", ".jpeg", ".png", ".bmp", ".gif", ".webp"} return ( path for path in Path(folder_path).rglob("*") if path.suffix.lower() in valid_extensions and (OVERWRITE or not path.with_suffix('.txt').exists()) )
- Assistant