Stylique commited on
Commit
fb407f9
·
verified ·
1 Parent(s): 8ef4d59

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +25 -0
  2. post_install.py +157 -0
  3. requirements.txt +3 -2
app.py CHANGED
@@ -17,6 +17,31 @@ packages_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'package
17
  if os.path.exists(packages_dir):
18
  sys.path.append(packages_dir)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  try:
21
  from loop import loop
22
  except ImportError as e:
 
17
  if os.path.exists(packages_dir):
18
  sys.path.append(packages_dir)
19
 
20
+ # Check if complex dependencies are installed
21
+ def check_complex_dependencies():
22
+ """Check if complex dependencies are available"""
23
+ try:
24
+ import torch_sparse
25
+ import torch_scatter
26
+ import nvdiffrast
27
+ import pytorch3d
28
+ return True
29
+ except ImportError:
30
+ return False
31
+
32
+ # Run post-install if needed
33
+ if not check_complex_dependencies():
34
+ print("Complex dependencies not found, running post-install...")
35
+ try:
36
+ import subprocess
37
+ subprocess.run([sys.executable, "post_install.py"], check=True)
38
+ print("Post-install completed, restarting...")
39
+ # Restart the process to pick up new dependencies
40
+ os.execv(sys.executable, ['python'] + sys.argv)
41
+ except Exception as e:
42
+ print(f"Post-install failed: {e}")
43
+ sys.exit(1)
44
+
45
  try:
46
  from loop import loop
47
  except ImportError as e:
post_install.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Post-install script for Hugging Face Spaces
4
+ This script installs complex dependencies that need PyTorch to be available first
5
+ """
6
+
7
+ import os
8
+ import sys
9
+ import subprocess
10
+ import shutil
11
+ from pathlib import Path
12
+
13
+ def run_command(command, cwd=None):
14
+ """Run a shell command and return the result"""
15
+ print(f"Running: {command}")
16
+ result = subprocess.run(command, shell=True, cwd=cwd, capture_output=True, text=True)
17
+ if result.returncode != 0:
18
+ print(f"Error running command: {command}")
19
+ print(f"Error output: {result.stderr}")
20
+ return False
21
+ print(f"Success: {command}")
22
+ return True
23
+
24
+ def install_torch_sparse():
25
+ """Install torch-sparse with the current PyTorch version"""
26
+ print("Installing torch-sparse...")
27
+
28
+ # Get current PyTorch version
29
+ try:
30
+ import torch
31
+ torch_version = torch.__version__
32
+ print(f"Current PyTorch version: {torch_version}")
33
+
34
+ # Extract major.minor version
35
+ version_parts = torch_version.split('.')
36
+ torch_major_minor = f"{version_parts[0]}.{version_parts[1]}"
37
+
38
+ # Try to install torch-sparse with the correct version
39
+ if not run_command(f"pip install torch-sparse -f https://data.pyg.org/whl/torch-{torch_major_minor}+cu121.html"):
40
+ print("Failed to install torch-sparse with pre-built wheel, trying from source...")
41
+ if not run_command("pip install torch-sparse"):
42
+ return False
43
+
44
+ return True
45
+ except Exception as e:
46
+ print(f"Error installing torch-sparse: {e}")
47
+ return False
48
+
49
+ def install_torch_scatter():
50
+ """Install torch-scatter with the current PyTorch version"""
51
+ print("Installing torch-scatter...")
52
+
53
+ try:
54
+ import torch
55
+ torch_version = torch.__version__
56
+ version_parts = torch_version.split('.')
57
+ torch_major_minor = f"{version_parts[0]}.{version_parts[1]}"
58
+
59
+ if not run_command(f"pip install torch-scatter -f https://data.pyg.org/whl/torch-{torch_major_minor}+cu121.html"):
60
+ print("Failed to install torch-scatter with pre-built wheel, trying from source...")
61
+ if not run_command("pip install torch-scatter"):
62
+ return False
63
+
64
+ return True
65
+ except Exception as e:
66
+ print(f"Error installing torch-scatter: {e}")
67
+ return False
68
+
69
+ def install_nvdiffrast():
70
+ """Install nvdiffrast"""
71
+ print("Installing nvdiffrast...")
72
+
73
+ # Create packages directory if it doesn't exist
74
+ packages_dir = Path("packages")
75
+ packages_dir.mkdir(exist_ok=True)
76
+
77
+ # Clone nvdiffrast
78
+ if not (packages_dir / "nvdiffrast").exists():
79
+ if not run_command("git clone https://github.com/NVlabs/nvdiffrast.git", cwd=packages_dir):
80
+ return False
81
+
82
+ # Install nvdiffrast
83
+ nvdiffrast_dir = packages_dir / "nvdiffrast"
84
+ if not run_command("pip install .", cwd=nvdiffrast_dir):
85
+ return False
86
+
87
+ return True
88
+
89
+ def install_pytorch3d():
90
+ """Install PyTorch3D"""
91
+ print("Installing PyTorch3D...")
92
+
93
+ packages_dir = Path("packages")
94
+
95
+ # Clone PyTorch3D
96
+ if not (packages_dir / "pytorch3d").exists():
97
+ if not run_command("git clone https://github.com/facebookresearch/pytorch3d.git", cwd=packages_dir):
98
+ return False
99
+
100
+ # Install PyTorch3D with CUDA support
101
+ pytorch3d_dir = packages_dir / "pytorch3d"
102
+ if not run_command("FORCE_CUDA=1 pip install .", cwd=pytorch3d_dir):
103
+ return False
104
+
105
+ return True
106
+
107
+ def install_fashion_clip():
108
+ """Setup Fashion-CLIP"""
109
+ print("Setting up Fashion-CLIP...")
110
+
111
+ packages_dir = Path("packages")
112
+
113
+ # Clone Fashion-CLIP if not already present
114
+ if not (packages_dir / "fashion-clip").exists():
115
+ if not run_command("git clone https://github.com/patrickjohncyh/fashion-clip.git", cwd=packages_dir):
116
+ return False
117
+
118
+ # Install Fashion-CLIP dependencies
119
+ fashion_clip_dir = packages_dir / "fashion-clip"
120
+ dependencies = ["appdirs", "boto3", "annoy", "validators", "transformers", "datasets"]
121
+
122
+ for dep in dependencies:
123
+ if not run_command(f"pip install {dep}", cwd=fashion_clip_dir):
124
+ print(f"Warning: Failed to install {dep}")
125
+
126
+ return True
127
+
128
+ def main():
129
+ """Main installation function"""
130
+ print("Starting post-installation for Garment3DGen...")
131
+
132
+ # Install complex dependencies
133
+ if not install_torch_sparse():
134
+ print("Failed to install torch-sparse")
135
+ sys.exit(1)
136
+
137
+ if not install_torch_scatter():
138
+ print("Failed to install torch-scatter")
139
+ sys.exit(1)
140
+
141
+ if not install_nvdiffrast():
142
+ print("Failed to install nvdiffrast")
143
+ sys.exit(1)
144
+
145
+ if not install_pytorch3d():
146
+ print("Failed to install PyTorch3D")
147
+ sys.exit(1)
148
+
149
+ if not install_fashion_clip():
150
+ print("Failed to install Fashion-CLIP")
151
+ sys.exit(1)
152
+
153
+ print("Post-installation completed successfully!")
154
+ print("All dependencies are now available.")
155
+
156
+ if __name__ == "__main__":
157
+ main()
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
- # Core dependencies (PyTorch will be installed separately in Dockerfile)
 
2
  clip
3
  imageio
4
  cython
@@ -33,5 +34,5 @@ boto3
33
  annoy
34
  validators
35
 
36
- # Note: PyTorch, torch-sparse, torch-scatter, pytorch3d, and nvdiffrast
37
  # will be installed separately in the Dockerfile to ensure correct versions
 
1
+ # Core dependencies - compatible with HF Spaces PyTorch 2.7.1
2
+ # PyTorch will be installed by HF Spaces automatically
3
  clip
4
  imageio
5
  cython
 
34
  annoy
35
  validators
36
 
37
+ # Note: torch-sparse, torch-scatter, pytorch3d, and nvdiffrast
38
  # will be installed separately in the Dockerfile to ensure correct versions