alex4cip Claude commited on
Commit
51c066f
·
1 Parent(s): a0f4ab8

feat: Add flexible PyTorch installation for local vs HF Spaces

Browse files

Problem:
- HF Spaces ZeroGPU requires PyTorch 2.2.0
- Local environments can use latest PyTorch (2.5+)
- Single requirements.txt forces old version on local

Solution:
- Create requirements-local.txt for local development
- Create setup.py for automatic environment detection
- Update README.md with 3 installation methods
- Add INSTALL.md comprehensive guide

Files Added:
1. requirements-local.txt:
- PyTorch >=2.2.0 (no ZeroGPU restriction)
- Same other dependencies as requirements.txt
- Optimized for Mac/Linux/Windows local dev

2. setup.py:
- Auto-detects HF Spaces vs local
- Installs appropriate PyTorch version
- Supports Apple Silicon (MPS)
- Supports CUDA GPUs

3. INSTALL.md:
- Complete installation guide
- Method comparison table
- Platform-specific instructions
- FAQ and troubleshooting

Installation Methods:
1. requirements-local.txt (recommended):
- pip install -r requirements-local.txt
- PyTorch >=2.2.0 latest

2. setup.py (automatic):
- python setup.py
- Auto-detects environment

3. requirements.txt (HF Spaces):
- pip install -r requirements.txt
- PyTorch 2.2.0 (ZeroGPU compatible)

Benefits:
- Local: Use latest PyTorch features
- HF Spaces: ZeroGPU compatibility maintained
- No version conflicts
- Clear separation of concerns
- Platform-specific optimization

Updated Documentation:
- README.md: Add 3 installation methods
- INSTALL.md: Comprehensive guide
- Clear instructions for each platform

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (4) hide show
  1. INSTALL.md +179 -0
  2. README.md +18 -1
  3. requirements-local.txt +25 -0
  4. setup.py +68 -0
INSTALL.md ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 설치 가이드
2
+
3
+ ## PyTorch 버전 제약 문제
4
+
5
+ **문제**: HF Spaces의 ZeroGPU는 PyTorch 2.0.0-2.2.0만 지원하지만, 로컬 환경에서는 최신 버전을 사용하고 싶습니다.
6
+
7
+ **해결책**: 환경별로 다른 requirements 파일 사용
8
+
9
+ ---
10
+
11
+ ## 방법 비교
12
+
13
+ | 방법 | 장점 | 단점 | 추천 |
14
+ |------|------|------|------|
15
+ | **requirements-local.txt** | 간단, 명확 | 수동 선택 필요 | ⭐⭐⭐ |
16
+ | **setup.py** | 자동 감지 | 복잡도 증가 | ⭐⭐ |
17
+ | **requirements.txt** | HF Spaces 호환 | 구버전 강제 | ⭐ |
18
+
19
+ ---
20
+
21
+ ## 방법 1: requirements-local.txt (권장)
22
+
23
+ ### HF Spaces 배포
24
+ ```bash
25
+ # requirements.txt 사용 (자동)
26
+ # PyTorch 2.2.0 (ZeroGPU 호환)
27
+ ```
28
+
29
+ ### 로컬 개발
30
+ ```bash
31
+ # 가상환경 생성
32
+ python -m venv venv
33
+ source venv/bin/activate # Windows: venv\Scripts\activate
34
+
35
+ # 로컬용 requirements 설치
36
+ pip install -r requirements-local.txt
37
+ # PyTorch >=2.2.0 (최신 버전)
38
+ ```
39
+
40
+ ### 파일 구조
41
+ ```
42
+ requirements.txt # HF Spaces용 (PyTorch 2.2.0)
43
+ requirements-local.txt # 로컬용 (PyTorch >=2.2.0)
44
+ ```
45
+
46
+ ---
47
+
48
+ ## 방법 2: setup.py (자동 감지)
49
+
50
+ ### 설치
51
+ ```bash
52
+ python setup.py
53
+ ```
54
+
55
+ ### 동작 방식
56
+ 1. `SPACE_ID` 환경 변수 확인
57
+ 2. HF Spaces → PyTorch 2.2.0 설치
58
+ 3. 로컬 → PyTorch 최신 버전 설치
59
+ 4. Apple Silicon 감지 시 MPS 지원
60
+
61
+ ### 장점
62
+ - 자동 환경 감지
63
+ - 한 명령으로 설치
64
+ - 플랫폼별 최적화
65
+
66
+ ---
67
+
68
+ ## PyTorch 버전별 기능
69
+
70
+ ### PyTorch 2.2.0 (HF Spaces ZeroGPU)
71
+ - ✅ ZeroGPU 호환
72
+ - ✅ 기본 LLM 기능
73
+ - ✅ 안정성 검증됨
74
+
75
+ ### PyTorch >=2.2.0 (로컬)
76
+ - ✅ 최신 기능
77
+ - ✅ 성능 개선
78
+ - ✅ 최신 GPU 지원
79
+ - ✅ MPS 최적화 (Apple Silicon)
80
+
81
+ ---
82
+
83
+ ## 플랫폼별 권장 설치
84
+
85
+ ### Mac (Apple Silicon)
86
+ ```bash
87
+ # M1/M2/M3/M4
88
+ pip install -r requirements-local.txt
89
+ # MPS 가속 지원
90
+ ```
91
+
92
+ ### Linux (NVIDIA GPU)
93
+ ```bash
94
+ pip install -r requirements-local.txt
95
+ # CUDA 12.x 지원
96
+ ```
97
+
98
+ ### Windows (NVIDIA GPU)
99
+ ```bash
100
+ pip install -r requirements-local.txt
101
+ # CUDA 12.x 지원
102
+ ```
103
+
104
+ ### HF Spaces (자동)
105
+ ```bash
106
+ # requirements.txt 자동 사용
107
+ # 수동 설치 불필요
108
+ ```
109
+
110
+ ---
111
+
112
+ ## 검증
113
+
114
+ ### 설치 확인
115
+ ```python
116
+ import torch
117
+ print(f"PyTorch: {torch.__version__}")
118
+ print(f"CUDA: {torch.cuda.is_available()}")
119
+ print(f"MPS: {torch.backends.mps.is_available()}")
120
+ ```
121
+
122
+ ### 예상 출력
123
+
124
+ **로컬 (최신)**:
125
+ ```
126
+ PyTorch: 2.5.1
127
+ CUDA: True / False
128
+ MPS: True / False
129
+ ```
130
+
131
+ **HF Spaces (ZeroGPU)**:
132
+ ```
133
+ PyTorch: 2.2.0
134
+ CUDA: True
135
+ MPS: False
136
+ ```
137
+
138
+ ---
139
+
140
+ ## FAQ
141
+
142
+ ### Q: 로컬에서 PyTorch 2.2.0을 사용해야 하나요?
143
+ A: 아니요. 로컬에서는 `requirements-local.txt`로 최신 버전을 사용하세요.
144
+
145
+ ### Q: requirements.txt와 requirements-local.txt 차이는?
146
+ A:
147
+ - `requirements.txt`: HF Spaces용 (PyTorch 2.2.0)
148
+ - `requirements-local.txt`: 로컬용 (PyTorch >=2.2.0)
149
+
150
+ ### Q: setup.py를 사용해야 하나요?
151
+ A: 선택 사항입니다. 자동 감지를 원하면 사용하세요.
152
+
153
+ ### Q: 두 파일을 동기화해야 하나요?
154
+ A: PyTorch 버전만 다르고 나머지는 동일하게 유지하세요.
155
+
156
+ ---
157
+
158
+ ## 문제 해결
159
+
160
+ ### ImportError: spaces
161
+ **로컬 환경**:
162
+ ```
163
+ 정상 동작 - app.py가 자동으로 감지하고 CPU/GPU 모드로 전환
164
+ ```
165
+
166
+ ### PyTorch 버전 충돌
167
+ ```bash
168
+ pip uninstall torch -y
169
+ pip install -r requirements-local.txt
170
+ ```
171
+
172
+ ### CUDA 버전 불일치
173
+ ```bash
174
+ # CUDA 버전 확인
175
+ nvcc --version
176
+
177
+ # 적절한 PyTorch 설치
178
+ pip install torch --index-url https://download.pytorch.org/whl/cu121
179
+ ```
README.md CHANGED
@@ -207,8 +207,25 @@ cd simple-chatbot-gradio
207
  python -m venv venv
208
  source venv/bin/activate # Windows: venv\Scripts\activate
209
 
210
- # 의존성 설치
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  pip install -r requirements.txt
 
212
  ```
213
 
214
  ### .env 파일 설정
 
207
  python -m venv venv
208
  source venv/bin/activate # Windows: venv\Scripts\activate
209
 
210
+ # 의존성 설치 (3가지 방법)
211
+ ```
212
+
213
+ **방법 1: 로컬 전용 requirements (권장)**
214
+ ```bash
215
+ pip install -r requirements-local.txt
216
+ # 최신 PyTorch 버전 사용 (ZeroGPU 제약 없음)
217
+ ```
218
+
219
+ **방법 2: 자동 환경 감지 설치**
220
+ ```bash
221
+ python setup.py
222
+ # 환경을 자동 감지하고 적절한 버전 설치
223
+ ```
224
+
225
+ **방법 3: HF Spaces용 requirements**
226
+ ```bash
227
  pip install -r requirements.txt
228
+ # PyTorch 2.2.0 (ZeroGPU 호환)
229
  ```
230
 
231
  ### .env 파일 설정
requirements-local.txt ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Local Development Requirements
2
+ # Use this for Mac/Linux/Windows local development
3
+ # Install: pip install -r requirements-local.txt
4
+
5
+ # Gradio Framework
6
+ gradio==5.49.1
7
+
8
+ # ML Core Libraries (Latest versions for local)
9
+ transformers==4.57.1
10
+ torch>=2.2.0 # No ZeroGPU restriction - use latest
11
+ safetensors==0.6.2
12
+ accelerate==0.26.1
13
+
14
+ # Tokenizers & Serialization
15
+ sentencepiece==0.2.0
16
+ protobuf==4.25.1
17
+
18
+ # HF Hub & Authentication
19
+ huggingface-hub>=0.19.0
20
+
21
+ # Environment Management
22
+ python-dotenv==1.0.0
23
+
24
+ # Note: 'spaces' package not needed for local development
25
+ # It will be imported conditionally and gracefully fail
setup.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Smart dependency installer
4
+ Detects environment and installs appropriate PyTorch version
5
+ """
6
+
7
+ import os
8
+ import sys
9
+ import subprocess
10
+ import platform
11
+
12
+ def detect_environment():
13
+ """Detect if running on HF Spaces or local"""
14
+ is_hf_spaces = os.environ.get('SPACE_ID') is not None
15
+ return 'hf_spaces' if is_hf_spaces else 'local'
16
+
17
+ def get_pytorch_version(env):
18
+ """Get appropriate PyTorch version for environment"""
19
+ if env == 'hf_spaces':
20
+ # ZeroGPU compatible version
21
+ return 'torch==2.2.0'
22
+ else:
23
+ # Latest version for local
24
+ # Check if Apple Silicon
25
+ if platform.system() == 'Darwin' and platform.machine() == 'arm64':
26
+ return 'torch>=2.2.0' # MPS support
27
+ else:
28
+ return 'torch>=2.2.0' # CUDA/CPU
29
+
30
+ def install_dependencies():
31
+ """Install dependencies based on environment"""
32
+ env = detect_environment()
33
+ print(f"Detected environment: {env}")
34
+
35
+ # Base dependencies
36
+ base_deps = [
37
+ 'gradio==5.49.1',
38
+ 'transformers==4.57.1',
39
+ 'safetensors==0.6.2',
40
+ 'accelerate==0.26.1',
41
+ 'sentencepiece==0.2.0',
42
+ 'protobuf==4.25.1',
43
+ 'huggingface-hub>=0.19.0',
44
+ 'python-dotenv==1.0.0',
45
+ ]
46
+
47
+ # Add PyTorch with appropriate version
48
+ pytorch = get_pytorch_version(env)
49
+ base_deps.insert(2, pytorch)
50
+
51
+ # Add spaces for HF Spaces only
52
+ if env == 'hf_spaces':
53
+ base_deps.append('spaces')
54
+
55
+ print(f"Installing PyTorch: {pytorch}")
56
+ print(f"Installing {len(base_deps)} packages...")
57
+
58
+ # Install all dependencies
59
+ subprocess.check_call([
60
+ sys.executable, '-m', 'pip', 'install', '--upgrade'
61
+ ] + base_deps)
62
+
63
+ print("✅ Installation complete!")
64
+ print(f"Environment: {env}")
65
+ print(f"PyTorch: {pytorch}")
66
+
67
+ if __name__ == '__main__':
68
+ install_dependencies()