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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π Deployment Guide for Hugging Face Spaces
|
| 2 |
+
|
| 3 |
+
This guide will walk you through deploying your voice recognition model to Hugging Face Spaces.
|
| 4 |
+
|
| 5 |
+
## π Prerequisites
|
| 6 |
+
|
| 7 |
+
1. **Hugging Face Account**: Create an account at [huggingface.co](https://huggingface.co)
|
| 8 |
+
2. **Model File**: Your trained model `voice_recognition_fullmodel.pth`
|
| 9 |
+
3. **Git LFS**: For handling large model files
|
| 10 |
+
|
| 11 |
+
## ποΈ File Structure
|
| 12 |
+
|
| 13 |
+
Your deployment should have this structure:
|
| 14 |
+
```
|
| 15 |
+
your-voice-recognition-space/
|
| 16 |
+
βββ app.py # Main Gradio application
|
| 17 |
+
βββ requirements.txt # Python dependencies
|
| 18 |
+
βββ README.md # Project documentation
|
| 19 |
+
βββ voice_recognition_fullmodel.pth # Your trained model
|
| 20 |
+
βββ Dockerfile # Optional: for custom container
|
| 21 |
+
βββ .gitignore # Git ignore file
|
| 22 |
+
βββ DEPLOYMENT_GUIDE.md # This file
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
## π§ Step-by-Step Deployment
|
| 26 |
+
|
| 27 |
+
### Step 1: Create a New Space
|
| 28 |
+
|
| 29 |
+
1. Go to [huggingface.co/new-space](https://huggingface.co/new-space)
|
| 30 |
+
2. Choose a name for your space (e.g., `voice-recognition-security`)
|
| 31 |
+
3. Select **Gradio** as the SDK
|
| 32 |
+
4. Choose **Public** or **Private** visibility
|
| 33 |
+
5. Click **Create Space**
|
| 34 |
+
|
| 35 |
+
### Step 2: Prepare Your Files
|
| 36 |
+
|
| 37 |
+
1. **Update app.py**: Make sure the user classes in the label encoder match your trained model:
|
| 38 |
+
```python
|
| 39 |
+
# In app.py, update this line with your actual user classes
|
| 40 |
+
all_users = ['user1', 'user2', 'user3', 'user4', 'user5', 'user6', 'user7']
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
2. **Model File**: Ensure your `voice_recognition_fullmodel.pth` is in the root directory
|
| 44 |
+
|
| 45 |
+
3. **Test Locally** (Optional):
|
| 46 |
+
```bash
|
| 47 |
+
pip install -r requirements.txt
|
| 48 |
+
python app.py
|
| 49 |
+
```
|
| 50 |
+
|
| 51 |
+
### Step 3: Upload Files
|
| 52 |
+
|
| 53 |
+
#### Option A: Web Interface
|
| 54 |
+
1. Go to your space's page
|
| 55 |
+
2. Click **Files** tab
|
| 56 |
+
3. Upload each file individually
|
| 57 |
+
4. For the model file, you might need to use Git LFS (see Option B)
|
| 58 |
+
|
| 59 |
+
#### Option B: Git Clone (Recommended for large files)
|
| 60 |
+
1. Clone your space repository:
|
| 61 |
+
```bash
|
| 62 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 63 |
+
cd YOUR_SPACE_NAME
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
2. Set up Git LFS for large files:
|
| 67 |
+
```bash
|
| 68 |
+
git lfs install
|
| 69 |
+
git lfs track "*.pth"
|
| 70 |
+
git add .gitattributes
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
3. Add all your files:
|
| 74 |
+
```bash
|
| 75 |
+
cp /path/to/your/files/* .
|
| 76 |
+
git add .
|
| 77 |
+
git commit -m "Initial deployment of voice recognition system"
|
| 78 |
+
git push
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
### Step 4: Configure Space Settings
|
| 82 |
+
|
| 83 |
+
1. Go to your space's **Settings** tab
|
| 84 |
+
2. **Hardware**:
|
| 85 |
+
- For CPU inference: Basic (free)
|
| 86 |
+
- For faster processing: CPU Upgrade ($0.05/hour)
|
| 87 |
+
3. **Timeout**: Set to appropriate value (default is usually fine)
|
| 88 |
+
4. **Visibility**: Adjust as needed
|
| 89 |
+
|
| 90 |
+
### Step 5: Monitor Deployment
|
| 91 |
+
|
| 92 |
+
1. Your space will automatically build after pushing files
|
| 93 |
+
2. Check the **Logs** tab for any errors
|
| 94 |
+
3. Once built, your space will be available at: `https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME`
|
| 95 |
+
|
| 96 |
+
## π Troubleshooting
|
| 97 |
+
|
| 98 |
+
### Common Issues and Solutions
|
| 99 |
+
|
| 100 |
+
#### 1. Model Loading Errors
|
| 101 |
+
**Problem**: `FileNotFoundError` or model loading issues
|
| 102 |
+
**Solution**:
|
| 103 |
+
- Ensure `voice_recognition_fullmodel.pth` is in the root directory
|
| 104 |
+
- Check file size limits (use Git LFS for files >10MB)
|
| 105 |
+
- Verify model architecture matches training code
|
| 106 |
+
|
| 107 |
+
#### 2. Dependency Issues
|
| 108 |
+
**Problem**: Import errors or package conflicts
|
| 109 |
+
**Solution**:
|
| 110 |
+
- Update `requirements.txt` with exact versions
|
| 111 |
+
- Test locally with a clean virtual environment
|
| 112 |
+
- Check for GPU-specific packages if using CPU deployment
|
| 113 |
+
|
| 114 |
+
#### 3. Memory Issues
|
| 115 |
+
**Problem**: OutOfMemoryError during model loading
|
| 116 |
+
**Solution**:
|
| 117 |
+
- Use CPU-only inference: `map_location='cpu'`
|
| 118 |
+
- Consider model quantization for smaller size
|
| 119 |
+
- Upgrade to a higher memory tier
|
| 120 |
+
|
| 121 |
+
#### 4. Audio Processing Errors
|
| 122 |
+
**Problem**: Librosa or audio processing failures
|
| 123 |
+
**Solution**:
|
| 124 |
+
- Install system audio libraries in Dockerfile
|
| 125 |
+
- Add error handling for unsupported formats
|
| 126 |
+
- Test with various audio file types
|
| 127 |
+
|
| 128 |
+
### Example Error Fixes
|
| 129 |
+
|
| 130 |
+
#### Fix 1: Model Architecture Mismatch
|
| 131 |
+
```python
|
| 132 |
+
# In app.py, add this fallback loading method
|
| 133 |
+
try:
|
| 134 |
+
model = torch.load('voice_recognition_fullmodel.pth', map_location=device)
|
| 135 |
+
model.eval()
|
| 136 |
+
except Exception as e:
|
| 137 |
+
print(f"Loading full model failed: {e}")
|
| 138 |
+
# Create model architecture and load state dict
|
| 139 |
+
model = TransferLearningModel(len(all_users))
|
| 140 |
+
state_dict = torch.load('voice_recognition_fullmodel.pth', map_location=device)
|
| 141 |
+
model.load_state_dict(state_dict)
|
| 142 |
+
model.eval()
|
| 143 |
+
```
|
| 144 |
+
|
| 145 |
+
#### Fix 2: Audio Format Support
|
| 146 |
+
```python
|
| 147 |
+
# Add more robust audio loading
|
| 148 |
+
def load_audio_robust(file_path):
|
| 149 |
+
try:
|
| 150 |
+
audio, sr = librosa.load(file_path, res_type='kaiser_fast')
|
| 151 |
+
return audio, sr
|
| 152 |
+
except Exception as e1:
|
| 153 |
+
try:
|
| 154 |
+
import soundfile as sf
|
| 155 |
+
audio, sr = sf.read(file_path)
|
| 156 |
+
if len(audio.shape) > 1:
|
| 157 |
+
audio = audio[:, 0] # Take first channel
|
| 158 |
+
return audio, sr
|
| 159 |
+
except Exception as e2:
|
| 160 |
+
raise Exception(f"Could not load audio: {e1}, {e2}")
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
## π Security Considerations
|
| 164 |
+
|
| 165 |
+
### For Production Deployment:
|
| 166 |
+
|
| 167 |
+
1. **Environment Variables**: Store sensitive config in space secrets
|
| 168 |
+
2. **Rate Limiting**: Implement request throttling
|
| 169 |
+
3. **Input Validation**: Validate audio file types and sizes
|
| 170 |
+
4. **Logging**: Add comprehensive logging for security monitoring
|
| 171 |
+
|
| 172 |
+
## π Performance Optimization
|
| 173 |
+
|
| 174 |
+
### Tips for Better Performance:
|
| 175 |
+
|
| 176 |
+
1. **Model Optimization**:
|
| 177 |
+
```python
|
| 178 |
+
# Quantize model for smaller size and faster inference
|
| 179 |
+
model = torch.quantization.quantize_dynamic(
|
| 180 |
+
model, {torch.nn.Linear}, dtype=torch.qint8
|
| 181 |
+
)
|
| 182 |
+
```
|
| 183 |
+
|
| 184 |
+
2. **Caching**: Cache model loading and feature extraction
|
| 185 |
+
3. **Batch Processing**: Process multiple files if needed
|
| 186 |
+
4. **Memory Management**: Clear unused variables
|
| 187 |
+
|
| 188 |
+
## π― Testing Your Deployment
|
| 189 |
+
|
| 190 |
+
### Test Cases:
|
| 191 |
+
1. **Valid User Audio**: Test with authorized user samples
|
| 192 |
+
2. **Invalid User Audio**: Test with unauthorized samples
|
| 193 |
+
3. **Various Formats**: Test .wav, .mp3, .flac files
|
| 194 |
+
4. **Edge Cases**: Empty files, very short/long audio
|
| 195 |
+
5. **Noise Tests**: Test with background noise
|
| 196 |
+
|
| 197 |
+
### Validation Script:
|
| 198 |
+
```python
|
| 199 |
+
def test_deployment():
|
| 200 |
+
# Test cases for your deployed model
|
| 201 |
+
test_cases = [
|
| 202 |
+
("valid_user1.wav", True),
|
| 203 |
+
("invalid_user.wav", False),
|
| 204 |
+
("noisy_audio.mp3", True), # Should still work
|
| 205 |
+
]
|
| 206 |
+
|
| 207 |
+
for audio_file, expected_access in test_cases:
|
| 208 |
+
result = predict_voice(audio_file)
|
| 209 |
+
print(f"File: {audio_file}, Expected: {expected_access}, Got: {result[0]}")
|
| 210 |
+
```
|
| 211 |
+
|
| 212 |
+
## π Support
|
| 213 |
+
|
| 214 |
+
If you encounter issues:
|
| 215 |
+
1. Check the [Hugging Face Spaces documentation](https://huggingface.co/docs/hub/spaces)
|
| 216 |
+
2. Review the logs in your space's **Logs** tab
|
| 217 |
+
3. Join the [Hugging Face Discord](https://discord.gg/huggingface) for community support
|
| 218 |
+
4. Open an issue in this repository
|
| 219 |
+
|
| 220 |
+
---
|
| 221 |
+
|
| 222 |
+
Good luck with your deployment! π
|