File size: 6,942 Bytes
5fd4bb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# Hugging Face Spaces Deployment Guide

This guide walks you through deploying the RAG system to Hugging Face Spaces for free, permanent hosting.

## Prerequisites

- Hugging Face account ([sign up here](https://huggingface.co/join))
- Git installed on your local machine
- Code migrated to use HF Inference API (already done βœ…)

## Step 1: Test Locally (Recommended)

Before deploying, test the application with HF Inference API locally:

### 1.1 Get Your HF Token

1. Go to [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
2. Click "New token"
3. Name it (e.g., "RAG System")
4. Select `read` permissions
5. Click "Generate"
6. Copy the token (starts with `hf_`)

### 1.2 Set Environment Variable

```bash
# Linux/Mac
export HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

# Windows (PowerShell)
$env:HF_TOKEN="hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
```

### 1.3 Install Dependencies

```bash
cd /Users/v.hirenko/Desktop/DevHubVault/my-ai-projects/rag-python-rag
source venv/bin/activate  # or venv\Scripts\activate on Windows
pip install -r requirements.txt
```

### 1.4 Run Tests

```bash
python test_hf_integration.py
```

If all tests pass, proceed to deployment!

### 1.5 Test the Application

```bash
python app.py
```

Open http://localhost:7860 and test with a few questions.

## Step 2: Create Hugging Face Space

### 2.1 Create New Space

1. Go to [https://huggingface.co/new-space](https://huggingface.co/new-space)
2. Fill in the details:
   - **Owner**: Your username (e.g., `monsara`)
   - **Space name**: `rag-python-rag`
   - **License**: MIT
   - **Select the Space SDK**: Gradio
   - **Space hardware**: CPU basic (free)
   - **Space visibility**: Public (or Private if you prefer)
3. Click "Create Space"

### 2.2 Note Your Space URL

Your Space will be available at:
```
https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag
```

## Step 3: Configure Space Secrets

### 3.1 Add HF_TOKEN Secret

1. Go to your Space page
2. Click on "Settings" (gear icon)
3. Scroll down to "Repository secrets"
4. Click "Add a secret"
5. Fill in:
   - **Name**: `HF_TOKEN`
   - **Value**: Your HF token from Step 1.1
6. Click "Add"

⚠️ **Important**: The Space will NOT work without this secret!

## Step 4: Push Code to Space

You have two options:

### Option A: Direct Git Push (Recommended)

```bash
cd /Users/v.hirenko/Desktop/DevHubVault/my-ai-projects/rag-python-rag

# Add HF Space as remote
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag
# Replace YOUR_USERNAME with your actual HF username

# Push to HF Space
git push hf main
```

If prompted for credentials:
- **Username**: Your HF username
- **Password**: Your HF token (the same one you created)

### Option B: Link GitHub Repository

1. In your Space Settings
2. Find "Linked repositories"
3. Click "Link a GitHub repository"
4. Select `monsara/rag-python-rag`
5. The Space will automatically sync with your GitHub repo

## Step 5: Verify Deployment

### 5.1 Check Build Logs

1. Go to your Space page
2. Click on "Logs" tab
3. Watch the build process
4. Look for:
   ```
   βœ… HF_TOKEN validated successfully
   βœ… RAG pipeline setup complete!
   Running on local URL:  http://0.0.0.0:7860
   ```

### 5.2 Test the Space

1. Once "Running" status appears
2. Click on the Space URL
3. Try example questions
4. Verify answers are generated correctly

## Step 6: Update README (Optional)

Replace the Space README with the HF-specific one:

```bash
cd /Users/v.hirenko/Desktop/DevHubVault/my-ai-projects/rag-python-rag

# Backup current README
mv README.md README_LOCAL.md

# Use HF README
cp README_HF.md README.md

# Commit and push
git add README.md
git commit -m "Update README for HF Spaces"
git push hf main
```

## Troubleshooting

### Issue: "HF_TOKEN not found"

**Solution**: Make sure you added `HF_TOKEN` to Space secrets (Step 3.1)

### Issue: "Model not found" or "Rate limit exceeded"

**Solution**: 
- Check if you're using the correct model name in `config.py`
- Free tier has rate limits (~1000 requests/hour)
- Consider upgrading to HF Pro ($9/month)

### Issue: "Build failed"

**Solution**:
1. Check the build logs for specific errors
2. Verify `requirements.txt` has all dependencies
3. Make sure Python version is compatible (3.9+)

### Issue: "Application crashes on startup"

**Solution**:
1. Check if `app.py` is set as the entry point in Space settings
2. Verify all imports are correct
3. Check logs for Python errors

### Issue: Slow responses

**Solution**:
- Free tier uses shared infrastructure
- Upgrade to better hardware (paid)
- Or optimize chunk size and retrieval count

## Monitoring

### View Logs

```bash
# Real-time logs
# Go to Space page β†’ Logs tab
```

### Check Usage

1. Go to your HF profile
2. Click on "Usage & billing"
3. View API usage statistics

## Updating the Space

### Update Code

```bash
cd /Users/v.hirenko/Desktop/DevHubVault/my-ai-projects/rag-python-rag

# Make your changes
git add .
git commit -m "Your update message"
git push hf main
```

The Space will automatically rebuild.

### Update Dependencies

Edit `requirements.txt`, then:

```bash
git add requirements.txt
git commit -m "Update dependencies"
git push hf main
```

### Update Configuration

Edit `config.py`, then push changes as above.

## Cost Considerations

### Free Tier (Current Setup)

- βœ… **Cost**: $0/month
- βœ… **Hosting**: Unlimited
- ⚠️ **Rate limits**: ~1000 requests/hour
- ⚠️ **Performance**: Shared CPU
- ⚠️ **Tokens**: 1024 max per response

### Upgrade Options

**HF Pro ($9/month)**:
- Higher rate limits
- Faster inference
- Priority support
- Better hardware options

**Dedicated Hardware**:
- CPU Upgrade: $0.03/hour (~$22/month)
- GPU T4: $0.60/hour (~$432/month)
- GPU A10G: $1.05/hour (~$756/month)

## Security Best Practices

1. **Never commit HF_TOKEN** to Git
2. **Use Space secrets** for all sensitive data
3. **Rotate tokens** periodically
4. **Monitor usage** for unexpected spikes
5. **Set rate limits** in your application

## Next Steps

After successful deployment:

1. βœ… Test thoroughly with various questions
2. βœ… Share the Space URL with users
3. βœ… Monitor logs for errors
4. βœ… Gather user feedback
5. βœ… Iterate and improve

## Support

If you need help:

1. Check [HF Spaces documentation](https://huggingface.co/docs/hub/spaces)
2. Visit [HF Community forums](https://discuss.huggingface.co/)
3. Open an issue on [GitHub](https://github.com/monsara/rag-python-rag/issues)

---

## Quick Reference

**Space URL**: `https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag`

**Settings**: `https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag/settings`

**Logs**: `https://huggingface.co/spaces/YOUR_USERNAME/rag-python-rag/logs`

**Push command**:
```bash
git push hf main
```

**Update secret**: Space Settings β†’ Repository secrets β†’ Edit

---

Good luck with your deployment! πŸš€