Spaces:
No application file
No application file
Clanflare
feat: add timestamped model directories and model selection in interactive testing
c04ffda Download src/pull_model_from_hub.py from clanflare/mistral-7b-finetuning-scripts: direct link, hf CLI and curl.
- Browser
- Download file 5.14 kB
-
https://huggingface.co/spaces/clanflare/mistral-7b-finetuning-scripts/resolve/main/src/pull_model_from_hub.py
- Command line
-
hf download hf://spaces/clanflare/mistral-7b-finetuning-scripts/src/pull_model_from_hub.py
-
curl -L -o pull_model_from_hub.py https://huggingface.co/spaces/clanflare/mistral-7b-finetuning-scripts/resolve/main/src/pull_model_from_hub.py
5.14 kB
| #!/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() | |