Spaces:
Paused
Paused
Ali Mohsin commited on
Commit ·
21b6aa3
1
Parent(s): 5e3538b
Fix runtime error
Browse files
app.py
CHANGED
|
@@ -18,6 +18,15 @@ packages_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'package
|
|
| 18 |
if os.path.exists(packages_dir):
|
| 19 |
sys.path.append(packages_dir)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Check if complex dependencies are installed
|
| 22 |
def check_complex_dependencies():
|
| 23 |
"""Check if complex dependencies are available"""
|
|
@@ -46,8 +55,21 @@ def check_complex_dependencies():
|
|
| 46 |
except ImportError:
|
| 47 |
print("torch-scatter not available, but this may not be critical")
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return True
|
| 50 |
-
except ImportError:
|
|
|
|
| 51 |
return False
|
| 52 |
|
| 53 |
# Run post-install if needed
|
|
@@ -70,13 +92,63 @@ if not check_complex_dependencies():
|
|
| 70 |
print(f"Post-install failed: {e}")
|
| 71 |
sys.exit(1)
|
| 72 |
|
|
|
|
|
|
|
| 73 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
from loop import loop
|
|
|
|
| 75 |
except ImportError as e:
|
| 76 |
print(f"Error importing loop: {e}")
|
| 77 |
print("Make sure all dependencies are installed correctly")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
sys.exit(1)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
# Global variables for configuration
|
| 81 |
DEFAULT_CONFIG = {
|
| 82 |
'output_path': './outputs',
|
|
@@ -303,8 +375,41 @@ def process_garment(input_type, text_prompt, base_text_prompt, target_image, bas
|
|
| 303 |
|
| 304 |
# Run the main processing loop
|
| 305 |
progress(0.2, desc="Running garment generation...")
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
progress(0.9, desc="Processing complete, preparing output...")
|
| 309 |
|
| 310 |
# Look for output files, prioritize mesh files
|
|
@@ -576,6 +681,34 @@ def create_interface():
|
|
| 576 |
return interface
|
| 577 |
|
| 578 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
# Create and launch the interface
|
| 580 |
-
|
| 581 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if os.path.exists(packages_dir):
|
| 19 |
sys.path.append(packages_dir)
|
| 20 |
|
| 21 |
+
# Fix for torchvision operator error in newer PyTorch versions
|
| 22 |
+
try:
|
| 23 |
+
import torch._custom_ops
|
| 24 |
+
if not hasattr(torch._custom_ops, "_register_all_aten_ops"):
|
| 25 |
+
# Add this attribute to avoid errors with newer PyTorch versions and torchvision
|
| 26 |
+
setattr(torch._custom_ops, "_register_all_aten_ops", lambda: None)
|
| 27 |
+
except:
|
| 28 |
+
pass
|
| 29 |
+
|
| 30 |
# Check if complex dependencies are installed
|
| 31 |
def check_complex_dependencies():
|
| 32 |
"""Check if complex dependencies are available"""
|
|
|
|
| 55 |
except ImportError:
|
| 56 |
print("torch-scatter not available, but this may not be critical")
|
| 57 |
|
| 58 |
+
# Try to safely import torchvision
|
| 59 |
+
try:
|
| 60 |
+
import torchvision
|
| 61 |
+
print(f"torchvision {torchvision.__version__} loaded successfully")
|
| 62 |
+
except RuntimeError as e:
|
| 63 |
+
if "operator torchvision::nms does not exist" in str(e):
|
| 64 |
+
print("WARNING: Compatibility issue with torchvision. Will attempt to continue anyway.")
|
| 65 |
+
else:
|
| 66 |
+
print(f"WARNING: torchvision error: {e}")
|
| 67 |
+
except ImportError:
|
| 68 |
+
print("WARNING: torchvision not available")
|
| 69 |
+
|
| 70 |
return True
|
| 71 |
+
except ImportError as e:
|
| 72 |
+
print(f"Dependency error: {e}")
|
| 73 |
return False
|
| 74 |
|
| 75 |
# Run post-install if needed
|
|
|
|
| 92 |
print(f"Post-install failed: {e}")
|
| 93 |
sys.exit(1)
|
| 94 |
|
| 95 |
+
# Custom import handling for loop module to handle dependency issues
|
| 96 |
+
loop = None
|
| 97 |
try:
|
| 98 |
+
# First, try to patch the torchvision runtime error
|
| 99 |
+
try:
|
| 100 |
+
import torchvision
|
| 101 |
+
except RuntimeError as e:
|
| 102 |
+
if "operator torchvision::nms does not exist" in str(e):
|
| 103 |
+
print("Detected torchvision operator issue. Attempting to work around...")
|
| 104 |
+
# Create a dummy function to replace the problematic one
|
| 105 |
+
import types
|
| 106 |
+
dummy_nms = types.SimpleNamespace()
|
| 107 |
+
dummy_nms.__name__ = "nms"
|
| 108 |
+
|
| 109 |
+
# Try to register it with torch ops
|
| 110 |
+
try:
|
| 111 |
+
if hasattr(torch.ops, 'torchvision'):
|
| 112 |
+
if not hasattr(torch.ops.torchvision, 'nms'):
|
| 113 |
+
torch.ops.torchvision.nms = lambda *args, **kwargs: torch.zeros(0, dtype=torch.int64)
|
| 114 |
+
print("Applied torchvision.nms workaround")
|
| 115 |
+
except:
|
| 116 |
+
print("Failed to apply torchvision.nms workaround")
|
| 117 |
+
|
| 118 |
+
# Now try to import the loop module
|
| 119 |
from loop import loop
|
| 120 |
+
print("Successfully imported loop module")
|
| 121 |
except ImportError as e:
|
| 122 |
print(f"Error importing loop: {e}")
|
| 123 |
print("Make sure all dependencies are installed correctly")
|
| 124 |
+
if "torchvision" in str(e) or "torch" in str(e):
|
| 125 |
+
print("This appears to be a PyTorch/torchvision compatibility issue. Please check versions.")
|
| 126 |
+
sys.exit(1)
|
| 127 |
+
except RuntimeError as e:
|
| 128 |
+
print(f"Runtime error importing loop: {e}")
|
| 129 |
+
if "operator torchvision::nms does not exist" in str(e):
|
| 130 |
+
print("This is a known issue with incompatible PyTorch/torchvision versions.")
|
| 131 |
sys.exit(1)
|
| 132 |
|
| 133 |
+
# Ensure NeuralJacobianFields is properly configured
|
| 134 |
+
try:
|
| 135 |
+
# Check if PoissonSystem.py needs to be modified to disable torch-sparse
|
| 136 |
+
poisson_system_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
| 137 |
+
"NeuralJacobianFields", "PoissonSystem.py")
|
| 138 |
+
if os.path.exists(poisson_system_path):
|
| 139 |
+
with open(poisson_system_path, 'r') as f:
|
| 140 |
+
content = f.read()
|
| 141 |
+
|
| 142 |
+
if "USE_TORCH_SPARSE = True" in content:
|
| 143 |
+
print("Disabling torch-sparse in PoissonSystem.py")
|
| 144 |
+
content = content.replace("USE_TORCH_SPARSE = True", "USE_TORCH_SPARSE = False")
|
| 145 |
+
with open(poisson_system_path, 'w') as f:
|
| 146 |
+
f.write(content)
|
| 147 |
+
print("Successfully disabled torch-sparse in PoissonSystem.py")
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"Warning: Could not check/modify NeuralJacobianFields configuration: {e}")
|
| 150 |
+
# Continue execution, as this is not fatal
|
| 151 |
+
|
| 152 |
# Global variables for configuration
|
| 153 |
DEFAULT_CONFIG = {
|
| 154 |
'output_path': './outputs',
|
|
|
|
| 375 |
|
| 376 |
# Run the main processing loop
|
| 377 |
progress(0.2, desc="Running garment generation...")
|
| 378 |
+
try:
|
| 379 |
+
# Check if loop is available
|
| 380 |
+
if loop is None:
|
| 381 |
+
error_message = "Error: The garment generation engine could not be loaded due to dependency issues."
|
| 382 |
+
print(error_message)
|
| 383 |
+
# Create an error file to return
|
| 384 |
+
error_file = os.path.join(temp_dir, "error_report.txt")
|
| 385 |
+
with open(error_file, 'w') as f:
|
| 386 |
+
f.write("Garment3DGen Error Report\n\n")
|
| 387 |
+
f.write(error_message + "\n\n")
|
| 388 |
+
f.write("This is likely due to compatibility issues between PyTorch and torchvision.")
|
| 389 |
+
return error_file
|
| 390 |
+
|
| 391 |
+
# Run the loop with error handling
|
| 392 |
+
loop(config)
|
| 393 |
+
except RuntimeError as e:
|
| 394 |
+
print(f"Runtime error during processing: {e}")
|
| 395 |
+
if "operator torchvision::nms does not exist" in str(e):
|
| 396 |
+
print("This is a known issue with PyTorch/torchvision version incompatibility.")
|
| 397 |
+
|
| 398 |
+
# Create error file
|
| 399 |
+
error_file = os.path.join(temp_dir, "error_report.txt")
|
| 400 |
+
with open(error_file, 'w') as f:
|
| 401 |
+
f.write("Garment3DGen Error Report\n\n")
|
| 402 |
+
f.write(f"Error during processing: {str(e)}\n\n")
|
| 403 |
+
f.write("Please check that you have compatible versions of PyTorch and torchvision installed.")
|
| 404 |
+
return error_file
|
| 405 |
+
except Exception as e:
|
| 406 |
+
print(f"Error during processing: {e}")
|
| 407 |
+
error_file = os.path.join(temp_dir, "error_report.txt")
|
| 408 |
+
with open(error_file, 'w') as f:
|
| 409 |
+
f.write("Garment3DGen Error Report\n\n")
|
| 410 |
+
f.write(f"Error: {str(e)}")
|
| 411 |
+
return error_file
|
| 412 |
+
|
| 413 |
progress(0.9, desc="Processing complete, preparing output...")
|
| 414 |
|
| 415 |
# Look for output files, prioritize mesh files
|
|
|
|
| 681 |
return interface
|
| 682 |
|
| 683 |
if __name__ == "__main__":
|
| 684 |
+
# Add special handling for torchvision operator issue
|
| 685 |
+
try:
|
| 686 |
+
import torchvision
|
| 687 |
+
print(f"torchvision {torchvision.__version__} loaded successfully")
|
| 688 |
+
except RuntimeError as e:
|
| 689 |
+
if "operator torchvision::nms does not exist" in str(e):
|
| 690 |
+
print("WARNING: Detected torchvision operator issue. Attempting to continue anyway.")
|
| 691 |
+
# Create a monkeypatch for the problem
|
| 692 |
+
try:
|
| 693 |
+
import types
|
| 694 |
+
if not hasattr(torch, "ops"):
|
| 695 |
+
torch.ops = types.SimpleNamespace()
|
| 696 |
+
if not hasattr(torch.ops, "torchvision"):
|
| 697 |
+
torch.ops.torchvision = types.SimpleNamespace()
|
| 698 |
+
torch.ops.torchvision.nms = lambda *args, **kwargs: torch.zeros(0, dtype=torch.int64)
|
| 699 |
+
print("Applied workaround for torchvision.nms issue")
|
| 700 |
+
except Exception as patch_error:
|
| 701 |
+
print(f"Failed to apply workaround: {patch_error}")
|
| 702 |
+
|
| 703 |
# Create and launch the interface
|
| 704 |
+
try:
|
| 705 |
+
interface = create_interface()
|
| 706 |
+
interface.launch()
|
| 707 |
+
except Exception as e:
|
| 708 |
+
print(f"Error launching interface: {e}")
|
| 709 |
+
# If this is related to the torchvision issue, provide a more helpful message
|
| 710 |
+
if "torchvision" in str(e) or "operator" in str(e):
|
| 711 |
+
print("\n" + "="*80)
|
| 712 |
+
print("CRITICAL ERROR: This appears to be related to PyTorch/torchvision compatibility issues.")
|
| 713 |
+
print("The Hugging Face Spaces environment might have incompatible versions installed.")
|
| 714 |
+
print("="*80 + "\n")
|