Spaces:
Running
Running
| #!/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()) | |