GreenGoat commited on
Commit
e3c888f
·
verified ·
1 Parent(s): 8778654

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -1
app.py CHANGED
@@ -4,6 +4,19 @@ import gradio as gr
4
  import numpy as np
5
  import torch
6
  import safetensors.torch as sf
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  from PIL import Image
9
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
@@ -47,16 +60,20 @@ except Exception as e:
47
  sd15_name = 'stablediffusionapi/realistic-vision-v51'
48
 
49
  # Better CUDA detection and debugging
 
 
50
  print("=== GPU Detection Debug ===")
51
  print(f"PyTorch version: {torch.__version__}")
 
52
  print(f"CUDA available: {torch.cuda.is_available()}")
53
  if torch.cuda.is_available():
54
  print(f"CUDA version: {torch.version.cuda}")
55
  print(f"GPU count: {torch.cuda.device_count()}")
56
  print(f"Current GPU: {torch.cuda.current_device()}")
57
  print(f"GPU name: {torch.cuda.get_device_name()}")
 
58
  else:
59
- print("CUDA not available - checking reasons...")
60
  try:
61
  import subprocess
62
  result = subprocess.run(['nvidia-smi'], capture_output=True, text=True)
@@ -67,6 +84,19 @@ else:
67
  print("nvidia-smi failed, no GPU hardware detected")
68
  except:
69
  print("nvidia-smi command not found")
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
72
  print(f"Selected device: {device}")
@@ -241,6 +271,7 @@ def resize_without_crop(image, target_width, target_height):
241
  pil_image = pil_image.resize((target_width, target_height), Image.LANCZOS)
242
  return np.array(pil_image)
243
 
 
244
  @torch.inference_mode()
245
  def run_rmbg(img, sigma=0.0):
246
  # Simplified background removal
@@ -281,6 +312,7 @@ class BGSource(Enum):
281
  BOTTOM = "Bottom Light"
282
  GREY = "Ambient"
283
 
 
284
  @torch.inference_mode()
285
  def process(input_fg, input_bg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, bg_source):
286
  bg_source = BGSource(bg_source)
@@ -374,6 +406,7 @@ def process(input_fg, input_bg, prompt, image_width, image_height, num_samples,
374
 
375
  return pixels, [fg, bg]
376
 
 
377
  @torch.inference_mode()
378
  def process_relight(input_fg, input_bg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, bg_source):
379
  try:
 
4
  import numpy as np
5
  import torch
6
  import safetensors.torch as sf
7
+ from datetime import datetime
8
+
9
+ # Import spaces for GPU decorator
10
+ try:
11
+ import spaces
12
+ HF_SPACES_GPU = True
13
+ except ImportError:
14
+ HF_SPACES_GPU = False
15
+ # Create a dummy decorator if spaces is not available
16
+ class spaces:
17
+ @staticmethod
18
+ def GPU(func):
19
+ return func
20
 
21
  from PIL import Image
22
  from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipeline
 
60
  sd15_name = 'stablediffusionapi/realistic-vision-v51'
61
 
62
  # Better CUDA detection and debugging
63
+ print("===== Application Startup at", datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "=====")
64
+ print()
65
  print("=== GPU Detection Debug ===")
66
  print(f"PyTorch version: {torch.__version__}")
67
+ print(f"Hugging Face Spaces GPU support: {HF_SPACES_GPU}")
68
  print(f"CUDA available: {torch.cuda.is_available()}")
69
  if torch.cuda.is_available():
70
  print(f"CUDA version: {torch.version.cuda}")
71
  print(f"GPU count: {torch.cuda.device_count()}")
72
  print(f"Current GPU: {torch.cuda.current_device()}")
73
  print(f"GPU name: {torch.cuda.get_device_name()}")
74
+ print("✅ GPU detected and available!")
75
  else:
76
+ print("CUDA not available - checking reasons...")
77
  try:
78
  import subprocess
79
  result = subprocess.run(['nvidia-smi'], capture_output=True, text=True)
 
84
  print("nvidia-smi failed, no GPU hardware detected")
85
  except:
86
  print("nvidia-smi command not found")
87
+
88
+ if HF_SPACES_GPU:
89
+ print("🔄 Running on Hugging Face Spaces with @spaces.GPU decorator")
90
+ print(" GPU will be allocated when GPU-decorated functions are called")
91
+ else:
92
+ print()
93
+ print("🚨 WARNING: This application requires GPU to run properly!")
94
+ print("📋 To fix this issue:")
95
+ print(" 1. Go to your Space settings: https://huggingface.co/spaces/GreenGoat/IClight-demo/settings")
96
+ print(" 2. In the Hardware section, select 'GPU basic' or higher")
97
+ print(" 3. Make sure your Hugging Face account is verified")
98
+ print(" 4. Check if you have available GPU quota")
99
+ print()
100
 
101
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
102
  print(f"Selected device: {device}")
 
271
  pil_image = pil_image.resize((target_width, target_height), Image.LANCZOS)
272
  return np.array(pil_image)
273
 
274
+ @spaces.GPU
275
  @torch.inference_mode()
276
  def run_rmbg(img, sigma=0.0):
277
  # Simplified background removal
 
312
  BOTTOM = "Bottom Light"
313
  GREY = "Ambient"
314
 
315
+ @spaces.GPU
316
  @torch.inference_mode()
317
  def process(input_fg, input_bg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, bg_source):
318
  bg_source = BGSource(bg_source)
 
406
 
407
  return pixels, [fg, bg]
408
 
409
+ @spaces.GPU
410
  @torch.inference_mode()
411
  def process_relight(input_fg, input_bg, prompt, image_width, image_height, num_samples, seed, steps, a_prompt, n_prompt, cfg, highres_scale, highres_denoise, bg_source):
412
  try: