Spaces:
Runtime error
Runtime error
π Add comprehensive Hugging Face Spaces deployment guide
Browse files- Step-by-step deployment instructions
- Hardware recommendations (CPU vs GPU)
- API usage examples
- Troubleshooting guide
- Customization options
- Success indicators
Updated .gitignore to include important documentation files.
π€ Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
- .gitignore +3 -0
- HUGGINGFACE_DEPLOYMENT.md +185 -0
.gitignore
CHANGED
|
@@ -147,6 +147,9 @@ docs/
|
|
| 147 |
doc/
|
| 148 |
*.md
|
| 149 |
!README.md
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
|
| 152 |
# Node modules and web dependencies (if any)
|
|
|
|
| 147 |
doc/
|
| 148 |
*.md
|
| 149 |
!README.md
|
| 150 |
+
!HUGGINGFACE_DEPLOYMENT.md
|
| 151 |
+
!DEPLOYMENT.md
|
| 152 |
+
!FINE_TUNING_STATUS.md
|
| 153 |
|
| 154 |
|
| 155 |
# Node modules and web dependencies (if any)
|
HUGGINGFACE_DEPLOYMENT.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π Hugging Face Spaces Deployment Guide
|
| 2 |
+
|
| 3 |
+
## β
**Step 1: Repository is Ready!**
|
| 4 |
+
|
| 5 |
+
Your code has been successfully pushed to GitHub with all the modular architecture and automatic fine-tuning features.
|
| 6 |
+
|
| 7 |
+
## π **Step 2: Create Hugging Face Space**
|
| 8 |
+
|
| 9 |
+
### **Option A: Web Interface (Recommended)**
|
| 10 |
+
1. Go to [https://huggingface.co/new-space](https://huggingface.co/new-space)
|
| 11 |
+
2. **Space Settings:**
|
| 12 |
+
- **Name**: `vietnamese-sentiment-analysis` (or your choice)
|
| 13 |
+
- **License**: MIT (or your preferred license)
|
| 14 |
+
- **Hardware**: CPU Basic (free) or GPU T4 (for faster fine-tuning)
|
| 15 |
+
- **Visibility**: Public (free) or Private
|
| 16 |
+
- **SDK**: Gradio
|
| 17 |
+
|
| 18 |
+
### **Option B: Using Git**
|
| 19 |
+
```bash
|
| 20 |
+
# Clone your Hugging Face Space (replace with your space URL)
|
| 21 |
+
git clone https://huggingface.co/spaces/your-username/your-space-name
|
| 22 |
+
cd your-space-name
|
| 23 |
+
|
| 24 |
+
# Add your repository as remote
|
| 25 |
+
git remote add upstream https://github.com/your-username/SentimentAnalysis.git
|
| 26 |
+
git pull upstream main
|
| 27 |
+
git push origin main
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
## π **Step 3: Files to Upload**
|
| 31 |
+
|
| 32 |
+
Make sure these files are in your Space:
|
| 33 |
+
|
| 34 |
+
```
|
| 35 |
+
SentimentAnalysis/
|
| 36 |
+
βββ app.py # β
Main application
|
| 37 |
+
βββ requirements.txt # β
Dependencies
|
| 38 |
+
βββ py/
|
| 39 |
+
β βββ __init__.py # β
Package
|
| 40 |
+
β βββ api_controller.py # β
API controller
|
| 41 |
+
β βββ fine_tune_sentiment.py # β
Fine-tuning script
|
| 42 |
+
β βββ pages/ # β
UI pages
|
| 43 |
+
β βββ __init__.py
|
| 44 |
+
β βββ single_analysis.py
|
| 45 |
+
β βββ batch_analysis.py
|
| 46 |
+
β βββ model_info.py
|
| 47 |
+
β βββ api_endpoints.py
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
## βοΈ **Step 4: Space Configuration**
|
| 51 |
+
|
| 52 |
+
### **Requirements.txt Check:**
|
| 53 |
+
Ensure your `requirements.txt` includes:
|
| 54 |
+
```
|
| 55 |
+
torch>=2.0.0
|
| 56 |
+
transformers>=4.21.0
|
| 57 |
+
datasets>=2.0.0
|
| 58 |
+
gradio>=4.44.1
|
| 59 |
+
fastapi>=0.104.0
|
| 60 |
+
uvicorn>=0.24.0
|
| 61 |
+
pydantic>=2.5.0
|
| 62 |
+
psutil>=5.9.0
|
| 63 |
+
accelerate>=0.21.0
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
### **Hardware Selection:**
|
| 67 |
+
- **CPU Basic**: Free, but fine-tuning will be slow (30-60 minutes)
|
| 68 |
+
- **GPU T4**: Paid, but fine-tuning will be fast (5-10 minutes)
|
| 69 |
+
- **GPU A10G**: More expensive, best performance
|
| 70 |
+
|
| 71 |
+
## π **Step 5: Deployment Process**
|
| 72 |
+
|
| 73 |
+
### **What Happens Automatically:**
|
| 74 |
+
1. **Environment Detection**: App detects it's running on Hugging Face Spaces
|
| 75 |
+
2. **Model Loading**: Tries to load fine-tuned model, runs fine-tuning if needed
|
| 76 |
+
3. **API Server**: Starts REST API on port 7861 automatically
|
| 77 |
+
4. **Gradio Interface**: Launches with all 4 tabs
|
| 78 |
+
|
| 79 |
+
### **Expected Timeline:**
|
| 80 |
+
- **Initial Build**: 2-5 minutes (installing dependencies)
|
| 81 |
+
- **First Run**: 10-30 minutes (fine-tuning, depends on hardware)
|
| 82 |
+
- **Subsequent Runs**: 1-2 minutes (model already created)
|
| 83 |
+
|
| 84 |
+
## π± **Step 6: Testing Your Space**
|
| 85 |
+
|
| 86 |
+
Once deployed, your Space will have:
|
| 87 |
+
|
| 88 |
+
### **Main Interface** (Main URL):
|
| 89 |
+
- π **Single Text Analysis** - Analyze individual Vietnamese texts
|
| 90 |
+
- π **Batch Analysis** - Process multiple texts
|
| 91 |
+
- βΉοΈ **Model Information** - View model details and memory usage
|
| 92 |
+
- π **REST API Endpoints** - API documentation and examples
|
| 93 |
+
|
| 94 |
+
### **REST API** (Your Space URL + :7861):
|
| 95 |
+
- **Interactive Docs**: `https://your-space.hf.space:7861/docs`
|
| 96 |
+
- **Health Check**: `GET /health`
|
| 97 |
+
- **Analysis**: `POST /analyze`
|
| 98 |
+
- **Batch**: `POST /analyze/batch`
|
| 99 |
+
|
| 100 |
+
## π― **Step 7: API Usage Examples**
|
| 101 |
+
|
| 102 |
+
### **Python Example:**
|
| 103 |
+
```python
|
| 104 |
+
import requests
|
| 105 |
+
|
| 106 |
+
response = requests.post(
|
| 107 |
+
"https://your-space.hf.space:7861/analyze",
|
| 108 |
+
json={"text": "GiαΊ£ng viΓͺn dαΊ‘y rαΊ₯t hay vΓ tΓ’m huyαΊΏt."}
|
| 109 |
+
)
|
| 110 |
+
result = response.json()
|
| 111 |
+
print(f"Sentiment: {result['sentiment']}")
|
| 112 |
+
print(f"Confidence: {result['confidence']:.2%}")
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
### **cURL Example:**
|
| 116 |
+
```bash
|
| 117 |
+
curl -X POST "https://your-space.hf.space:7861/analyze" \
|
| 118 |
+
-H "Content-Type: application/json" \
|
| 119 |
+
-d '{"text": "GiαΊ£ng viΓͺn dαΊ‘y rαΊ₯t hay vΓ tΓ’m huyαΊΏt."}'
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
## β οΈ **Step 8: Troubleshooting**
|
| 123 |
+
|
| 124 |
+
### **Common Issues:**
|
| 125 |
+
|
| 126 |
+
1. **Build Fails:**
|
| 127 |
+
- Check requirements.txt has all dependencies
|
| 128 |
+
- Ensure no syntax errors in Python files
|
| 129 |
+
- Check Space logs for specific error messages
|
| 130 |
+
|
| 131 |
+
2. **Fine-Tuning Takes Too Long:**
|
| 132 |
+
- Consider upgrading to GPU hardware
|
| 133 |
+
- The process will complete eventually on CPU
|
| 134 |
+
|
| 135 |
+
3. **API Not Working:**
|
| 136 |
+
- API runs on port 7861 (different from main Gradio port)
|
| 137 |
+
- Check if CORS is enabled (included in our setup)
|
| 138 |
+
|
| 139 |
+
4. **Memory Issues:**
|
| 140 |
+
- The app includes automatic memory cleanup
|
| 141 |
+
- Batch size is limited to 10 texts
|
| 142 |
+
- GPU spaces have more memory available
|
| 143 |
+
|
| 144 |
+
## π§ **Step 9: Advanced Configuration**
|
| 145 |
+
|
| 146 |
+
### **Custom Fine-Tuned Model:**
|
| 147 |
+
If you have your own fine-tuned model on Hugging Face Hub:
|
| 148 |
+
1. Update line 32 in `app.py`:
|
| 149 |
+
```python
|
| 150 |
+
self.finetuned_model = "your-username/your-model-name"
|
| 151 |
+
```
|
| 152 |
+
|
| 153 |
+
### **Custom Fine-Tuning Parameters:**
|
| 154 |
+
Modify `py/fine_tune_sentiment.py` to adjust:
|
| 155 |
+
- Learning rate
|
| 156 |
+
- Number of epochs
|
| 157 |
+
- Batch size
|
| 158 |
+
- Dataset
|
| 159 |
+
|
| 160 |
+
## π **Step 10: Monitoring**
|
| 161 |
+
|
| 162 |
+
### **Space Metrics:**
|
| 163 |
+
- Usage statistics in your Space dashboard
|
| 164 |
+
- API endpoint performance
|
| 165 |
+
- Model loading time
|
| 166 |
+
- Memory usage (shown in Model Info tab)
|
| 167 |
+
|
| 168 |
+
## π **Success Indicators:**
|
| 169 |
+
|
| 170 |
+
Your deployment is successful when you see:
|
| 171 |
+
- β
Gradio interface loads with all 4 tabs
|
| 172 |
+
- β
Model information shows "vietnamese_sentiment_finetuned"
|
| 173 |
+
- β
API documentation is accessible at `/docs`
|
| 174 |
+
- β
Test analysis returns correct sentiment predictions
|
| 175 |
+
- β
Health check endpoint returns status "healthy"
|
| 176 |
+
|
| 177 |
+
## π **Support**
|
| 178 |
+
|
| 179 |
+
If you encounter issues:
|
| 180 |
+
1. Check the Space logs for error messages
|
| 181 |
+
2. Verify all files are uploaded correctly
|
| 182 |
+
3. Test locally with `python app.py` first
|
| 183 |
+
4. Review the fine-tuning process in the logs
|
| 184 |
+
|
| 185 |
+
Your modular Vietnamese Sentiment Analysis app is now ready for production deployment! π
|