File size: 2,142 Bytes
deb090d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Download the sentence transformer model for offline use.
Run this script when you have internet access to cache the model locally.
"""

import os
import sys
from sentence_transformers import SentenceTransformer

def download_model():
    """Download and cache the sentence transformer model."""
    try:
        print("Downloading sentence transformer model 'all-MiniLM-L6-v2'...")
        print("This may take a few minutes on first run...")
        
        # This will download and cache the model
        model = SentenceTransformer("all-MiniLM-L6-v2")
        
        # Test that it works
        test_text = "This is a test sentence."
        embedding = model.encode([test_text])
        
        print(f"βœ“ Model downloaded successfully!")
        print(f"βœ“ Model tested successfully!")
        print(f"βœ“ Embedding dimension: {len(embedding[0])}")
        print(f"βœ“ Model cache location: {model.cache_folder}")
        
        return True
        
    except Exception as e:
        print(f"βœ— Failed to download model: {e}")
        return False

def check_model_exists():
    """Check if the model is already cached."""
    try:
        # Try to load from cache
        import os
        os.environ['TRANSFORMERS_OFFLINE'] = '1'
        os.environ['HF_HUB_OFFLINE'] = '1'
        
        model = SentenceTransformer("all-MiniLM-L6-v2")
        print("βœ“ Model is already cached and available for offline use!")
        return True
        
    except Exception:
        print("βœ— Model is not cached or not available for offline use")
        return False

if __name__ == "__main__":
    print("Sentence Transformer Model Downloader")
    print("=" * 40)
    
    # Check if model already exists
    if check_model_exists():
        print("\nModel is already available. No download needed.")
        sys.exit(0)
    
    # Download the model
    print("\nDownloading model...")
    if download_model():
        print("\nβœ“ Setup complete! You can now run the application offline.")
    else:
        print("\nβœ— Download failed. Please check your internet connection.")
        sys.exit(1)