Spaces:
Runtime error
Runtime error
Update install.py
Browse files- install.py +153 -152
install.py
CHANGED
@@ -1,153 +1,154 @@
|
|
1 |
-
#!/usr/bin/env python
|
2 |
-
# coding=utf-8
|
3 |
-
|
4 |
-
"""
|
5 |
-
Installation script for Hugging Face Space setup.
|
6 |
-
This script ensures all dependencies are installed correctly
|
7 |
-
during the Space build process.
|
8 |
-
"""
|
9 |
-
|
10 |
-
import
|
11 |
-
import
|
12 |
-
import
|
13 |
-
import
|
14 |
-
import
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
logging
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
)
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
logger.error(f"
|
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 |
-
if cuda_available
|
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 |
sys.exit(exit_code)
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding=utf-8
|
3 |
+
|
4 |
+
"""
|
5 |
+
Installation script for Hugging Face Space setup.
|
6 |
+
This script ensures all dependencies are installed correctly
|
7 |
+
during the Space build process.
|
8 |
+
"""
|
9 |
+
|
10 |
+
import unsloth
|
11 |
+
import os
|
12 |
+
import sys
|
13 |
+
import subprocess
|
14 |
+
import logging
|
15 |
+
import traceback
|
16 |
+
from pathlib import Path
|
17 |
+
|
18 |
+
# Configure logging
|
19 |
+
logging.basicConfig(
|
20 |
+
level=logging.INFO,
|
21 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
22 |
+
handlers=[logging.StreamHandler(sys.stdout)]
|
23 |
+
)
|
24 |
+
logger = logging.getLogger(__name__)
|
25 |
+
|
26 |
+
def run_command(cmd, description=""):
|
27 |
+
"""Run a shell command and log the output."""
|
28 |
+
logger.info(f"Running: {description if description else cmd}")
|
29 |
+
try:
|
30 |
+
process = subprocess.run(
|
31 |
+
cmd,
|
32 |
+
shell=True,
|
33 |
+
check=True,
|
34 |
+
stdout=subprocess.PIPE,
|
35 |
+
stderr=subprocess.PIPE,
|
36 |
+
text=True
|
37 |
+
)
|
38 |
+
logger.info(f"Command output: {process.stdout}")
|
39 |
+
return True
|
40 |
+
except subprocess.CalledProcessError as e:
|
41 |
+
logger.error(f"Command failed with exit code {e.returncode}")
|
42 |
+
logger.error(f"Error output: {e.stderr}")
|
43 |
+
return False
|
44 |
+
|
45 |
+
def install_dependencies():
|
46 |
+
"""Install all required dependencies in the correct order."""
|
47 |
+
current_dir = Path(__file__).parent
|
48 |
+
req_path = current_dir / "requirements.txt"
|
49 |
+
|
50 |
+
if not req_path.exists():
|
51 |
+
logger.error(f"Requirements file not found: {req_path}")
|
52 |
+
return False
|
53 |
+
|
54 |
+
try:
|
55 |
+
# Step 1: Upgrade pip
|
56 |
+
run_command(f"{sys.executable} -m pip install --upgrade pip", "Upgrading pip")
|
57 |
+
|
58 |
+
# Step 2: Install direct torch version for CUDA compatibility
|
59 |
+
run_command(
|
60 |
+
f"{sys.executable} -m pip install torch>=2.0.0,<2.2.0 --extra-index-url https://download.pytorch.org/whl/cu118",
|
61 |
+
"Installing PyTorch with CUDA support"
|
62 |
+
)
|
63 |
+
|
64 |
+
# Step 3: Install base dependencies
|
65 |
+
run_command(
|
66 |
+
f"{sys.executable} -m pip install transformers accelerate bitsandbytes peft einops",
|
67 |
+
"Installing ML dependencies"
|
68 |
+
)
|
69 |
+
|
70 |
+
# Step 4: Install unsloth separately
|
71 |
+
run_command(
|
72 |
+
f"{sys.executable} -m pip install unsloth>=2024.3",
|
73 |
+
"Installing Unsloth"
|
74 |
+
)
|
75 |
+
|
76 |
+
# Step 5: Install all remaining requirements
|
77 |
+
run_command(
|
78 |
+
f"{sys.executable} -m pip install -r {req_path}",
|
79 |
+
"Installing all requirements"
|
80 |
+
)
|
81 |
+
|
82 |
+
# Verify critical packages
|
83 |
+
import_check = verify_imports()
|
84 |
+
if not import_check:
|
85 |
+
logger.error("Failed to verify critical packages")
|
86 |
+
return False
|
87 |
+
|
88 |
+
logger.info("All dependencies installed successfully!")
|
89 |
+
return True
|
90 |
+
|
91 |
+
except Exception as e:
|
92 |
+
logger.error(f"Error installing dependencies: {str(e)}")
|
93 |
+
traceback.print_exc()
|
94 |
+
return False
|
95 |
+
|
96 |
+
def verify_imports():
|
97 |
+
"""Verify that critical packages can be imported."""
|
98 |
+
critical_packages = [
|
99 |
+
"torch", "transformers", "unsloth", "peft",
|
100 |
+
"gradio", "accelerate", "bitsandbytes"
|
101 |
+
]
|
102 |
+
|
103 |
+
success = True
|
104 |
+
for package in critical_packages:
|
105 |
+
try:
|
106 |
+
module = __import__(package)
|
107 |
+
version = getattr(module, "__version__", "unknown")
|
108 |
+
logger.info(f"Successfully imported {package} (version: {version})")
|
109 |
+
except ImportError:
|
110 |
+
logger.error(f"CRITICAL: Failed to import {package}")
|
111 |
+
success = False
|
112 |
+
except Exception as e:
|
113 |
+
logger.error(f"Error verifying {package}: {str(e)}")
|
114 |
+
success = False
|
115 |
+
|
116 |
+
# Check CUDA
|
117 |
+
try:
|
118 |
+
import torch
|
119 |
+
cuda_available = torch.cuda.is_available()
|
120 |
+
device_count = torch.cuda.device_count() if cuda_available else 0
|
121 |
+
if cuda_available:
|
122 |
+
device_name = torch.cuda.get_device_name(0)
|
123 |
+
logger.info(f"CUDA available - Devices: {device_count}, Name: {device_name}")
|
124 |
+
else:
|
125 |
+
logger.warning(f"CUDA not available - This might affect performance")
|
126 |
+
except Exception as e:
|
127 |
+
logger.error(f"Error checking CUDA: {str(e)}")
|
128 |
+
|
129 |
+
return success
|
130 |
+
|
131 |
+
def main():
|
132 |
+
logger.info("Starting installation for Phi-4 Unsloth Training Space")
|
133 |
+
|
134 |
+
try:
|
135 |
+
# Install dependencies
|
136 |
+
if not install_dependencies():
|
137 |
+
logger.error("Failed to install dependencies")
|
138 |
+
sys.exit(1)
|
139 |
+
|
140 |
+
# Create marker file to show successful installation
|
141 |
+
with open("INSTALL_SUCCESS.txt", "w") as f:
|
142 |
+
f.write("Installation completed successfully")
|
143 |
+
|
144 |
+
logger.info("Installation completed successfully")
|
145 |
+
return 0
|
146 |
+
|
147 |
+
except Exception as e:
|
148 |
+
logger.error(f"Installation failed with error: {str(e)}")
|
149 |
+
traceback.print_exc()
|
150 |
+
return 1
|
151 |
+
|
152 |
+
if __name__ == "__main__":
|
153 |
+
exit_code = main()
|
154 |
sys.exit(exit_code)
|