Spaces:
Running
Running
| #!/usr/bin/env python3 | |
| """ | |
| Pre-download all models used in HuggingFace Enabling Sessions | |
| Run this BEFORE your session to cache models locally | |
| Models will save to ~/.cache/huggingface/hub | |
| """ | |
| import os | |
| from transformers import pipeline, AutoTokenizer | |
| from sentence_transformers import SentenceTransformer | |
| import config | |
| print("=" * 60) | |
| print("π€ HuggingFace Model Pre-Download Script") | |
| print("=" * 60) | |
| # Set HF cache directory (optional, for explicit control) | |
| HF_HOME = os.path.expanduser("~/.cache/huggingface") | |
| os.makedirs(HF_HOME, exist_ok=True) | |
| print(f"\nπ Cache location: {HF_HOME}") | |
| models_to_download = [ | |
| ("Sentiment Analysis", config.SENTIMENT_MODEL, "sentiment"), | |
| ("NER", config.NER_MODEL, "ner"), | |
| ("Question Answering", config.QA_MODEL, "qa"), | |
| ("Summarization", config.SUMMARIZATION_MODEL, "summarization"), | |
| ("Semantic Similarity", config.EMBEDDINGS_MODEL, "embeddings"), | |
| ] | |
| print(f"\nπ₯ Starting download of {len(models_to_download)} models...\n") | |
| # Download pipelines | |
| for task_name, model_id, task_type in models_to_download[:4]: | |
| try: | |
| print(f"β³ Downloading {task_name} ({model_id})...") | |
| if task_type == "ner": | |
| pipeline("ner", model=model_id) | |
| elif task_type == "qa": | |
| pipeline("question-answering", model=model_id) | |
| elif task_type == "summarization": | |
| pipeline("summarization", model=model_id) | |
| else: | |
| pipeline("sentiment-analysis", model=model_id) | |
| print(f"β {task_name} downloaded successfully\n") | |
| except Exception as e: | |
| print(f"β Error downloading {task_name}: {str(e)}\n") | |
| # Download Sentence-BERT | |
| try: | |
| print(f"β³ Downloading Semantic Similarity ({config.EMBEDDINGS_MODEL})...") | |
| SentenceTransformer(config.EMBEDDINGS_MODEL) | |
| print(f"β Semantic Similarity downloaded successfully\n") | |
| except Exception as e: | |
| print(f"β Error downloading Semantic Similarity: {str(e)}\n") | |
| # Download tokenizer | |
| try: | |
| print(f"β³ Downloading Tokenizer ({config.SENTIMENT_MODEL})...") | |
| AutoTokenizer.from_pretrained(config.SENTIMENT_MODEL) | |
| print(f"β Tokenizer downloaded successfully\n") | |
| except Exception as e: | |
| print(f"β Error downloading Tokenizer: {str(e)}\n") | |
| print("=" * 60) | |
| print("β Model pre-download complete!") | |
| print("=" * 60) | |
| print("\nπ Notes:") | |
| print("- All models are cached in ~/.cache/huggingface/hub") | |
| print("- Models will be used instantly in Spaces demos") | |
| print("- Total size: ~2-3 GB (may take 10-20 minutes)") | |
| print("\nπ Ready for your session!") | |