File size: 7,068 Bytes
eda8578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Create Compatible Environment Script
===================================

Creates a compatible Python environment for ECG-FM testing.
This addresses the version incompatibilities found in the lightweight test.
"""

import subprocess
import sys
import os

def check_python_version():
    """Check current Python version"""
    version = sys.version_info
    print(f"🐍 Current Python: {version.major}.{version.minor}.{version.micro}")
    
    if version.major == 3 and 8 <= version.minor <= 11:
        print("βœ… Python version is compatible with HF Spaces")
        return True
    else:
        print("❌ Python version NOT compatible with HF Spaces")
        print("   Need Python 3.8-3.11, have 3.13.3")
        return False

def create_virtual_env():
    """Create a virtual environment with compatible Python"""
    print("\nπŸ”§ Creating Virtual Environment")
    print("=" * 40)
    
    # Check if virtualenv is available
    try:
        result = subprocess.run([sys.executable, '-m', 'pip', 'install', 'virtualenv'], 
                              capture_output=True, text=True)
        if result.returncode == 0:
            print("βœ… virtualenv installed")
        else:
            print("❌ Failed to install virtualenv")
            return False
    except Exception as e:
        print(f"❌ Error installing virtualenv: {e}")
        return False
    
    # Create virtual environment
    env_name = "ecg_fm_env"
    try:
        cmd = [sys.executable, '-m', 'virtualenv', env_name]
        print(f"Creating environment: {env_name}")
        result = subprocess.run(cmd, capture_output=True, text=True)
        
        if result.returncode == 0:
            print(f"βœ… Virtual environment created: {env_name}")
            return True
        else:
            print("❌ Failed to create virtual environment")
            print(result.stderr)
            return False
    except Exception as e:
        print(f"❌ Error creating environment: {e}")
        return False

def install_compatible_packages():
    """Install compatible package versions"""
    print("\nπŸ“¦ Installing Compatible Packages")
    print("=" * 40)
    
    # These versions are compatible with each other
    packages = [
        "numpy==1.24.3",
        "torch==1.13.1",
        "torchvision==0.14.1", 
        "torchaudio==0.13.1",
        "omegaconf==2.3.0",
        "fastapi==0.104.1",
        "uvicorn[standard]==0.24.0",
        "huggingface-hub==0.19.4",
        "transformers==4.21.0",
        "einops==0.7.0",
        "pyyaml==6.0.1"
    ]
    
    # Get the virtual environment Python
    if os.name == 'nt':  # Windows
        python_path = os.path.join("ecg_fm_env", "Scripts", "python.exe")
    else:  # Unix/Linux
        python_path = os.path.join("ecg_fm_env", "bin", "python")
    
    if not os.path.exists(python_path):
        print("❌ Virtual environment Python not found")
        return False
    
    print(f"Using Python: {python_path}")
    
    all_good = True
    
    for package in packages:
        print(f"Installing {package}...")
        try:
            cmd = [python_path, '-m', 'pip', 'install', package]
            result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
            
            if result.returncode == 0:
                print(f"  βœ… {package} installed")
            else:
                print(f"  ❌ {package} failed")
                print(f"     Error: {result.stderr}")
                all_good = False
                
        except subprocess.TimeoutExpired:
            print(f"  ⚠️  {package} timed out")
            all_good = False
        except Exception as e:
            print(f"  ❌ {package} error: {e}")
            all_good = False
    
    return all_good

def test_compatible_environment():
    """Test the compatible environment"""
    print("\nπŸ§ͺ Testing Compatible Environment")
    print("=" * 40)
    
    if os.name == 'nt':  # Windows
        python_path = os.path.join("ecg_fm_env", "Scripts", "python.exe")
    else:  # Unix/Linux
        python_path = os.path.join("ecg_fm_env", "bin", "python")
    
    if not os.path.exists(python_path):
        print("❌ Virtual environment not found")
        return False
    
    # Test imports
    test_script = f"""
import sys
print(f"Python: {{sys.version}}")

try:
    import numpy
    print(f"NumPy: {{numpy.__version__}}")
except ImportError as e:
    print(f"NumPy: ❌ {{e}}")

try:
    import torch
    print(f"PyTorch: {{torch.__version__}}")
except ImportError as e:
    print(f"PyTorch: ❌ {{e}}")

try:
    import omegaconf
    print(f"OmegaConf: {{omegaconf.__version__}}")
except ImportError as e:
    print(f"OmegaConf: ❌ {{e}}")

try:
    import fastapi
    print(f"FastAPI: {{fastapi.__version__}}")
except ImportError as e:
    print(f"FastAPI: ❌ {{e}}")

try:
    import transformers
    print(f"Transformers: {{transformers.__version__}}")
except ImportError as e:
    print(f"Transformers: ❌ {{e}}")
"""
    
    try:
        result = subprocess.run([python_path, '-c', test_script], 
                              capture_output=True, text=True)
        
        if result.returncode == 0:
            print("βœ… Environment test successful!")
            print("\nπŸ“‹ Package Versions:")
            print(result.stdout)
            return True
        else:
            print("❌ Environment test failed!")
            print(result.stderr)
            return False
            
    except Exception as e:
        print(f"❌ Test error: {e}")
        return False

def main():
    """Main function"""
    print("πŸš€ Creating Compatible Environment for ECG-FM")
    print("=" * 60)
    print("This will create a virtual environment with compatible versions")
    print()
    
    # Step 1: Check Python version
    if not check_python_version():
        print("\n⚠️  Warning: Python 3.13.3 may cause issues")
        print("   Virtual environment will help isolate dependencies")
    
    # Step 2: Create virtual environment
    if not create_virtual_env():
        print("\n❌ Failed to create virtual environment")
        return 1
    
    # Step 3: Install compatible packages
    if not install_compatible_packages():
        print("\n❌ Failed to install compatible packages")
        return 1
    
    # Step 4: Test environment
    if not test_compatible_environment():
        print("\n❌ Environment test failed")
        return 1
    
    print("\nπŸŽ‰ COMPATIBLE ENVIRONMENT CREATED!")
    print("βœ… All packages installed with compatible versions")
    print("πŸš€ Ready for local testing before HF upload")
    
    # Instructions
    print("\nπŸ“‹ How to use:")
    if os.name == 'nt':  # Windows
        print("   Activate: ecg_fm_env\\Scripts\\activate")
        print("   Python: ecg_fm_env\\Scripts\\python.exe")
    else:  # Unix/Linux
        print("   Activate: source ecg_fm_env/bin/activate")
        print("   Python: ecg_fm_env/bin/python")
    
    return 0

if __name__ == "__main__":
    sys.exit(main())