-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_env.py
More file actions
265 lines (202 loc) · 6.38 KB
/
setup_env.py
File metadata and controls
265 lines (202 loc) · 6.38 KB
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
114
115
116
117
118
119
120
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
"""
Setup script for STIP (Structural Timeline of Intellectual Progression)
Install all dependencies and download required spaCy models.
Usage:
python setup_env.py [--skip-models]
Options:
--skip-models Skip downloading spaCy models (faster, use if already installed)
"""
import subprocess
import sys
import os
from pathlib import Path
def run_command(command, description):
"""Run a shell command with progress output."""
print(f"\n{'='*60}")
print(f"📦 {description}")
print(f"{'='*60}")
print(f"Command: {' '.join(command)}\n")
result = subprocess.run(command, capture_output=False)
if result.returncode != 0:
print(f"❌ Failed: {description}")
return False
print(f"✅ Success: {description}")
return True
def check_python_version():
"""Check if Python version is compatible."""
print("\n" + "="*60)
print("🐍 Checking Python Version")
print("="*60)
version = sys.version_info
print(f"Python version: {version.major}.{version.minor}.{version.micro}")
if version.major < 3 or (version.major == 3 and version.minor < 8):
print("❌ Python 3.8+ is required")
return False
print("✅ Python version is compatible")
return True
def install_dependencies(skip_models=False):
"""Install all required dependencies."""
# Upgrade pip first
run_command(
[sys.executable, "-m", "pip", "install", "--upgrade", "pip"],
"Upgrading pip"
)
# Install requirements
success = run_command(
[sys.executable, "-m", "pip", "install", "-r", "requirements.txt"],
"Installing core dependencies from requirements.txt"
)
if not success:
return False
# Install optional development dependencies
print("\n" + "="*60)
print("🔧 Installing development tools")
print("="*60)
dev_deps = [
"ipython>=8.0.0",
"jupyter>=1.0.0",
"pre-commit>=2.0.0"
]
for dep in dev_deps:
run_command(
[sys.executable, "-m", "pip", "install", dep],
f"Installing {dep}"
)
return True
def download_spacy_models():
"""Download required spaCy language models."""
print("\n" + "="*60)
print("🤖 Downloading spaCy Models")
print("="*60)
models = [
("en_core_web_sm", "Small English model (fast, recommended for testing)"),
("en_core_web_md", "Medium English model (balanced)"),
]
for model, description in models:
print(f"\n📥 Installing {model} - {description}")
result = subprocess.run(
[sys.executable, "-m", "spacy", "download", model],
capture_output=False
)
if result.returncode == 0:
print(f"✅ {model} installed successfully")
else:
print(f"⚠️ {model} installation failed (optional)")
return True
def verify_installation():
"""Verify that all critical packages are installed correctly."""
print("\n" + "="*60)
print("✅ Verifying Installation")
print("="*60)
packages = [
"spacy",
"numpy",
"pandas",
"sklearn",
"sentence_transformers",
"matplotlib",
"pytest",
]
all_ok = True
for package in packages:
try:
__import__(package)
module = sys.modules[package]
version = getattr(module, "__version__", "unknown")
print(f"✅ {package:25} v{version}")
except ImportError as e:
print(f"❌ {package:25} NOT INSTALLED")
all_ok = False
# Check spaCy models
try:
import spacy
try:
nlp = spacy.load("en_core_web_sm")
print(f"✅ en_core_web_sm loaded successfully")
except OSError:
print(f"⚠️ en_core_web_sm not found (run: python -m spacy download en_core_web_sm)")
except:
pass
return all_ok
def create_virtual_env_guide():
"""Create a guide for virtual environment setup."""
guide = """# Virtual Environment Setup Guide
## Recommended: Using venv
```bash
# Create virtual environment
python -m venv venv
# Activate on Linux/Mac
source venv/bin/activate
# Activate on Windows
venv\\Scripts\\activate
# Install dependencies
pip install -r requirements.txt
# Run setup script
python setup_env.py
```
## Alternative: Using conda
```bash
# Create conda environment
conda create -n stip python=3.9
# Activate environment
conda activate stip
# Install dependencies
pip install -r requirements.txt
# Run setup script
python setup_env.py
```
## Quick Start
After activation, simply run:
```bash
pip install -r requirements.txt
python setup_env.py
```
Then test the installation:
```bash
pytest tests/
python examples/analyze_attention.py
```
"""
guide_path = Path("VIRTUAL_ENV_GUIDE.md")
guide_path.write_text(guide)
print(f"\n📝 Created {guide_path}")
def main():
"""Main setup function."""
print("\n" + "="*60)
print("🚀 STIP Development Environment Setup")
print("="*60)
print("\nThis script will set up your local development environment.")
# Check Python version
if not check_python_version():
sys.exit(1)
# Parse arguments
skip_models = "--skip-models" in sys.argv
# Install dependencies
if not install_dependencies(skip_models):
print("\n❌ Dependency installation failed")
sys.exit(1)
# Download spaCy models (unless skipped)
if not skip_models:
download_spacy_models()
# Verify installation
all_ok = verify_installation()
# Create virtual env guide
create_virtual_env_guide()
# Final summary
print("\n" + "="*60)
print("🎉 Setup Complete!")
print("="*60)
if all_ok:
print("\n✅ All critical packages installed successfully")
print("\nNext steps:")
print(" 1. Run tests: pytest tests/")
print(" 2. Try example: python examples/analyze_attention.py")
print(" 3. Start developing! 🚀")
else:
print("\n⚠️ Some packages may need manual installation")
print(" Check the errors above and try: pip install <package-name>")
print("\n📚 See VIRTUAL_ENV_GUIDE.md for more details")
print("="*60 + "\n")
if __name__ == "__main__":
main()