pizb commited on
Commit
fdd09ce
·
1 Parent(s): d8b5b6d

gpu support

Browse files
Files changed (4) hide show
  1. HUGGINGFACE_DEPLOY.md +156 -0
  2. QUICKSTART.md +92 -0
  3. README.md +12 -1
  4. app.py +6 -5
HUGGINGFACE_DEPLOY.md ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Spaces Deployment Guide
2
+
3
+ ## Key Changes for ZeroGPU Support
4
+
5
+ ### 1. Import Order (CRITICAL!)
6
+ The `spaces` package **must** be imported before any CUDA-related packages:
7
+
8
+ ```python
9
+ # CORRECT ✅
10
+ import spaces
11
+ import torch
12
+ import cv2
13
+
14
+ # WRONG ❌
15
+ import torch
16
+ import spaces # Too late!
17
+ ```
18
+
19
+ ### 2. GPU Decorator
20
+ Use `@spaces.GPU` decorator on functions that need GPU:
21
+
22
+ ```python
23
+ @spaces.GPU
24
+ def sam_refine(video_state, point_prompt, click_state, evt):
25
+ initialize_models() # Lazy load on first use
26
+ # ... GPU code here
27
+
28
+ @spaces.GPU(duration=120) # Specify duration for long tasks
29
+ def run_videomama_with_sam2(video_state, click_state):
30
+ # ... GPU code here
31
+ ```
32
+
33
+ ### 3. Lazy Model Loading
34
+ Models should be initialized on first use, not at app startup:
35
+
36
+ ```python
37
+ # Global model variables
38
+ sam2_tracker = None
39
+ videomama_pipeline = None
40
+
41
+ def initialize_models():
42
+ global sam2_tracker, videomama_pipeline
43
+ if sam2_tracker is not None:
44
+ return # Already loaded
45
+ # Load models here...
46
+
47
+ # In GPU functions:
48
+ @spaces.GPU
49
+ def inference_function():
50
+ initialize_models() # Load on first use
51
+ # Use models...
52
+ ```
53
+
54
+ ### 4. Requirements
55
+ Add `spaces` to `requirements.txt`:
56
+
57
+ ```txt
58
+ # CRITICAL: Hugging Face ZeroGPU support
59
+ spaces
60
+
61
+ # Other packages...
62
+ torch>=2.0.0
63
+ gradio==4.31.0
64
+ ```
65
+
66
+ ### 5. README Configuration
67
+ Update `README.md` with hardware specification:
68
+
69
+ ```yaml
70
+ ---
71
+ title: VideoMaMa
72
+ sdk: gradio
73
+ sdk_version: 4.31.0
74
+ app_file: app.py
75
+ python_version: "3.10"
76
+ hardware: zero-a10g # For ZeroGPU Pro
77
+ ---
78
+ ```
79
+
80
+ ## Available Hardware Options
81
+
82
+ For Pro subscribers:
83
+ - `zero-a10g` - NVIDIA A10G (24GB VRAM) - Recommended
84
+ - `zero-a100` - NVIDIA A100 (40GB VRAM) - For larger models
85
+
86
+ ## Duration Parameter
87
+
88
+ The `duration` parameter specifies GPU allocation time:
89
+ ```python
90
+ @spaces.GPU(duration=60) # 1 minute - good for single image
91
+ @spaces.GPU(duration=120) # 2 minutes - good for short videos
92
+ @spaces.GPU(duration=300) # 5 minutes - for long processing
93
+ ```
94
+
95
+ ## Testing Locally
96
+
97
+ To test without ZeroGPU:
98
+ ```python
99
+ # Mock the spaces decorator for local testing
100
+ try:
101
+ import spaces
102
+ except ImportError:
103
+ # Mock for local development
104
+ class MockSpaces:
105
+ @staticmethod
106
+ def GPU(duration=None):
107
+ def decorator(func):
108
+ return func
109
+ return decorator if duration else lambda f: f
110
+ spaces = MockSpaces()
111
+ ```
112
+
113
+ ## Common Errors
114
+
115
+ ### Error: "CUDA has been initialized before importing spaces"
116
+ **Solution**: Move `import spaces` to the very top of `app.py`, before any other imports.
117
+
118
+ ### Error: "GPU time exceeded"
119
+ **Solution**: Increase the `duration` parameter in `@spaces.GPU(duration=X)`.
120
+
121
+ ### Error: "Out of memory"
122
+ **Solution**:
123
+ - Use smaller batch sizes
124
+ - Clear CUDA cache: `torch.cuda.empty_cache()`
125
+ - Consider requesting `zero-a100` hardware
126
+
127
+ ## Deployment Checklist
128
+
129
+ - [ ] `import spaces` is the FIRST import in app.py
130
+ - [ ] All GPU functions have `@spaces.GPU` decorator
131
+ - [ ] Models use lazy loading (initialized on first use)
132
+ - [ ] `spaces` is in requirements.txt
133
+ - [ ] README.md specifies `hardware: zero-a10g`
134
+ - [ ] Tested locally without errors
135
+ - [ ] Git pushed to Hugging Face Space repository
136
+
137
+ ## Files Modified
138
+
139
+ 1. **app.py**
140
+ - Added `import spaces` at the top
141
+ - Added `@spaces.GPU` decorators to GPU functions
142
+ - Implemented lazy model loading
143
+ - Removed model initialization from main block
144
+
145
+ 2. **requirements.txt**
146
+ - Added `spaces` package
147
+
148
+ 3. **README.md**
149
+ - Added hardware configuration
150
+ - Set correct Gradio version
151
+
152
+ ## Support
153
+
154
+ For issues with ZeroGPU:
155
+ - Documentation: https://huggingface.co/docs/hub/spaces-zerogpu
156
+ - Forum: https://discuss.huggingface.co/
QUICKSTART.md ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Quick Start: Fix ZeroGPU CUDA Error
2
+
3
+ ## The Problem
4
+ ```
5
+ RuntimeError: CUDA has been initialized before importing the `spaces` package.
6
+ ```
7
+
8
+ ## The Solution (Applied ✅)
9
+
10
+ ### 1. Import `spaces` FIRST in app.py
11
+ ```python
12
+ # BEFORE (❌ Wrong):
13
+ import os
14
+ import torch
15
+ import spaces # Too late!
16
+
17
+ # AFTER (✅ Fixed):
18
+ import spaces # MUST BE FIRST!
19
+ import os
20
+ import torch
21
+ ```
22
+
23
+ ### 2. Add GPU Decorators
24
+ ```python
25
+ @spaces.GPU
26
+ def sam_refine(...):
27
+ initialize_models() # Lazy load
28
+ # ... processing
29
+
30
+ @spaces.GPU(duration=120) # 2 minutes for video processing
31
+ def run_videomama_with_sam2(...):
32
+ initialize_models() # Lazy load
33
+ # ... processing
34
+ ```
35
+
36
+ ### 3. Lazy Load Models
37
+ ```python
38
+ # Don't load at startup:
39
+ if __name__ == "__main__":
40
+ # initialize_models() # ❌ Remove this
41
+ demo.launch()
42
+
43
+ # Load on first use instead:
44
+ @spaces.GPU
45
+ def inference_function():
46
+ initialize_models() # ✅ Load here
47
+ # ...
48
+ ```
49
+
50
+ ### 4. Update requirements.txt
51
+ ```txt
52
+ spaces # Add this at the top
53
+ torch>=2.0.0
54
+ gradio==4.31.0
55
+ # ... rest
56
+ ```
57
+
58
+ ### 5. Update README.md
59
+ ```yaml
60
+ ---
61
+ hardware: zero-a10g # For ZeroGPU Pro
62
+ sdk_version: 4.31.0
63
+ ---
64
+ ```
65
+
66
+ ## All Changes Applied ✅
67
+
68
+ The following files have been updated:
69
+ - ✅ `app.py` - Added `import spaces` at top, GPU decorators, lazy loading
70
+ - ✅ `requirements.txt` - Added `spaces` package
71
+ - ✅ `README.md` - Added hardware configuration
72
+
73
+ ## Deploy Now
74
+
75
+ Simply push to your Hugging Face Space:
76
+
77
+ ```bash
78
+ git add app.py requirements.txt README.md
79
+ git commit -m "Fix ZeroGPU CUDA initialization error"
80
+ git push
81
+ ```
82
+
83
+ ## Test Result
84
+
85
+ Your Space should now:
86
+ 1. ✅ Start without CUDA errors
87
+ 2. ✅ Allocate GPU only when needed (clicks or processing)
88
+ 3. ✅ Work with ZeroGPU Pro subscription
89
+
90
+ ## Need Help?
91
+
92
+ See `HUGGINGFACE_DEPLOY.md` for detailed documentation.
README.md CHANGED
@@ -4,9 +4,20 @@ emoji: ⚡
4
  colorFrom: gray
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 6.0.0
8
  app_file: app.py
9
  pinned: false
 
 
10
  ---
11
 
 
 
 
 
 
 
 
 
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
4
  colorFrom: gray
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 4.31.0
8
  app_file: app.py
9
  pinned: false
10
+ python_version: "3.10"
11
+ hardware: zero-a10g
12
  ---
13
 
14
+ # VideoMaMa: Interactive Video Matting
15
+
16
+ VideoMaMa is an interactive video matting system that combines SAM2 for mask tracking and VideoMaMa for high-quality alpha matte generation.
17
+
18
+ ## Features
19
+ - 🎯 Interactive mask creation with SAM2
20
+ - 🎨 High-quality video matting with VideoMaMa
21
+ - 🚀 GPU-accelerated inference with Hugging Face ZeroGPU
22
+
23
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -501,8 +501,9 @@ if __name__ == "__main__":
501
 
502
  # Launch demo
503
  demo.queue()
504
- demo.launch(
505
- server_name="127.0.0.1",
506
- server_port=7860,
507
- share=True
508
- )
 
 
501
 
502
  # Launch demo
503
  demo.queue()
504
+ # demo.launch(
505
+ # server_name="127.0.0.1",
506
+ # server_port=7860,
507
+ # share=True
508
+ # )
509
+ demo.launch()