Spaces:
Sleeping
Sleeping
Commit
Β·
cb2ce22
0
Parent(s):
Complete history cleanup and model removal
Browse files- .DS_Store +0 -0
- .gitattributes +35 -0
- DEPLOYMENT_GUIDE.md +285 -0
- HF_DOWNLOAD_STRATEGY.md +272 -0
- Pipfile +16 -0
- Pipfile.lock +0 -0
- README.md +98 -0
- app.py +297 -0
- requirements.txt +9 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
DEPLOYMENT_GUIDE.md
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hugging Face Spaces Deployment Guide
|
| 2 |
+
|
| 3 |
+
## Quick Start for Download-at-Runtime
|
| 4 |
+
|
| 5 |
+
This guide walks you through deploying your WASH CFM Topic Classifier to Hugging Face Spaces using the Download-at-Runtime strategy.
|
| 6 |
+
|
| 7 |
+
## Prerequisites
|
| 8 |
+
|
| 9 |
+
1. β
Your model files are ready (`.safetensors`, config files, tokenizer files)
|
| 10 |
+
2. β
You have a Hugging Face account
|
| 11 |
+
3. β
You've created a public model repository on Hugging Face Hub
|
| 12 |
+
|
| 13 |
+
## Step 1: Upload Model to Hugging Face Hub
|
| 14 |
+
|
| 15 |
+
### 1.1 Create Model Repository
|
| 16 |
+
|
| 17 |
+
1. Go to [huggingface.co/new](https://huggingface.co/new)
|
| 18 |
+
2. Select **"Model"** tab
|
| 19 |
+
3. Repository name: `wash-cfm-classifier` (or your preferred name)
|
| 20 |
+
4. Make it **Public** (required for Spaces)
|
| 21 |
+
5. Click **"Create a new model"**
|
| 22 |
+
|
| 23 |
+
### 1.2 Upload Model Files
|
| 24 |
+
|
| 25 |
+
Upload these files to your repository:
|
| 26 |
+
|
| 27 |
+
```
|
| 28 |
+
π wash-cfm-classifier/
|
| 29 |
+
βββ model.safetensors (~400-500MB)
|
| 30 |
+
βββ config.json (~1KB)
|
| 31 |
+
βββ tokenizer.json (~2-3MB)
|
| 32 |
+
βββ tokenizer_config.json (~1KB)
|
| 33 |
+
βββ special_tokens_map.json (~1KB)
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
**Methods to upload:**
|
| 37 |
+
- **Web Interface**: Drag and drop files
|
| 38 |
+
- **Git LFS**: For command-line users
|
| 39 |
+
- **Python Script**: Use `huggingface_hub` library
|
| 40 |
+
|
| 41 |
+
### 1.3 Add Model Card (Optional but Recommended)
|
| 42 |
+
|
| 43 |
+
Create a `README.md` in your repository:
|
| 44 |
+
|
| 45 |
+
```markdown
|
| 46 |
+
# WASH CFM Topic Classifier
|
| 47 |
+
|
| 48 |
+
A fine-tuned ModernBERT model for classifying WASH (Water, Sanitation, and Hygiene) feedback into topic categories.
|
| 49 |
+
|
| 50 |
+
## Usage
|
| 51 |
+
|
| 52 |
+
```python
|
| 53 |
+
from transformers import pipeline
|
| 54 |
+
|
| 55 |
+
classifier = pipeline("text-classification",
|
| 56 |
+
model="your-username/wash-cfm-classifier")
|
| 57 |
+
result = classifier("The water pump is broken")
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
## Model Details
|
| 61 |
+
|
| 62 |
+
- **Base Model**: modernbert-large
|
| 63 |
+
- **Fine-tuned on**: WASH CFM feedback data
|
| 64 |
+
- **Task**: Multi-label text classification
|
| 65 |
+
- **Labels**: [Add your actual labels]
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
## Step 2: Update Your Application Code
|
| 69 |
+
|
| 70 |
+
### 2.1 Configuration
|
| 71 |
+
|
| 72 |
+
In `app.py`, update the configuration section:
|
| 73 |
+
|
| 74 |
+
```python
|
| 75 |
+
# CONFIGURATION SECTION
|
| 76 |
+
HF_REPO_ID = "your-username/wash-cfm-classifier" # β Replace with your repo
|
| 77 |
+
HF_MODEL_CACHE_DIR = "./model_cache" # Cache directory
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
### 2.2 Verify Dependencies
|
| 81 |
+
|
| 82 |
+
Ensure `requirements.txt` includes:
|
| 83 |
+
|
| 84 |
+
```txt
|
| 85 |
+
huggingface_hub>=0.16.0
|
| 86 |
+
torch>=2.0.0
|
| 87 |
+
transformers>=4.30.0
|
| 88 |
+
gradio>=4.0.0
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
## Step 3: Create Hugging Face Space
|
| 92 |
+
|
| 93 |
+
### 3.1 Create New Space
|
| 94 |
+
|
| 95 |
+
1. Go to [huggingface.co/spaces](https://huggingface.co/spaces)
|
| 96 |
+
2. Click **"Create new Space"**
|
| 97 |
+
3. Fill in details:
|
| 98 |
+
- **Space name**: `wash-cfm-classifier` (or your choice)
|
| 99 |
+
- **License**: `apache-2.0` (or your preference)
|
| 100 |
+
- **Hardware**: `CPU basic` (sufficient for this model)
|
| 101 |
+
- **Visibility**: `Public`
|
| 102 |
+
|
| 103 |
+
### 3.2 Choose SDK
|
| 104 |
+
|
| 105 |
+
Select **"Gradio"** as your SDK.
|
| 106 |
+
|
| 107 |
+
### 3.3 Upload Files
|
| 108 |
+
|
| 109 |
+
Upload these files to your Space repository:
|
| 110 |
+
|
| 111 |
+
```
|
| 112 |
+
π wash-cfm-classifier-space/
|
| 113 |
+
βββ app.py # Your main application
|
| 114 |
+
βββ requirements.txt # Dependencies
|
| 115 |
+
βββ README.md # Space documentation
|
| 116 |
+
βββ .gitattributes # Optional: for large file handling
|
| 117 |
+
```
|
| 118 |
+
|
| 119 |
+
## Step 4: Space Configuration
|
| 120 |
+
|
| 121 |
+
### 4.1 Hardware Recommendations
|
| 122 |
+
|
| 123 |
+
For your model size (~500MB):
|
| 124 |
+
|
| 125 |
+
- **CPU Basic**: β
Sufficient (free tier)
|
| 126 |
+
- **CPU Upgrade**: β‘ Faster inference
|
| 127 |
+
- **GPU**: π Only if needed for larger models
|
| 128 |
+
|
| 129 |
+
### 4.2 Environment Variables (Optional)
|
| 130 |
+
|
| 131 |
+
In Space Settings β Environment, add:
|
| 132 |
+
|
| 133 |
+
```
|
| 134 |
+
HF_HOME=/tmp/.cache/huggingface
|
| 135 |
+
TRANSFORMERS_CACHE=/tmp/.cache/transformers
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
This ensures cache directories have sufficient space.
|
| 139 |
+
|
| 140 |
+
### 4.3 Build Logs
|
| 141 |
+
|
| 142 |
+
Monitor the **"Logs"** tab for:
|
| 143 |
+
- β
Successful dependency installation
|
| 144 |
+
- β
Model download progress
|
| 145 |
+
- β
Application startup
|
| 146 |
+
|
| 147 |
+
## Step 5: Testing and Validation
|
| 148 |
+
|
| 149 |
+
### 5.1 First Run
|
| 150 |
+
|
| 151 |
+
The first run will:
|
| 152 |
+
1. **Install dependencies** (~2-3 minutes)
|
| 153 |
+
2. **Download model** (~1-2 minutes, depending on connection)
|
| 154 |
+
3. **Start application** (~30 seconds)
|
| 155 |
+
|
| 156 |
+
**Expected timeline**: 5-7 minutes for first successful run.
|
| 157 |
+
|
| 158 |
+
### 5.2 Subsequent Runs
|
| 159 |
+
|
| 160 |
+
After caching:
|
| 161 |
+
- **Startup time**: ~10-15 seconds
|
| 162 |
+
- **Prediction time**: <1 second per request
|
| 163 |
+
|
| 164 |
+
### 5.3 Verification Checklist
|
| 165 |
+
|
| 166 |
+
- [ ] Space builds successfully (green β
in status)
|
| 167 |
+
- [ ] Model downloads without errors
|
| 168 |
+
- [ ] Web interface loads
|
| 169 |
+
- [ ] Sample predictions work
|
| 170 |
+
- [ ] Performance is acceptable
|
| 171 |
+
|
| 172 |
+
## Step 6: Optimization and Monitoring
|
| 173 |
+
|
| 174 |
+
### 6.1 Performance Monitoring
|
| 175 |
+
|
| 176 |
+
Monitor these metrics:
|
| 177 |
+
- **Build time**: First deployment duration
|
| 178 |
+
- **Download time**: Model download duration
|
| 179 |
+
- **Inference time**: Response latency
|
| 180 |
+
- **Memory usage**: RAM consumption
|
| 181 |
+
|
| 182 |
+
### 6.2 Common Issues and Solutions
|
| 183 |
+
|
| 184 |
+
#### Issue: "Model download timeout"
|
| 185 |
+
```bash
|
| 186 |
+
# Solution: Use faster hardware tier or optimize cache
|
| 187 |
+
HF_MODEL_CACHE_DIR = "/tmp/model_cache"
|
| 188 |
+
```
|
| 189 |
+
|
| 190 |
+
#### Issue: "Out of memory"
|
| 191 |
+
```bash
|
| 192 |
+
# Solution: Use smaller hardware or optimize model loading
|
| 193 |
+
device = torch.device("cpu") # Force CPU if GPU memory insufficient
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
+
#### Issue: "Repository not found"
|
| 197 |
+
```python
|
| 198 |
+
# Solution: Verify repository ID and visibility
|
| 199 |
+
HF_REPO_ID = "exact-username/exact-repo-name" # Case sensitive
|
| 200 |
+
```
|
| 201 |
+
|
| 202 |
+
### 6.3 Space Management
|
| 203 |
+
|
| 204 |
+
**Regular maintenance:**
|
| 205 |
+
- Monitor disk usage in cache directory
|
| 206 |
+
- Update model versions by changing repository revision
|
| 207 |
+
- Scale hardware based on usage patterns
|
| 208 |
+
|
| 209 |
+
**Version updates:**
|
| 210 |
+
- Update model in your Hub repository
|
| 211 |
+
- Space automatically uses latest version (or specify revision)
|
| 212 |
+
|
| 213 |
+
## Step 7: Production Considerations
|
| 214 |
+
|
| 215 |
+
### 7.1 Security
|
| 216 |
+
|
| 217 |
+
- β
Use public repositories for Spaces
|
| 218 |
+
- β
Validate model integrity
|
| 219 |
+
- β
Implement proper error handling
|
| 220 |
+
- β
Monitor for unusual access patterns
|
| 221 |
+
|
| 222 |
+
### 7.2 Reliability
|
| 223 |
+
|
| 224 |
+
- β
Implement retry logic for downloads
|
| 225 |
+
- β
Add fallback mechanisms
|
| 226 |
+
- β
Monitor network connectivity
|
| 227 |
+
- β
Set up alerts for failures
|
| 228 |
+
|
| 229 |
+
### 7.3 Scalability
|
| 230 |
+
|
| 231 |
+
- **Multiple Spaces**: Same model, different interfaces
|
| 232 |
+
- **Load Balancing**: Distribute across multiple hardware tiers
|
| 233 |
+
- **Caching Strategy**: Optimize for your usage patterns
|
| 234 |
+
|
| 235 |
+
## Troubleshooting Guide
|
| 236 |
+
|
| 237 |
+
### Build Failures
|
| 238 |
+
|
| 239 |
+
| Error | Solution |
|
| 240 |
+
|-------|----------|
|
| 241 |
+
| `pip install failed` | Check requirements.txt syntax |
|
| 242 |
+
| `torch install failed` | Verify Python version compatibility |
|
| 243 |
+
| `Memory limit exceeded` | Reduce model size or upgrade hardware |
|
| 244 |
+
|
| 245 |
+
### Runtime Failures
|
| 246 |
+
|
| 247 |
+
| Error | Solution |
|
| 248 |
+
|-------|----------|
|
| 249 |
+
| `Download interrupted` | Network issues - will auto-resume |
|
| 250 |
+
| `Model not found` | Verify repository ID and visibility |
|
| 251 |
+
| `CUDA out of memory` | Use CPU fallback or upgrade hardware |
|
| 252 |
+
|
| 253 |
+
### Performance Issues
|
| 254 |
+
|
| 255 |
+
| Issue | Solution |
|
| 256 |
+
|-------|----------|
|
| 257 |
+
| Slow first run | Normal - model download required |
|
| 258 |
+
| High memory usage | Consider hardware upgrade |
|
| 259 |
+
| Slow predictions | Optimize model or upgrade hardware |
|
| 260 |
+
|
| 261 |
+
## Success Metrics
|
| 262 |
+
|
| 263 |
+
Your deployment is successful when:
|
| 264 |
+
|
| 265 |
+
- β
Space builds without errors
|
| 266 |
+
- β
Model downloads and loads successfully
|
| 267 |
+
- β
Web interface is responsive
|
| 268 |
+
- β
Predictions are accurate and fast
|
| 269 |
+
- β
Resource usage is within limits
|
| 270 |
+
|
| 271 |
+
## Next Steps
|
| 272 |
+
|
| 273 |
+
1. **Monitor Performance**: Track usage and optimize as needed
|
| 274 |
+
2. **User Feedback**: Collect feedback and iterate
|
| 275 |
+
3. **Feature Updates**: Add new features or model improvements
|
| 276 |
+
4. **Scaling**: Consider multiple spaces or hardware upgrades
|
| 277 |
+
|
| 278 |
+
---
|
| 279 |
+
|
| 280 |
+
**π Congratulations!** Your WASH CFM Topic Classifier is now deployed to Hugging Face Spaces with Download-at-Runtime functionality, bypassing the 1GB storage limit while maintaining excellent performance.
|
| 281 |
+
|
| 282 |
+
For additional help, consult:
|
| 283 |
+
- [Hugging Face Spaces Documentation](https://huggingface.co/docs/spaces)
|
| 284 |
+
- [huggingface_hub Documentation](https://huggingface.co/docs/huggingface_hub)
|
| 285 |
+
- [Community Forum](https://discuss.huggingface.co/)
|
HF_DOWNLOAD_STRATEGY.md
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hugging Face Download-at-Runtime Strategy
|
| 2 |
+
|
| 3 |
+
## Overview
|
| 4 |
+
|
| 5 |
+
This document explains how to implement a "Download at Runtime" strategy for your WASH CFM Topic Classifier model using `huggingface_hub`. This approach allows you to bypass the 1GB storage limit in Hugging Face Spaces by hosting your model in a separate Hugging Face repository and downloading it at runtime.
|
| 6 |
+
|
| 7 |
+
## Why Use Download-at-Runtime?
|
| 8 |
+
|
| 9 |
+
1. **Space Constraint Resolution**: Hugging Face Spaces have a 1GB storage limit for uploaded files
|
| 10 |
+
2. **Model Reusability**: Host your model once and reuse it across multiple applications
|
| 11 |
+
3. **Version Control**: Leverage Hugging Face's built-in version control for model updates
|
| 12 |
+
4. **Efficient Caching**: Models are cached locally after first download
|
| 13 |
+
5. **Scalability**: Easy to update models without redeploying the entire Space
|
| 14 |
+
|
| 15 |
+
## Implementation Details
|
| 16 |
+
|
| 17 |
+
### Key Components
|
| 18 |
+
|
| 19 |
+
#### 1. Dependencies
|
| 20 |
+
The implementation requires `huggingface_hub>=0.16.0` added to your requirements:
|
| 21 |
+
|
| 22 |
+
```txt
|
| 23 |
+
huggingface_hub>=0.16.0
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
#### 2. Configuration
|
| 27 |
+
Configure your Hugging Face repository details at the top of `app.py`:
|
| 28 |
+
|
| 29 |
+
```python
|
| 30 |
+
# CONFIGURATION SECTION
|
| 31 |
+
HF_REPO_ID = "your-username/wash-cfm-classifier" # Your model repository
|
| 32 |
+
HF_MODEL_CACHE_DIR = "./model_cache" # Local cache directory
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
#### 3. Download Function
|
| 36 |
+
The core download logic uses `snapshot_download()` from `huggingface_hub`:
|
| 37 |
+
|
| 38 |
+
```python
|
| 39 |
+
from huggingface_hub import snapshot_download
|
| 40 |
+
|
| 41 |
+
model_path = snapshot_download(
|
| 42 |
+
repo_id=HF_REPO_ID,
|
| 43 |
+
cache_dir=HF_MODEL_CACHE_DIR,
|
| 44 |
+
resume_download=True, # Resume interrupted downloads
|
| 45 |
+
local_files_only=False # Force download if not cached
|
| 46 |
+
)
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
### Key Features
|
| 50 |
+
|
| 51 |
+
1. **Intelligent Caching**:
|
| 52 |
+
- Models are cached in `HF_MODEL_CACHE_DIR`
|
| 53 |
+
- Subsequent runs use cached versions
|
| 54 |
+
- No repeated downloads
|
| 55 |
+
|
| 56 |
+
2. **Resume Capability**:
|
| 57 |
+
- `resume_download=True` handles interrupted downloads
|
| 58 |
+
- Useful for large models and unstable connections
|
| 59 |
+
|
| 60 |
+
3. **Error Handling**:
|
| 61 |
+
- Comprehensive error messages for troubleshooting
|
| 62 |
+
- Network connectivity checks
|
| 63 |
+
- Repository access validation
|
| 64 |
+
|
| 65 |
+
4. **Performance Optimization**:
|
| 66 |
+
- LRU caching prevents model reloading
|
| 67 |
+
- Device-aware inference (CPU/GPU/MPS)
|
| 68 |
+
|
| 69 |
+
## Step-by-Step Implementation
|
| 70 |
+
|
| 71 |
+
### Step 1: Upload Your Model to Hugging Face
|
| 72 |
+
|
| 73 |
+
1. **Create a Hugging Face Account** (if you don't have one)
|
| 74 |
+
2. **Create a New Model Repository**:
|
| 75 |
+
- Go to https://huggingface.co/new
|
| 76 |
+
- Name it appropriately (e.g., `your-username/wash-cfm-classifier`)
|
| 77 |
+
- Make it **Public** (required for Spaces)
|
| 78 |
+
- Upload your model files:
|
| 79 |
+
- `model.safetensors`
|
| 80 |
+
- `config.json`
|
| 81 |
+
- `tokenizer.json`
|
| 82 |
+
- `tokenizer_config.json`
|
| 83 |
+
- `special_tokens_map.json`
|
| 84 |
+
|
| 85 |
+
### Step 2: Update Configuration
|
| 86 |
+
|
| 87 |
+
Edit the configuration section in `app.py`:
|
| 88 |
+
|
| 89 |
+
```python
|
| 90 |
+
HF_REPO_ID = "your-username/wash-cfm-classifier" # Replace with your actual repo
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
### Step 3: Install Dependencies
|
| 94 |
+
|
| 95 |
+
Add to your `requirements.txt`:
|
| 96 |
+
|
| 97 |
+
```txt
|
| 98 |
+
huggingface_hub>=0.16.0
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
### Step 4: Deploy to Hugging Face Space
|
| 102 |
+
|
| 103 |
+
1. **Create or update your Hugging Face Space**
|
| 104 |
+
2. **Upload your modified files** (app.py with download logic)
|
| 105 |
+
3. **The Space will automatically**:
|
| 106 |
+
- Install dependencies from requirements.txt
|
| 107 |
+
- Download the model on first run
|
| 108 |
+
- Cache it for subsequent runs
|
| 109 |
+
|
| 110 |
+
## How It Works
|
| 111 |
+
|
| 112 |
+
### First Run
|
| 113 |
+
```
|
| 114 |
+
1. User accesses the Space
|
| 115 |
+
2. app.py imports huggingface_hub
|
| 116 |
+
3. load_model() function calls snapshot_download()
|
| 117 |
+
4. Model downloads from Hugging Face Hub (~500MB)
|
| 118 |
+
5. Model loads into memory
|
| 119 |
+
6. First prediction takes longer (download + load time)
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
### Subsequent Runs
|
| 123 |
+
```
|
| 124 |
+
1. User accesses the Space
|
| 125 |
+
2. load_model() function checks cache
|
| 126 |
+
3. Model loads from local cache (~5-10 seconds)
|
| 127 |
+
4. Predictions are fast
|
| 128 |
+
```
|
| 129 |
+
|
| 130 |
+
## Benefits vs Local Storage
|
| 131 |
+
|
| 132 |
+
| Aspect | Local Storage | Download-at-Runtime |
|
| 133 |
+
|--------|---------------|---------------------|
|
| 134 |
+
| **Initial Load Time** | Instant | 30-60 seconds (first run) |
|
| 135 |
+
| **Subsequent Runs** | Instant | Fast (cached) |
|
| 136 |
+
| **Space Usage** | Counts toward 1GB limit | Minimal (just cache) |
|
| 137 |
+
| **Model Updates** | Manual reupload | Automatic from repo |
|
| 138 |
+
| **Scalability** | Limited by Space size | Unlimited |
|
| 139 |
+
|
| 140 |
+
## Troubleshooting
|
| 141 |
+
|
| 142 |
+
### Common Issues and Solutions
|
| 143 |
+
|
| 144 |
+
1. **Repository Not Found**
|
| 145 |
+
```
|
| 146 |
+
Error: Repository 'username/repo-name' not found
|
| 147 |
+
Solution: Verify repo ID and ensure repository is public
|
| 148 |
+
```
|
| 149 |
+
|
| 150 |
+
2. **Download Timeout**
|
| 151 |
+
```
|
| 152 |
+
Error: Download interrupted
|
| 153 |
+
Solution: The resume_download=True handles this automatically
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
3. **Authentication Issues**
|
| 157 |
+
```
|
| 158 |
+
Error: Access denied
|
| 159 |
+
Solution: Ensure repository is public or use access tokens
|
| 160 |
+
```
|
| 161 |
+
|
| 162 |
+
4. **Disk Space**
|
| 163 |
+
```
|
| 164 |
+
Error: No space left on device
|
| 165 |
+
Solution: Clean cache or use external storage
|
| 166 |
+
```
|
| 167 |
+
|
| 168 |
+
### Debug Commands
|
| 169 |
+
|
| 170 |
+
To test your setup locally:
|
| 171 |
+
|
| 172 |
+
```python
|
| 173 |
+
from huggingface_hub import snapshot_download
|
| 174 |
+
|
| 175 |
+
# Test download
|
| 176 |
+
path = snapshot_download(
|
| 177 |
+
repo_id="your-username/wash-cfm-classifier",
|
| 178 |
+
cache_dir="./test_cache"
|
| 179 |
+
)
|
| 180 |
+
print(f"Model downloaded to: {path}")
|
| 181 |
+
```
|
| 182 |
+
|
| 183 |
+
## Advanced Options
|
| 184 |
+
|
| 185 |
+
### 1. Progressive Loading
|
| 186 |
+
For very large models, consider loading components separately:
|
| 187 |
+
|
| 188 |
+
```python
|
| 189 |
+
from huggingface_hub import hf_hub_download
|
| 190 |
+
|
| 191 |
+
# Download individual files
|
| 192 |
+
config_path = hf_hub_download(
|
| 193 |
+
repo_id=HF_REPO_ID,
|
| 194 |
+
filename="config.json",
|
| 195 |
+
cache_dir=HF_MODEL_CACHE_DIR
|
| 196 |
+
)
|
| 197 |
+
```
|
| 198 |
+
|
| 199 |
+
### 2. Custom Cache Location
|
| 200 |
+
Use persistent storage for Hugging Face Spaces:
|
| 201 |
+
|
| 202 |
+
```python
|
| 203 |
+
# Use /tmp or mounted storage for better persistence
|
| 204 |
+
HF_MODEL_CACHE_DIR = "/tmp/model_cache"
|
| 205 |
+
```
|
| 206 |
+
|
| 207 |
+
### 3. Model Versioning
|
| 208 |
+
Pin specific model versions:
|
| 209 |
+
|
| 210 |
+
```python
|
| 211 |
+
from huggingface_hub import snapshot_download
|
| 212 |
+
|
| 213 |
+
model_path = snapshot_download(
|
| 214 |
+
repo_id=HF_REPO_ID,
|
| 215 |
+
revision="v1.0", # Specific version
|
| 216 |
+
cache_dir=HF_MODEL_CACHE_DIR
|
| 217 |
+
)
|
| 218 |
+
```
|
| 219 |
+
|
| 220 |
+
## Performance Considerations
|
| 221 |
+
|
| 222 |
+
### First Run Optimization
|
| 223 |
+
- **Download Time**: 30-60 seconds for ~500MB model
|
| 224 |
+
- **Load Time**: 10-15 seconds for model initialization
|
| 225 |
+
- **Total**: ~1-2 minutes for first prediction
|
| 226 |
+
|
| 227 |
+
### Cached Run Performance
|
| 228 |
+
- **Load Time**: 5-10 seconds (from cache)
|
| 229 |
+
- **Prediction**: <1 second per inference
|
| 230 |
+
|
| 231 |
+
### Memory Usage
|
| 232 |
+
- **Model Loading**: ~2-3GB RAM during inference
|
| 233 |
+
- **Cached Storage**: ~500MB disk space
|
| 234 |
+
- **Peak Usage**: Higher during initial download
|
| 235 |
+
|
| 236 |
+
## Best Practices
|
| 237 |
+
|
| 238 |
+
1. **Repository Setup**:
|
| 239 |
+
- Use clear, descriptive repository names
|
| 240 |
+
- Include model cards (README.md) with usage instructions
|
| 241 |
+
- Tag releases for version control
|
| 242 |
+
|
| 243 |
+
2. **Error Handling**:
|
| 244 |
+
- Implement graceful fallbacks
|
| 245 |
+
- Provide clear error messages to users
|
| 246 |
+
- Log download progress for debugging
|
| 247 |
+
|
| 248 |
+
3. **User Experience**:
|
| 249 |
+
- Show download progress indicators
|
| 250 |
+
- Cache models efficiently
|
| 251 |
+
- Handle network failures gracefully
|
| 252 |
+
|
| 253 |
+
4. **Security**:
|
| 254 |
+
- Use public repositories for Spaces
|
| 255 |
+
- Validate model integrity
|
| 256 |
+
- Implement proper access controls
|
| 257 |
+
|
| 258 |
+
## Conclusion
|
| 259 |
+
|
| 260 |
+
The Download-at-Runtime strategy successfully addresses the Hugging Face Spaces 1GB limit by:
|
| 261 |
+
|
| 262 |
+
β
**Eliminating storage constraints**
|
| 263 |
+
β
**Enabling model reuse across applications**
|
| 264 |
+
β
**Providing efficient caching mechanisms**
|
| 265 |
+
β
**Maintaining good performance after initial setup**
|
| 266 |
+
β
**Offering built-in version control**
|
| 267 |
+
|
| 268 |
+
This approach is ideal for production applications where model size exceeds Space limits but network connectivity is reliable.
|
| 269 |
+
|
| 270 |
+
---
|
| 271 |
+
|
| 272 |
+
*For questions or issues, refer to the [huggingface_hub documentation](https://huggingface.co/docs/huggingface_hub/index) or create an issue in your repository.*
|
Pipfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[[source]]
|
| 2 |
+
url = "https://pypi.org/simple"
|
| 3 |
+
verify_ssl = true
|
| 4 |
+
name = "pypi"
|
| 5 |
+
|
| 6 |
+
[packages]
|
| 7 |
+
torch = ">=2.0.0"
|
| 8 |
+
transformers = ">=4.30.0"
|
| 9 |
+
gradio = ">=4.0.0"
|
| 10 |
+
huggingface-hub = "*"
|
| 11 |
+
|
| 12 |
+
[dev-packages]
|
| 13 |
+
|
| 14 |
+
[requires]
|
| 15 |
+
python_version = "3.11"
|
| 16 |
+
python_full_version = "3.11.4"
|
Pipfile.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Cfm Topic Classifier
|
| 3 |
+
emoji: π»
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 6.2.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
short_description: ModernBERT encoder model fine-tuned on CFM topics
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# π§ WASH CFM Topic Classifier
|
| 14 |
+
|
| 15 |
+
A Gradio web application for classifying WASH (Water, Sanitation, and Hygiene) feedback into relevant topic categories using a fine-tuned ModernBERT model.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- **Topic Classification**: Automatically classifies WASH feedback into relevant topic categories
|
| 20 |
+
- **ModernBERT Integration**: Uses a fine-tuned ModernBERT-large model for accurate classification
|
| 21 |
+
- **Multi-Device Support**: Automatically detects and utilizes the best available device:
|
| 22 |
+
- Apple Silicon (MPS)
|
| 23 |
+
- NVIDIA GPU (CUDA)
|
| 24 |
+
- CPU fallback
|
| 25 |
+
- **Top-K Predictions**: Shows the top 2 most probable topics with confidence scores
|
| 26 |
+
- **Interactive Interface**: User-friendly Gradio interface with real-time classification
|
| 27 |
+
- **Input Validation**: Validates input and provides helpful error messages
|
| 28 |
+
|
| 29 |
+
## Installation
|
| 30 |
+
|
| 31 |
+
1. Clone or download this repository
|
| 32 |
+
2. Install the required dependencies:
|
| 33 |
+
|
| 34 |
+
```bash
|
| 35 |
+
pip install -r requirements.txt
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
3. Ensure the model files are available in the `./wash_cfm_classifier/` directory
|
| 39 |
+
|
| 40 |
+
## Usage
|
| 41 |
+
|
| 42 |
+
1. Run the application:
|
| 43 |
+
|
| 44 |
+
```bash
|
| 45 |
+
python app.py
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
2. Open your web browser and navigate to `http://localhost:7860`
|
| 49 |
+
|
| 50 |
+
3. Enter WASH feedback text in the input box (e.g., "The water pump in our area has been broken for 3 days...")
|
| 51 |
+
|
| 52 |
+
4. Click "Submit" to get topic predictions with confidence scores
|
| 53 |
+
|
| 54 |
+
5. Use the "Clear" button to reset the interface
|
| 55 |
+
|
| 56 |
+
## Requirements
|
| 57 |
+
|
| 58 |
+
- Python 3.7+
|
| 59 |
+
- torch>=2.0.0
|
| 60 |
+
- transformers>=4.30.0
|
| 61 |
+
- gradio>=4.0.0
|
| 62 |
+
|
| 63 |
+
## Technical Details
|
| 64 |
+
|
| 65 |
+
- **Model**: Fine-tuned ModernBERT-large for sequence classification
|
| 66 |
+
- **Framework**: Gradio for web interface
|
| 67 |
+
- **Device Support**: Automatic device detection (MPS/CUDA/CPU)
|
| 68 |
+
- **Caching**: LRU cache for model loading to improve performance
|
| 69 |
+
- **Output Format**: HTML-formatted results with confidence percentages
|
| 70 |
+
|
| 71 |
+
## Example Input/Output
|
| 72 |
+
|
| 73 |
+
**Input**: "The water pump in our area has been broken for 3 days and we need access to clean water"
|
| 74 |
+
|
| 75 |
+
**Output**:
|
| 76 |
+
1. **Water Supply** - Confidence: 95.2%
|
| 77 |
+
2. **Infrastructure** - Confidence: 87.1%
|
| 78 |
+
|
| 79 |
+
## Error Handling
|
| 80 |
+
|
| 81 |
+
- Validates empty or whitespace-only input
|
| 82 |
+
- Handles missing model files gracefully
|
| 83 |
+
- Provides detailed error messages for troubleshooting
|
| 84 |
+
|
| 85 |
+
## Configuration
|
| 86 |
+
|
| 87 |
+
- **Server Address**: `0.0.0.0` (all interfaces)
|
| 88 |
+
- **Port**: `7860`
|
| 89 |
+
- **Model Path**: `./wash_cfm_classifier/`
|
| 90 |
+
- **Top-K Predictions**: `2`
|
| 91 |
+
|
| 92 |
+
## License
|
| 93 |
+
|
| 94 |
+
UNICEF WASH Cluster CFM System
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
|
| 98 |
+
*Powered by ModernBERT-large | UNICEF WASH Cluster CFM System*
|
app.py
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
WASH CFM Topic Classification Gradio Application
|
| 3 |
+
|
| 4 |
+
This application provides a user interface for classifying WASH (Water, Sanitation,
|
| 5 |
+
and Hygiene) feedback using a fine-tuned ModernBERT model.
|
| 6 |
+
|
| 7 |
+
This is a Gradio implementation with identical functionality to wash_cfm_app.py.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import torch
|
| 12 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 13 |
+
from huggingface_hub import snapshot_download, hf_hub_download
|
| 14 |
+
import functools
|
| 15 |
+
import os
|
| 16 |
+
import tempfile
|
| 17 |
+
|
| 18 |
+
# ================================
|
| 19 |
+
# CONFIGURATION SECTION
|
| 20 |
+
# ================================
|
| 21 |
+
# Replace these with your actual Hugging Face repository details
|
| 22 |
+
HF_REPO_ID = "ibagur/wash_cfm_classifier" # Your Hugging Face repository
|
| 23 |
+
HF_MODEL_CACHE_DIR = "/tmp/model_cache" # Cache directory (using /tmp for better Space compatibility)
|
| 24 |
+
# ================================
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@functools.lru_cache(maxsize=1)
|
| 28 |
+
def load_model():
|
| 29 |
+
"""
|
| 30 |
+
Load the pre-trained WASH CFM classifier model from Hugging Face Hub and create a pipeline.
|
| 31 |
+
Downloads the model at runtime if not already cached locally.
|
| 32 |
+
Uses LRU cache to avoid reloading on every interaction.
|
| 33 |
+
|
| 34 |
+
Returns:
|
| 35 |
+
pipeline: Hugging Face transformers pipeline for text classification
|
| 36 |
+
"""
|
| 37 |
+
print(f"Downloading model from Hugging Face Hub: {HF_REPO_ID}")
|
| 38 |
+
print("This may take a few minutes on first run...")
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
# Download the entire model repository to cache
|
| 42 |
+
# This is more efficient than downloading individual files
|
| 43 |
+
model_path = snapshot_download(
|
| 44 |
+
repo_id=HF_REPO_ID,
|
| 45 |
+
cache_dir=HF_MODEL_CACHE_DIR,
|
| 46 |
+
resume_download=True, # Resume if download was interrupted
|
| 47 |
+
local_files_only=False # Force download if not in cache
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
print(f"Model downloaded successfully to: {model_path}")
|
| 51 |
+
|
| 52 |
+
# Load tokenizer and model from the downloaded path
|
| 53 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 54 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
| 55 |
+
|
| 56 |
+
# Set to evaluation mode
|
| 57 |
+
model.eval()
|
| 58 |
+
|
| 59 |
+
# Check what device we're using (including Apple Silicon MPS support)
|
| 60 |
+
if torch.backends.mps.is_available():
|
| 61 |
+
device = torch.device("mps") # Apple Silicon
|
| 62 |
+
elif torch.cuda.is_available():
|
| 63 |
+
device = torch.device("cuda") # NVIDIA GPU
|
| 64 |
+
else:
|
| 65 |
+
device = torch.device("cpu") # CPU fallback
|
| 66 |
+
|
| 67 |
+
print(f"Using device: {device}")
|
| 68 |
+
|
| 69 |
+
model.to(device)
|
| 70 |
+
|
| 71 |
+
# Create pipeline for easy inference
|
| 72 |
+
classifier = pipeline(
|
| 73 |
+
'text-classification',
|
| 74 |
+
model=model,
|
| 75 |
+
tokenizer=tokenizer,
|
| 76 |
+
device=device
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
return classifier
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
print(f"Error downloading model: {str(e)}")
|
| 83 |
+
print("\nTroubleshooting steps:")
|
| 84 |
+
print("1. Check that your repository ID is correct")
|
| 85 |
+
print("2. Ensure the repository is public or you have proper access")
|
| 86 |
+
print("3. Check your internet connection")
|
| 87 |
+
print("4. Verify the repository exists on Hugging Face Hub")
|
| 88 |
+
raise
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def predict_topics(text, classifier, top_k=2):
|
| 92 |
+
"""
|
| 93 |
+
Predict the top-k most probable topics for the given text using the pipeline.
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
text (str): Input feedback text
|
| 97 |
+
classifier: Hugging Face transformers pipeline
|
| 98 |
+
top_k (int): Number of top predictions to return
|
| 99 |
+
|
| 100 |
+
Returns:
|
| 101 |
+
list: List of tuples (topic_name, probability)
|
| 102 |
+
"""
|
| 103 |
+
# Use pipeline for prediction - it handles all the complexity internally
|
| 104 |
+
predictions = classifier(text, top_k=top_k)
|
| 105 |
+
|
| 106 |
+
# Convert pipeline results to our format
|
| 107 |
+
results = [(pred['label'], pred['score']) for pred in predictions]
|
| 108 |
+
|
| 109 |
+
return results
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def classify_feedback(text):
|
| 113 |
+
"""
|
| 114 |
+
Main classification handler for Gradio interface.
|
| 115 |
+
|
| 116 |
+
Args:
|
| 117 |
+
text (str): Input WASH feedback text
|
| 118 |
+
|
| 119 |
+
Returns:
|
| 120 |
+
str: HTML formatted prediction results
|
| 121 |
+
"""
|
| 122 |
+
# Validate input
|
| 123 |
+
if not text or not text.strip():
|
| 124 |
+
return """
|
| 125 |
+
<div style="
|
| 126 |
+
background-color: #fff3cd;
|
| 127 |
+
color: #856404;
|
| 128 |
+
padding: 15px;
|
| 129 |
+
border-radius: 8px;
|
| 130 |
+
border-left: 4px solid #ffc107;
|
| 131 |
+
font-weight: 500;
|
| 132 |
+
">
|
| 133 |
+
β οΈ Please enter some feedback text.
|
| 134 |
+
</div>
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
try:
|
| 138 |
+
# Load classifier pipeline (cached)
|
| 139 |
+
classifier = load_model()
|
| 140 |
+
|
| 141 |
+
# Get predictions
|
| 142 |
+
predictions = predict_topics(
|
| 143 |
+
text,
|
| 144 |
+
classifier,
|
| 145 |
+
top_k=2
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
# Format results as HTML
|
| 149 |
+
html_output = """
|
| 150 |
+
<div style="margin-top: 10px;">
|
| 151 |
+
<h3 style="color: #333; margin-bottom: 15px;">π Predicted Topics</h3>
|
| 152 |
+
"""
|
| 153 |
+
|
| 154 |
+
for i, (topic, probability) in enumerate(predictions, 1):
|
| 155 |
+
# Add prediction box with fixed color
|
| 156 |
+
html_output += f"""
|
| 157 |
+
<div style="
|
| 158 |
+
background-color: #009999;
|
| 159 |
+
color: #ffffff;
|
| 160 |
+
padding: 15px;
|
| 161 |
+
border-radius: 8px;
|
| 162 |
+
margin-bottom: 10px;
|
| 163 |
+
font-weight: 500;
|
| 164 |
+
">
|
| 165 |
+
<div style="font-size: 16px; margin-bottom: 5px;">
|
| 166 |
+
{i}. {topic}
|
| 167 |
+
</div>
|
| 168 |
+
<div style="font-size: 14px; opacity: 0.9;">
|
| 169 |
+
Confidence: {probability:.1%}
|
| 170 |
+
</div>
|
| 171 |
+
</div>
|
| 172 |
+
"""
|
| 173 |
+
|
| 174 |
+
html_output += "</div>"
|
| 175 |
+
return html_output
|
| 176 |
+
|
| 177 |
+
except FileNotFoundError:
|
| 178 |
+
return """
|
| 179 |
+
<div style="
|
| 180 |
+
background-color: #f8d7da;
|
| 181 |
+
color: #721c24;
|
| 182 |
+
padding: 15px;
|
| 183 |
+
border-radius: 8px;
|
| 184 |
+
border-left: 4px solid #dc3545;
|
| 185 |
+
">
|
| 186 |
+
<strong>β Error loading model</strong><br>
|
| 187 |
+
Could not download or access the model from Hugging Face Hub.<br>
|
| 188 |
+
Please check your internet connection and repository configuration.
|
| 189 |
+
</div>
|
| 190 |
+
"""
|
| 191 |
+
except Exception as e:
|
| 192 |
+
return f"""
|
| 193 |
+
<div style="
|
| 194 |
+
background-color: #f8d7da;
|
| 195 |
+
color: #721c24;
|
| 196 |
+
padding: 15px;
|
| 197 |
+
border-radius: 8px;
|
| 198 |
+
border-left: 4px solid #dc3545;
|
| 199 |
+
">
|
| 200 |
+
<strong>β Error during prediction:</strong><br>
|
| 201 |
+
{str(e)}
|
| 202 |
+
</div>
|
| 203 |
+
"""
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
def clear_inputs():
|
| 207 |
+
"""
|
| 208 |
+
Clear both input and output fields.
|
| 209 |
+
|
| 210 |
+
Returns:
|
| 211 |
+
tuple: Empty strings for textbox and output
|
| 212 |
+
"""
|
| 213 |
+
return "", ""
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def create_interface():
|
| 217 |
+
"""
|
| 218 |
+
Create and configure the Gradio interface.
|
| 219 |
+
|
| 220 |
+
Returns:
|
| 221 |
+
gr.Blocks: Configured Gradio interface
|
| 222 |
+
"""
|
| 223 |
+
with gr.Blocks(
|
| 224 |
+
title="WASH CFM Topic Classifier",
|
| 225 |
+
theme=gr.themes.Soft()
|
| 226 |
+
) as demo:
|
| 227 |
+
# Header
|
| 228 |
+
gr.Markdown("""
|
| 229 |
+
# π§ WASH CFM Topic Classifier
|
| 230 |
+
|
| 231 |
+
This application classifies WASH (Water, Sanitation, and Hygiene) feedback
|
| 232 |
+
into relevant topic categories using a fine-tuned ModernBERT model.
|
| 233 |
+
|
| 234 |
+
**Enter your feedback below and click Submit.**
|
| 235 |
+
""")
|
| 236 |
+
|
| 237 |
+
# Input section
|
| 238 |
+
input_textbox = gr.Textbox(
|
| 239 |
+
label="Enter WASH feedback:",
|
| 240 |
+
placeholder="Example: The water pump in our area has been broken for 3 days...",
|
| 241 |
+
lines=6,
|
| 242 |
+
interactive=True
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
# Button row
|
| 246 |
+
with gr.Row():
|
| 247 |
+
submit_btn = gr.Button("β Submit", variant="primary", scale=2)
|
| 248 |
+
clear_btn = gr.Button("ποΈ Clear", scale=1)
|
| 249 |
+
|
| 250 |
+
# Output section
|
| 251 |
+
output_html = gr.HTML(label="Results")
|
| 252 |
+
|
| 253 |
+
# Footer
|
| 254 |
+
gr.Markdown("""
|
| 255 |
+
---
|
| 256 |
+
<div style="text-align: center; color: #666; font-size: 12px;">
|
| 257 |
+
Powered by ModernBERT-large | UNICEF WASH Cluster CFM System
|
| 258 |
+
</div>
|
| 259 |
+
""")
|
| 260 |
+
|
| 261 |
+
# Event handlers
|
| 262 |
+
submit_btn.click(
|
| 263 |
+
fn=classify_feedback,
|
| 264 |
+
inputs=input_textbox,
|
| 265 |
+
outputs=output_html
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
input_textbox.submit(
|
| 269 |
+
fn=classify_feedback,
|
| 270 |
+
inputs=input_textbox,
|
| 271 |
+
outputs=output_html
|
| 272 |
+
)
|
| 273 |
+
|
| 274 |
+
clear_btn.click(
|
| 275 |
+
fn=clear_inputs,
|
| 276 |
+
inputs=None,
|
| 277 |
+
outputs=[input_textbox, output_html]
|
| 278 |
+
)
|
| 279 |
+
|
| 280 |
+
return demo
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
def main():
|
| 284 |
+
"""
|
| 285 |
+
Main function to launch the Gradio application.
|
| 286 |
+
"""
|
| 287 |
+
demo = create_interface()
|
| 288 |
+
|
| 289 |
+
demo.launch(
|
| 290 |
+
server_name="0.0.0.0",
|
| 291 |
+
server_port=7860,
|
| 292 |
+
share=False
|
| 293 |
+
)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
if __name__ == "__main__":
|
| 297 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# WASH CFM Topic Classification Gradio Application Dependencies
|
| 2 |
+
|
| 3 |
+
# Core ML and NLP libraries
|
| 4 |
+
torch>=2.0.0
|
| 5 |
+
transformers>=4.30.0
|
| 6 |
+
huggingface_hub>=0.16.0
|
| 7 |
+
|
| 8 |
+
# Web UI framework
|
| 9 |
+
gradio>=4.0.0
|