YiftachEde commited on
Commit
85865a0
·
verified ·
1 Parent(s): ca0709a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -60
app.py CHANGED
@@ -19,77 +19,54 @@ try:
19
  except Exception as e:
20
  print(f"Error detecting environment versions: {e}")
21
 
22
- # Install PyTorch3D from source
23
- print("Installing PyTorch3D from source...")
24
 
25
  # First uninstall any existing PyTorch3D installation to avoid conflicts
26
  os.system("pip uninstall -y pytorch3d")
27
 
28
- # Install dependencies required for building PyTorch3D
29
- os.system("apt-get update && apt-get install -y git build-essential libglib2.0-0 libsm6 libxrender-dev libxext6 ninja-build")
30
- os.system("pip install 'imageio>=2.5.0' 'matplotlib>=3.1.2' 'numpy>=1.17.3' 'psutil>=5.6.5' 'scipy>=1.3.2' 'tqdm>=4.42.1' 'trimesh>=3.0.0'")
31
  os.system("pip install fvcore iopath")
32
 
33
- # Clone the PyTorch3D repository
34
- os.system("rm -rf pytorch3d") # Remove any existing directory
35
- os.system("git clone https://github.com/facebookresearch/pytorch3d.git")
36
 
37
- # Try to build with CUDA support first
38
- print("Attempting to build PyTorch3D with CUDA support...")
39
- os.environ["FORCE_CUDA"] = "1"
40
- os.environ["TORCH_CUDA_ARCH_LIST"] = "3.5;5.0;6.0;6.1;7.0;7.5;8.0;8.6+PTX"
41
 
42
- # Use a specific commit that is known to be stable
43
- os.system("cd pytorch3d && git checkout v0.7.4")
44
-
45
- # Install with CUDA support
46
- cuda_build_result = os.system("cd pytorch3d && FORCE_CUDA=1 pip install -e .")
47
-
48
- # Verify installation and specifically check for renderer module
49
- import_result = os.popen('python -c "import pytorch3d; from pytorch3d import renderer; print(\'PyTorch3D and renderer successfully imported\')" 2>&1').read()
50
- print(import_result)
51
-
52
- # If CUDA build fails or import fails, try CPU-only build
53
- if cuda_build_result != 0 or 'No module named' in import_result or 'Error' in import_result:
54
- print("CUDA build failed or import failed, trying CPU-only build...")
55
-
56
- # Uninstall any failed installation
57
- os.system("pip uninstall -y pytorch3d")
58
-
59
- # Clean the repository
60
- os.system("rm -rf pytorch3d")
61
- os.system("git clone https://github.com/facebookresearch/pytorch3d.git")
62
- os.system("cd pytorch3d && git checkout v0.7.4")
63
-
64
- # Set environment variables for CPU-only build
65
- os.environ.pop("FORCE_CUDA", None)
66
-
67
- # Install without CUDA
68
- os.system("cd pytorch3d && pip install -e .")
69
-
70
- # Verify again
71
- import_result = os.popen('python -c "import pytorch3d; from pytorch3d import renderer; print(\'PyTorch3D and renderer successfully imported\')" 2>&1').read()
72
- print(import_result)
73
-
74
- # Check specifically for the renderer module
75
- renderer_check = os.popen('python -c "import pytorch3d; print(\'renderer\' in dir(pytorch3d))" 2>&1').read().strip()
76
- print(f"Renderer module check: {renderer_check}")
77
-
78
- # If the renderer module is still missing, create a symbolic link to ensure it's found
79
- if renderer_check == "False" or "No module named" in import_result:
80
- print("Renderer module is missing, creating a workaround...")
81
 
82
  # Get the site-packages directory
83
  site_packages = os.popen('python -c "import site; print(site.getsitepackages()[0])"').read().strip()
84
 
85
  # Check if the pytorch3d directory exists
86
- if os.path.exists(f"{site_packages}/pytorch3d"):
87
- # Create the renderer directory if it doesn't exist
88
- os.system(f"mkdir -p {site_packages}/pytorch3d/renderer")
 
89
 
90
- # Create an __init__.py file in the renderer directory
91
- with open(f"{site_packages}/pytorch3d/renderer/__init__.py", "w") as f:
92
  f.write("""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  # PyTorch3D renderer module
94
  import torch
95
  import warnings
@@ -156,10 +133,49 @@ class FoVPerspectiveCameras:
156
  def __init__(self, *args, **kwargs):
157
  pass
158
  """)
159
-
160
- print("Created custom renderer module")
161
- else:
162
- print(f"PyTorch3D directory not found at {site_packages}/pytorch3d")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
  import torch
165
  import torch.nn as nn
 
19
  except Exception as e:
20
  print(f"Error detecting environment versions: {e}")
21
 
22
+ # Install PyTorch3D with CPU support for ZeroGPU environment
23
+ print("Installing PyTorch3D for CPU/ZeroGPU environment...")
24
 
25
  # First uninstall any existing PyTorch3D installation to avoid conflicts
26
  os.system("pip uninstall -y pytorch3d")
27
 
28
+ # Install dependencies required for PyTorch3D
 
 
29
  os.system("pip install fvcore iopath")
30
 
31
+ # For ZeroGPU, we need a CPU-only version that still has the renderer module
32
+ # Try installing from a pre-built wheel first (faster)
33
+ os.system("pip install pytorch3d")
34
 
35
+ # Verify if the renderer module is available
36
+ import_result = os.popen('python -c "import pytorch3d; print(\'renderer\' in dir(pytorch3d))" 2>&1').read().strip()
37
+ print(f"Renderer module check: {import_result}")
 
38
 
39
+ # If the renderer module is not available, create it manually
40
+ if import_result == "False" or "Error" in import_result:
41
+ print("Renderer module not found, creating it manually...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  # Get the site-packages directory
44
  site_packages = os.popen('python -c "import site; print(site.getsitepackages()[0])"').read().strip()
45
 
46
  # Check if the pytorch3d directory exists
47
+ pytorch3d_dir = f"{site_packages}/pytorch3d"
48
+ if not os.path.exists(pytorch3d_dir):
49
+ print(f"PyTorch3D directory not found at {pytorch3d_dir}, creating it...")
50
+ os.system(f"mkdir -p {pytorch3d_dir}")
51
 
52
+ # Create an __init__.py file in the pytorch3d directory
53
+ with open(f"{pytorch3d_dir}/__init__.py", "w") as f:
54
  f.write("""
55
+ # PyTorch3D module
56
+ import torch
57
+ import warnings
58
+
59
+ warnings.warn("Using custom PyTorch3D module")
60
+
61
+ __version__ = "0.7.4"
62
+ """)
63
+
64
+ # Create the renderer directory if it doesn't exist
65
+ os.system(f"mkdir -p {pytorch3d_dir}/renderer")
66
+
67
+ # Create an __init__.py file in the renderer directory
68
+ with open(f"{pytorch3d_dir}/renderer/__init__.py", "w") as f:
69
+ f.write("""
70
  # PyTorch3D renderer module
71
  import torch
72
  import warnings
 
133
  def __init__(self, *args, **kwargs):
134
  pass
135
  """)
136
+
137
+ print("Created custom renderer module")
138
+
139
+ # Patch the shap_e renderer to handle PyTorch3D renderer import error
140
+ shap_e_renderer_path = "/usr/local/lib/python3.10/site-packages/shap_e/models/stf/renderer.py"
141
+ if os.path.exists(shap_e_renderer_path):
142
+ print(f"Patching shap_e renderer at {shap_e_renderer_path}")
143
+
144
+ # Read the current content
145
+ with open(shap_e_renderer_path, "r") as f:
146
+ content = f.read()
147
+
148
+ # Create a backup
149
+ os.system(f"cp {shap_e_renderer_path} {shap_e_renderer_path}.bak")
150
+
151
+ # Modify the content to handle the error more gracefully
152
+ modified_content = content
153
+
154
+ # Replace the error message
155
+ if "exception rendering with PyTorch3D" in content:
156
+ modified_content = modified_content.replace(
157
+ 'warnings.warn(f"exception rendering with PyTorch3D: {exc}")',
158
+ 'warnings.warn("Using native PyTorch renderer")'
159
+ )
160
+
161
+ # Replace the fallback warning
162
+ if "falling back on native PyTorch renderer" in modified_content:
163
+ modified_content = modified_content.replace(
164
+ 'warnings.warn("falling back on native PyTorch renderer, which does not support full gradients")',
165
+ 'warnings.warn("Using native PyTorch renderer")'
166
+ )
167
+
168
+ # Write the modified content
169
+ with open(shap_e_renderer_path, "w") as f:
170
+ f.write(modified_content)
171
+
172
+ print("Successfully patched shap_e renderer")
173
+ else:
174
+ print(f"shap_e renderer not found at {shap_e_renderer_path}")
175
+
176
+ # Verify the installation again
177
+ import_result = os.popen('python -c "import pytorch3d; from pytorch3d import renderer; print(\'PyTorch3D and renderer successfully imported\')" 2>&1').read()
178
+ print(import_result)
179
 
180
  import torch
181
  import torch.nn as nn