File size: 3,664 Bytes
19b19f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Build script for Hugging Face Spaces - downloads model files at build time
"""

import os
import sys
import subprocess
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def run_download_script():
    """Run the model download script"""
    try:
        logger.info("Running advanced model download script...")
        
        # Try the advanced download script first
        result = subprocess.run([sys.executable, "download_model_advanced.py"], 
                              capture_output=True, text=True, check=True)
        logger.info("Advanced model download completed successfully")
        logger.info(result.stdout)
        return True
        
    except subprocess.CalledProcessError as e:
        logger.warning(f"Advanced download failed: {e}")
        logger.warning("Falling back to basic download script...")
        
        try:
            # Fallback to basic download script
            result = subprocess.run([sys.executable, "download_model.py"], 
                                  capture_output=True, text=True, check=True)
            logger.info("Basic model download completed successfully")
            logger.info(result.stdout)
            return True
            
        except subprocess.CalledProcessError as e2:
            logger.error(f"Basic download also failed: {e2}")
            logger.error(e2.stderr)
            return False

def verify_build():
    """Verify that the build was successful"""
    try:
        logger.info("Verifying build results...")
        
        # Check if int4 directory exists
        if not os.path.exists("./int4"):
            logger.error("int4 directory not found")
            return False
        
        # Check for essential files
        essential_files = [
            "config.json",
            "pytorch_model.bin",
            "tokenizer.json",
            "tokenizer_config.json"
        ]
        
        missing_files = []
        for file in essential_files:
            file_path = os.path.join("./int4", file)
            if not os.path.exists(file_path):
                missing_files.append(file)
        
        if missing_files:
            logger.error(f"Missing essential files: {missing_files}")
            return False
        
        # Check file sizes
        total_size = 0
        for file in essential_files:
            file_path = os.path.join("./int4", file)
            if os.path.exists(file_path):
                file_size = os.path.getsize(file_path)
                total_size += file_size
                logger.info(f"βœ… {file}: {file_size} bytes")
        
        logger.info(f"Total model size: {total_size / (1024*1024):.2f} MB")
        
        if total_size < 1000000:  # Less than 1MB
            logger.warning("Model files seem too small")
            return False
        
        logger.info("Build verification completed successfully")
        return True
        
    except Exception as e:
        logger.error(f"Error verifying build: {e}")
        return False

def main():
    """Main build function"""
    logger.info("Starting Hugging Face Space build process...")
    
    # Run the model download script
    if run_download_script():
        # Verify the build
        if verify_build():
            logger.info("Build process completed successfully")
            return True
        else:
            logger.error("Build verification failed")
            return False
    else:
        logger.error("Model download failed")
        return False

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)