Spaces:
No application file
No application file
File size: 5,142 Bytes
c04ffda | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #!/usr/bin/env python3
"""
Script to pull models from Hugging Face Hub.
Supports downloading models with various configurations and options.
"""
import argparse
import os
import torch
import logging
from pathlib import Path
from typing import Optional
from huggingface_hub import snapshot_download
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
# Set up logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler("model_download.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
def pull_model(
model_name: str = "clanflare/mistral-7b-lora-finetuned",
output_dir: str = "models/mistral-7b-lora-finetuned",
revision: Optional[str] = None,
token: Optional[str] = None,
cache_dir: Optional[str] = None,
local_files_only: bool = False,
force_download: bool = False,
) -> None:
"""
Pull a model from Hugging Face Hub with the same configuration as used in training.
Args:
model_name (str): Name of the model to download (default: "clanflare/mistral-7b-lora-finetuned")
output_dir (str): Directory to save the model (default: "models/mistral-7b-lora-finetuned")
revision (Optional[str]): Specific model version to download
token (Optional[str]): Hugging Face authentication token
cache_dir (Optional[str]): Directory to cache downloaded files
local_files_only (bool): If True, only use local files
force_download (bool): If True, force download even if files exist
"""
# Create output directory if it doesn't exist
output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)
logger.info(f"Downloading model {model_name} to {output_dir}...")
# Download model files with the same configuration as training
snapshot_download(
repo_id=model_name,
revision=revision,
local_dir=output_dir,
local_dir_use_symlinks=False,
token=token,
cache_dir=cache_dir,
local_files_only=local_files_only,
force_download=force_download,
)
# Load tokenizer and model with the same configuration as training
logger.info("Loading tokenizer and model to verify download...")
try:
# First load the base model
base_model = AutoModelForCausalLM.from_pretrained(
"mistralai/Mistral-7B-v0.1",
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True,
cache_dir=cache_dir,
resume_download=True,
low_cpu_mem_usage=True,
return_dict=True,
use_cache=False
)
# Then load the LoRA weights
model = PeftModel.from_pretrained(base_model, output_dir)
tokenizer = AutoTokenizer.from_pretrained(output_dir)
# Prepare model for training
model.config.use_cache = False
model.train()
logger.info(f"Successfully downloaded and verified model {model_name}")
logger.info(f"Model is ready for training at {output_dir}")
except Exception as e:
logger.error(f"Error loading model: {str(e)}")
logger.error("Please ensure you have:")
logger.error("1. Sufficient disk space (~15GB)")
logger.error("2. Stable internet connection")
logger.error("3. Hugging Face authentication if needed (use `huggingface-cli login`)")
raise
def main():
parser = argparse.ArgumentParser(description="Pull a model from Hugging Face Hub")
parser.add_argument(
"--model_name",
type=str,
default="clanflare/mistral-7b-lora-finetuned",
help="Name of the model to download (default: 'clanflare/mistral-7b-lora-finetuned')",
)
parser.add_argument(
"--output_dir",
type=str,
default="models/mistral-7b-lora-finetuned",
help="Directory to save the model (default: 'models/mistral-7b-lora-finetuned')",
)
parser.add_argument(
"--revision",
type=str,
default=None,
help="Specific model version to download",
)
parser.add_argument(
"--token",
type=str,
default=None,
help="Hugging Face authentication token",
)
parser.add_argument(
"--cache_dir",
type=str,
default=None,
help="Directory to cache downloaded files",
)
parser.add_argument(
"--local_files_only",
action="store_true",
help="Only use local files",
)
parser.add_argument(
"--force_download",
action="store_true",
help="Force download even if files exist",
)
args = parser.parse_args()
pull_model(
model_name=args.model_name,
output_dir=args.output_dir,
revision=args.revision,
token=args.token,
cache_dir=args.cache_dir,
local_files_only=args.local_files_only,
force_download=args.force_download,
)
if __name__ == "__main__":
main()
|