charbel-malo commited on
Commit
4f0b12e
·
verified ·
1 Parent(s): 3965b6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +131 -19
app.py CHANGED
@@ -36,31 +36,143 @@ parser.add_argument("--colab", action="store_true", help="Enable colab mode", de
36
  parser.add_argument("--device", default="cuda", type=str)
37
  user_args = parser.parse_args()
38
 
39
- @spaces.GPU
40
- def find_cuda():
41
- # Check if CUDA_HOME or CUDA_PATH environment variables are set
42
- cuda_home = os.environ.get('CUDA_HOME') or os.environ.get('CUDA_PATH')
43
-
44
- if cuda_home and os.path.exists(cuda_home):
45
- return cuda_home
 
 
46
 
47
- # Search for the nvcc executable in the system's PATH
48
- nvcc_path = shutil.which('nvcc')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- if nvcc_path:
51
- # Remove the 'bin/nvcc' part to get the CUDA installation path
52
- cuda_path = os.path.dirname(os.path.dirname(nvcc_path))
53
- return cuda_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
- cuda_path = find_cuda()
 
58
 
59
- if cuda_path:
60
- print(f"CUDA installation found at: {cuda_path}")
61
- else:
62
- print("CUDA installation not found")
63
 
 
 
64
  ## ------------------------------ DEFAULTS ------------------------------
65
 
66
  USE_COLAB = user_args.colab
 
36
  parser.add_argument("--device", default="cuda", type=str)
37
  user_args = parser.parse_args()
38
 
39
+ from huggingface_hub import hf_hub_download
40
+ import requests
41
+ import os
42
+ from typing import Any, List, Callable
43
+ import time
44
+ import tempfile
45
+ import subprocess
46
+ import gfpgan
47
+ import sys
48
 
49
+ print("Installing cudnn 9")
50
+ # Function to get the installed version of a pip package
51
+ def get_pip_version(package_name):
52
+ try:
53
+ result = subprocess.run(
54
+ [sys.executable, '-m', 'pip', 'show', package_name],
55
+ capture_output=True,
56
+ text=True,
57
+ check=True
58
+ )
59
+ output = result.stdout
60
+ version_line = next(
61
+ line for line in output.split('\n') if line.startswith('Version:')
62
+ )
63
+ return version_line.split(': ')[1]
64
+ except subprocess.CalledProcessError as e:
65
+ print(f"Error executing command: {e}")
66
+ return None
67
 
68
+ # Function to execute shell commands safely
69
+ def run_command(command, description=""):
70
+ try:
71
+ print(f"Executing: {' '.join(command) if isinstance(command, list) else command}")
72
+ result = subprocess.run(command, shell=isinstance(command, str), check=True, text=True, capture_output=True)
73
+ if result.stdout:
74
+ print(result.stdout)
75
+ if result.stderr:
76
+ print(result.stderr)
77
+ except subprocess.CalledProcessError as e:
78
+ print(f"Error during {description}: {e}")
79
+
80
+ print("Starting setup for CUDA 12.4 and cuDNN 9.2.1")
81
+
82
+ # Step 1: Uninstall conflicting ONNX Runtime packages
83
+ print("\nUninstalling conflicting ONNX Runtime packages...")
84
+ run_command([sys.executable, '-m', 'pip', 'uninstall', '-y', 'onnxruntime'], "uninstalling onnxruntime")
85
+ run_command([sys.executable, '-m', 'pip', 'uninstall', '-y', 'onnxruntime-gpu'], "uninstalling onnxruntime-gpu")
86
+
87
+ # Step 2: Install cuDNN 9.2.1 for CUDA 12.4
88
+ print("\nInstalling cuDNN 9.2.1 for CUDA 12.4...")
89
+ package_name = 'nvidia-cudnn-cu12' # Ensure this package corresponds to cuDNN 9.2.1
90
+ desired_version = '9.2.1'
91
+
92
+ installed_version = get_pip_version(package_name)
93
+ if installed_version:
94
+ print(f"Installed version of {package_name}: {installed_version}")
95
+ if installed_version != desired_version:
96
+ print(f"Updating {package_name} to version {desired_version}...")
97
+ run_command([sys.executable, '-m', 'pip', 'install', f'{package_name}=={desired_version}'], f"installing {package_name}=={desired_version}")
98
+ else:
99
+ print(f"{package_name} not found. Installing version {desired_version}...")
100
+ run_command([sys.executable, '-m', 'pip', 'install', f'{package_name}=={desired_version}'], f"installing {package_name}=={desired_version}")
101
+
102
+ # Step 3: Verify installation of cuDNN libraries
103
+ print("\nVerifying cuDNN library installation...")
104
+ find_cudnn_cmd = "find / -path /proc -prune -o -path /sys -prune -o -name 'libcudnn*' -print"
105
+ run_command(find_cudnn_cmd, "searching for libcudnn libraries")
106
+
107
+ # Step 4: Move and copy necessary CUDA libraries
108
+ print("\nOrganizing CUDA libraries...")
109
+ destination_path = '/usr/local/lib/python3.10/site-packages/nvidia/cudnn/lib/'
110
+ os.makedirs(destination_path, exist_ok=True)
111
+
112
+ library_commands = [
113
+ # Moving libraries
114
+ ['mv', '/usr/local/lib/python3.10/site-packages/nvidia/cublas/lib/libcublasLt.so.12', destination_path],
115
+ ['mv', '/usr/local/lib/python3.10/site-packages/nvidia/cublas/lib/libcublas.so.12', destination_path],
116
+ ['mv', '/usr/local/lib/python3.10/site-packages/nvidia/cufft/lib/libcufft.so.11', destination_path],
117
+ ['mv', '/usr/local/lib/python3.10/site-packages/nvidia/cufft/lib/libcufftw.so.11', destination_path],
118
+ ['mv', '/usr/local/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/libcudart.so.12', destination_path],
119
+ ['mv', '/usr/local/lib/python3.10/site-packages/nvidia/cuda_cupti/lib/libcupti.so.12', destination_path],
120
+ # Copying libraries
121
+ ['cp', '/usr/local/lib/python3.10/site-packages/nvidia/curand/lib/libcurand.so.10', destination_path],
122
+ ['cp', '/usr/local/lib/python3.10/site-packages/nvidia/cusolver/lib/libcusolver.so.11', destination_path],
123
+ ['cp', '/usr/local/lib/python3.10/site-packages/nvidia/cusolver/lib/libcusolverMg.so.11', destination_path],
124
+ ['cp', '/usr/local/lib/python3.10/site-packages/nvidia/cusparse/lib/libcusparse.so.12', destination_path],
125
+ ]
126
 
127
+ for cmd in library_commands:
128
+ run_command(cmd, f"processing {cmd[0]} command")
129
+
130
+ # Step 5: Verify CUDA libraries
131
+ print("\nVerifying CUDA libraries...")
132
+ find_cuda_cmd = "find / -path /proc -prune -o -path /sys -prune -o -name 'libcu*' -print"
133
+ run_command(find_cuda_cmd, "searching for CUDA libraries")
134
+
135
+ # Step 6: Install only the GPU variant of ONNX Runtime
136
+ print("\nInstalling ONNX Runtime GPU variant...")
137
+ run_command([sys.executable, '-m', 'pip', 'install', 'onnxruntime-gpu'], "installing onnxruntime-gpu")
138
+
139
+ # Step 7: Install PyTorch with CUDA 12.4 support
140
+ print("\nInstalling PyTorch with CUDA 12.4 support...")
141
+ run_command([
142
+ sys.executable, '-m', 'pip', 'install', '-U',
143
+ 'torch', 'torchvision', 'torchaudio',
144
+ '--index-url', 'https://download.pytorch.org/whl/cu124'
145
+ ], "installing PyTorch with CUDA 12.4")
146
+
147
+ print("\nSetup complete.")
148
+ print("---------------------")
149
+ print(ort.get_available_providers())
150
+
151
+ def conditional_download(download_directory_path, urls):
152
+ if not os.path.exists(download_directory_path):
153
+ os.makedirs(download_directory_path)
154
+ for url in urls:
155
+ filename = url.split('/')[-1]
156
+ file_path = os.path.join(download_directory_path, filename)
157
+ if not os.path.exists(file_path):
158
+ print(f"Downloading {filename}...")
159
+ response = requests.get(url, stream=True)
160
+ if response.status_code == 200:
161
+ with open(file_path, 'wb') as file:
162
+ for chunk in response.iter_content(chunk_size=8192):
163
+ file.write(chunk)
164
+ print(f"{filename} downloaded successfully.")
165
+ else:
166
+ print(f"Failed to download {filename}. Status code: {response.status_code}")
167
+ else:
168
+ print(f"{filename} already exists. Skipping download.")
169
 
170
+ model_path = hf_hub_download(repo_id="countfloyd/deepfake", filename="inswapper_128.onnx")
171
+ conditional_download("./", ['https://github.com/TencentARC/GFPGAN/releases/download/v1.3.4/GFPGANv1.4.pth'])
172
 
 
 
 
 
173
 
174
+ USE_CUDA = True
175
+ BATCH_SIZE = 512
176
  ## ------------------------------ DEFAULTS ------------------------------
177
 
178
  USE_COLAB = user_args.colab