Ali Mohsin commited on
Commit
4437096
Β·
1 Parent(s): d172fbe
Files changed (1) hide show
  1. app.py +130 -14
app.py CHANGED
@@ -278,16 +278,48 @@ def try_import_loop():
278
  print("βœ“ nvdiffrast imported successfully")
279
  except ImportError as e:
280
  print(f"βœ— nvdiffrast import failed: {e}")
281
- loop_import_error = f"Critical dependency missing: nvdiffrast - {str(e)}"
282
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
 
284
  try:
285
  import pytorch3d
286
  print("βœ“ pytorch3d imported successfully")
287
  except ImportError as e:
288
  print(f"βœ— pytorch3d import failed: {e}")
289
- loop_import_error = f"Critical dependency missing: pytorch3d - {str(e)}"
290
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291
 
292
  # Try to import fashion_clip
293
  try:
@@ -295,8 +327,29 @@ def try_import_loop():
295
  print("βœ“ FashionCLIP imported successfully")
296
  except ImportError as e:
297
  print(f"βœ— FashionCLIP import failed: {e}")
298
- loop_import_error = f"Critical dependency missing: FashionCLIP - {str(e)}"
299
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
300
 
301
  # Now try to import the loop module - this is the core processing engine
302
  try:
@@ -489,8 +542,6 @@ if is_hf_spaces:
489
  else:
490
  print("🏠 Local development environment detected")
491
 
492
-
493
-
494
  # Try to import the loop module after dependency installation attempts
495
  print("πŸ”„ Attempting to import processing engine after dependency checks...")
496
  import_success = try_import_loop()
@@ -546,8 +597,8 @@ DEFAULT_CONFIG = {
546
  'consistency_clip_model': 'ViT-B/32',
547
  'consistency_vit_stride': 8,
548
  'consistency_vit_layer': 11,
549
- 'mesh': './meshes/longsleeve.obj',
550
- 'target_mesh': './meshes_target/jacket_sdf_new.obj',
551
  'retriangulate': 0,
552
  'bsdf': 'diffuse',
553
  'lr': 0.0025,
@@ -571,6 +622,8 @@ DEFAULT_CONFIG = {
571
  'elev_alpha': 1.0,
572
  'elev_beta': 5.0,
573
  'elev_max': 60.0,
 
 
574
  'azim_min': 0.0,
575
  'azim_max': 360.0,
576
  'aug_loc': 1,
@@ -650,12 +703,17 @@ def process_garment(input_type, text_prompt, base_text_prompt, mesh_target_image
650
  "dress_shortsleeve": "dress_shortsleeve"
651
  }
652
  mesh_file = mesh_mapping.get(source_mesh_type, "tshirt")
653
- source_mesh_file = f"./meshes/{mesh_file}.obj"
 
 
 
654
 
655
  # Check if the mesh file exists
656
  if not os.path.exists(source_mesh_file):
657
  return f"Error: Mesh file {source_mesh_file} not found. Please check if the mesh files are available."
658
 
 
 
659
  # Configure for image-to-mesh processing
660
  config.update({
661
  'mesh': source_mesh_file,
@@ -733,11 +791,67 @@ def process_garment(input_type, text_prompt, base_text_prompt, mesh_target_image
733
  # Run the loop with error handling
734
  try:
735
  print("πŸš€ Starting garment generation with real processing engine...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
736
  loop(config)
737
  print("βœ… Garment generation completed successfully!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
738
  except Exception as e:
739
  print(f"❌ Error during garment generation: {e}")
740
- error_message = f"Error during processing: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
741
  return error_message
742
  except RuntimeError as e:
743
  print(f"Runtime error during processing: {e}")
@@ -752,6 +866,7 @@ def process_garment(input_type, text_prompt, base_text_prompt, mesh_target_image
752
  except Exception as e:
753
  print(f"Error during processing: {e}")
754
  error_message = f"Error during processing: {str(e)}"
 
755
  return error_message
756
 
757
  progress(0.9, desc="Processing complete, preparing output...")
@@ -992,10 +1107,11 @@ def create_interface():
992
  status_msg += "Upload a garment image and select mesh type, then click Generate."
993
 
994
  print(f"Text visibility: {text_visibility}, Image to Mesh visibility: {image_to_mesh_visibility}")
 
995
 
996
  return (
997
- gr.update(visible=text_visibility),
998
- gr.update(visible=image_to_mesh_visibility),
999
  status_msg
1000
  )
1001
 
 
278
  print("βœ“ nvdiffrast imported successfully")
279
  except ImportError as e:
280
  print(f"βœ— nvdiffrast import failed: {e}")
281
+ # Try to install nvdiffrast if missing
282
+ try:
283
+ print("πŸ”„ Attempting to install nvdiffrast...")
284
+ import subprocess
285
+ result = subprocess.run([sys.executable, "-m", "pip", "install", "nvdiffrast"],
286
+ capture_output=True, text=True, timeout=300)
287
+ if result.returncode == 0:
288
+ print("βœ… nvdiffrast installed successfully")
289
+ import nvdiffrast
290
+ print("βœ“ nvdiffrast now imported successfully")
291
+ else:
292
+ print(f"⚠️ nvdiffrast installation failed: {result.stderr}")
293
+ loop_import_error = f"Critical dependency missing: nvdiffrast - {str(e)}"
294
+ return False
295
+ except Exception as install_e:
296
+ print(f"⚠️ Could not install nvdiffrast: {install_e}")
297
+ loop_import_error = f"Critical dependency missing: nvdiffrast - {str(e)}"
298
+ return False
299
 
300
  try:
301
  import pytorch3d
302
  print("βœ“ pytorch3d imported successfully")
303
  except ImportError as e:
304
  print(f"βœ— pytorch3d import failed: {e}")
305
+ # Try to install pytorch3d if missing
306
+ try:
307
+ print("πŸ”„ Attempting to install pytorch3d...")
308
+ import subprocess
309
+ result = subprocess.run([sys.executable, "-m", "pip", "install", "pytorch3d", "--no-deps"],
310
+ capture_output=True, text=True, timeout=300)
311
+ if result.returncode == 0:
312
+ print("βœ… pytorch3d installed successfully")
313
+ import pytorch3d
314
+ print("βœ“ pytorch3d now imported successfully")
315
+ else:
316
+ print(f"⚠️ pytorch3d installation failed: {result.stderr}")
317
+ loop_import_error = f"Critical dependency missing: pytorch3d - {str(e)}"
318
+ return False
319
+ except Exception as install_e:
320
+ print(f"⚠️ Could not install pytorch3d: {install_e}")
321
+ loop_import_error = f"Critical dependency missing: pytorch3d - {str(e)}"
322
+ return False
323
 
324
  # Try to import fashion_clip
325
  try:
 
327
  print("βœ“ FashionCLIP imported successfully")
328
  except ImportError as e:
329
  print(f"βœ— FashionCLIP import failed: {e}")
330
+ # Try to install FashionCLIP if missing
331
+ try:
332
+ print("πŸ”„ Attempting to install FashionCLIP...")
333
+ if os.path.exists("packages/fashion_clip"):
334
+ import subprocess
335
+ result = subprocess.run([sys.executable, "-m", "pip", "install", "-e", "packages/fashion_clip"],
336
+ capture_output=True, text=True, timeout=300)
337
+ if result.returncode == 0:
338
+ print("βœ… FashionCLIP installed successfully")
339
+ from packages.fashion_clip.fashion_clip.fashion_clip import FashionCLIP
340
+ print("βœ“ FashionCLIP now imported successfully")
341
+ else:
342
+ print(f"⚠️ FashionCLIP installation failed: {result.stderr}")
343
+ loop_import_error = f"Critical dependency missing: FashionCLIP - {str(e)}"
344
+ return False
345
+ else:
346
+ print("⚠️ FashionCLIP directory not found")
347
+ loop_import_error = f"Critical dependency missing: FashionCLIP - {str(e)}"
348
+ return False
349
+ except Exception as install_e:
350
+ print(f"⚠️ Could not install FashionCLIP: {install_e}")
351
+ loop_import_error = f"Critical dependency missing: FashionCLIP - {str(e)}"
352
+ return False
353
 
354
  # Now try to import the loop module - this is the core processing engine
355
  try:
 
542
  else:
543
  print("🏠 Local development environment detected")
544
 
 
 
545
  # Try to import the loop module after dependency installation attempts
546
  print("πŸ”„ Attempting to import processing engine after dependency checks...")
547
  import_success = try_import_loop()
 
597
  'consistency_clip_model': 'ViT-B/32',
598
  'consistency_vit_stride': 8,
599
  'consistency_vit_layer': 11,
600
+ 'mesh': os.path.join(os.path.dirname(os.path.abspath(__file__)), 'meshes', 'longsleeve.obj'),
601
+ 'target_mesh': os.path.join(os.path.dirname(os.path.abspath(__file__)), 'meshes_target', 'jacket_sdf_new.obj'),
602
  'retriangulate': 0,
603
  'bsdf': 'diffuse',
604
  'lr': 0.0025,
 
622
  'elev_alpha': 1.0,
623
  'elev_beta': 5.0,
624
  'elev_max': 60.0,
625
+ 'azim_alpha': 1.0,
626
+ 'azim_beta': 5.0,
627
  'azim_min': 0.0,
628
  'azim_max': 360.0,
629
  'aug_loc': 1,
 
703
  "dress_shortsleeve": "dress_shortsleeve"
704
  }
705
  mesh_file = mesh_mapping.get(source_mesh_type, "tshirt")
706
+
707
+ # Use absolute paths for mesh files
708
+ current_dir = os.path.dirname(os.path.abspath(__file__))
709
+ source_mesh_file = os.path.join(current_dir, "meshes", f"{mesh_file}.obj")
710
 
711
  # Check if the mesh file exists
712
  if not os.path.exists(source_mesh_file):
713
  return f"Error: Mesh file {source_mesh_file} not found. Please check if the mesh files are available."
714
 
715
+ print(f"Using source mesh: {source_mesh_file}")
716
+
717
  # Configure for image-to-mesh processing
718
  config.update({
719
  'mesh': source_mesh_file,
 
791
  # Run the loop with error handling
792
  try:
793
  print("πŸš€ Starting garment generation with real processing engine...")
794
+ print(f"Configuration: {config}")
795
+
796
+ # Validate mesh files before processing
797
+ if 'mesh' in config and config['mesh']:
798
+ mesh_path = config['mesh']
799
+ if not os.path.exists(mesh_path):
800
+ error_message = f"Error: Source mesh file not found: {mesh_path}"
801
+ print(error_message)
802
+ return error_message
803
+
804
+ # Check if mesh file is valid
805
+ try:
806
+ import pymeshlab
807
+ ms = pymeshlab.MeshSet()
808
+ ms.load_new_mesh(mesh_path)
809
+ if ms.current_mesh().vertex_number() == 0:
810
+ error_message = f"Error: Source mesh file has no vertices: {mesh_path}"
811
+ print(error_message)
812
+ return error_message
813
+ print(f"βœ“ Source mesh validated: {ms.current_mesh().vertex_number()} vertices, {ms.current_mesh().face_number()} faces")
814
+ except Exception as mesh_e:
815
+ print(f"Warning: Could not validate mesh file: {mesh_e}")
816
+
817
  loop(config)
818
  print("βœ… Garment generation completed successfully!")
819
+ except ValueError as ve:
820
+ print(f"❌ Validation error during garment generation: {ve}")
821
+ if "no vertices" in str(ve).lower() or "no faces" in str(ve).lower():
822
+ error_message = f"Error: Invalid mesh data detected. The source mesh appears to be corrupted or empty. Please try a different mesh file."
823
+ elif "jacobian" in str(ve).lower():
824
+ error_message = f"Error: Jacobian computation failed. This may indicate an issue with the mesh structure or processing pipeline."
825
+ elif "index" in str(ve).lower() and "bounds" in str(ve).lower():
826
+ error_message = f"Error: Mesh processing failed due to invalid data structure. This may indicate corrupted mesh files or processing errors."
827
+ else:
828
+ error_message = f"Error during processing: {str(ve)}"
829
+ return error_message
830
+ except FileNotFoundError as fe:
831
+ print(f"❌ File not found error during garment generation: {fe}")
832
+ if "mesh" in str(fe).lower():
833
+ error_message = f"Error: Required mesh file not found during processing. This may indicate an issue with the mesh loading pipeline."
834
+ elif "mtl" in str(fe).lower():
835
+ error_message = f"Error: Material file not found. This may indicate an issue with the mesh file structure."
836
+ else:
837
+ error_message = f"Error: Required file not found during processing: {str(fe)}"
838
+ return error_message
839
  except Exception as e:
840
  print(f"❌ Error during garment generation: {e}")
841
+ import traceback
842
+ traceback.print_exc()
843
+
844
+ # Provide more specific error messages based on error type
845
+ if "nvdiffrast" in str(e).lower():
846
+ error_message = "Error: Rendering engine (nvdiffrast) failed. This may be due to OpenGL/EGL compatibility issues."
847
+ elif "clip" in str(e).lower():
848
+ error_message = "Error: CLIP model failed to load or process. This may be due to model availability or compatibility issues."
849
+ elif "cuda" in str(e).lower() or "gpu" in str(e).lower():
850
+ error_message = "Error: GPU/CUDA processing failed. This may be due to hardware compatibility or driver issues."
851
+ elif "memory" in str(e).lower():
852
+ error_message = "Error: Insufficient memory during processing. Try reducing the number of epochs or using a smaller mesh."
853
+ else:
854
+ error_message = f"Error during processing: {str(e)}"
855
  return error_message
856
  except RuntimeError as e:
857
  print(f"Runtime error during processing: {e}")
 
866
  except Exception as e:
867
  print(f"Error during processing: {e}")
868
  error_message = f"Error during processing: {str(e)}"
869
+ print(error_message)
870
  return error_message
871
 
872
  progress(0.9, desc="Processing complete, preparing output...")
 
1107
  status_msg += "Upload a garment image and select mesh type, then click Generate."
1108
 
1109
  print(f"Text visibility: {text_visibility}, Image to Mesh visibility: {image_to_mesh_visibility}")
1110
+ print(f"Returning updates: text_group={text_visibility}, image_to_mesh_group={image_to_mesh_visibility}")
1111
 
1112
  return (
1113
+ gr.Group.update(visible=text_visibility),
1114
+ gr.Group.update(visible=image_to_mesh_visibility),
1115
  status_msg
1116
  )
1117