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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
---
|
| 10 |
|
| 11 |
-
|
|
|
|
| 1 |
+
# β
Final Deployment Checklist
|
| 2 |
+
|
| 3 |
+
## π¦ Your Complete File Structure
|
| 4 |
+
|
| 5 |
+
```
|
| 6 |
+
your-hf-space/
|
| 7 |
+
βββ .gitignore β Create this
|
| 8 |
+
βββ .dockerignore β Create this
|
| 9 |
+
βββ Dockerfile β Use the one I provided
|
| 10 |
+
βββ requirements.txt β Use the one I provided
|
| 11 |
+
βββ main.py β Use the one I provided β
|
| 12 |
+
βββ split.py β Your existing file
|
| 13 |
+
βββ query_service.py β Your existing file
|
| 14 |
+
βββ README.md β Optional
|
| 15 |
+
```
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## π Files to Create/Copy
|
| 20 |
+
|
| 21 |
+
### 1. `.gitignore`
|
| 22 |
+
```gitignore
|
| 23 |
+
__pycache__/
|
| 24 |
+
*.py[cod]
|
| 25 |
+
*$py.class
|
| 26 |
+
*.so
|
| 27 |
+
.Python
|
| 28 |
+
env/
|
| 29 |
+
venv/
|
| 30 |
+
*.egg-info/
|
| 31 |
+
dist/
|
| 32 |
+
build/
|
| 33 |
+
*.log
|
| 34 |
+
.DS_Store
|
| 35 |
+
vectorstore/
|
| 36 |
+
docstore/
|
| 37 |
+
docs/
|
| 38 |
+
*.pdf
|
| 39 |
+
.venv/
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
### 2. `.dockerignore`
|
| 43 |
+
```dockerignore
|
| 44 |
+
__pycache__
|
| 45 |
+
*.pyc
|
| 46 |
+
*.pyo
|
| 47 |
+
*.pyd
|
| 48 |
+
.Python
|
| 49 |
+
env/
|
| 50 |
+
venv/
|
| 51 |
+
.venv/
|
| 52 |
+
vectorstore/
|
| 53 |
+
docstore/
|
| 54 |
+
docs/
|
| 55 |
+
*.pdf
|
| 56 |
+
.git/
|
| 57 |
+
.gitignore
|
| 58 |
+
README.md
|
| 59 |
+
.DS_Store
|
| 60 |
+
*.log
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
### 3. `requirements.txt`
|
| 64 |
+
```txt
|
| 65 |
+
fastapi==0.109.0
|
| 66 |
+
uvicorn[standard]==0.27.0
|
| 67 |
+
python-multipart==0.0.6
|
| 68 |
+
requests==2.31.0
|
| 69 |
+
langchain-community==0.0.20
|
| 70 |
+
sentence-transformers==2.3.1
|
| 71 |
+
faiss-cpu==1.7.4
|
| 72 |
+
pypdf==3.17.4
|
| 73 |
+
pdfplumber==0.10.3
|
| 74 |
+
PyMuPDF==1.23.8
|
| 75 |
+
Pillow==10.2.0
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
### 4. `Dockerfile`
|
| 79 |
+
**Copy from the artifact:** "Dockerfile - Final Version (with main.py)"
|
| 80 |
+
|
| 81 |
+
### 5. `main.py`
|
| 82 |
+
**Copy from the artifact:** "main.py - Service Router"
|
| 83 |
+
|
| 84 |
+
### 6. Your Existing Files
|
| 85 |
+
- β
`split.py` (you already have this)
|
| 86 |
+
- β
`query_service.py` (you already have this)
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
## π Deployment Steps
|
| 91 |
+
|
| 92 |
+
### Step 1: Create Hugging Face Space
|
| 93 |
+
1. Go to https://huggingface.co/spaces
|
| 94 |
+
2. Click **"Create new Space"**
|
| 95 |
+
3. Configure:
|
| 96 |
+
- **Name**: `multimodal-rag` (or your choice)
|
| 97 |
+
- **License**: Apache 2.0
|
| 98 |
+
- **Space SDK**: **Docker** β οΈ (Important!)
|
| 99 |
+
- **Visibility**: Public or Private
|
| 100 |
+
4. Click **"Create Space"**
|
| 101 |
+
|
| 102 |
+
### Step 2: Clone Repository
|
| 103 |
+
```bash
|
| 104 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/multimodal-rag
|
| 105 |
+
cd multimodal-rag
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
### Step 3: Add All Files
|
| 109 |
+
Copy these 7 files into the directory:
|
| 110 |
+
- [ ] `.gitignore`
|
| 111 |
+
- [ ] `.dockerignore`
|
| 112 |
+
- [ ] `Dockerfile`
|
| 113 |
+
- [ ] `requirements.txt`
|
| 114 |
+
- [ ] `main.py`
|
| 115 |
+
- [ ] `split.py`
|
| 116 |
+
- [ ] `query_service.py`
|
| 117 |
+
|
| 118 |
+
### Step 4: Push to Hugging Face
|
| 119 |
+
```bash
|
| 120 |
+
git add .
|
| 121 |
+
git commit -m "Initial deployment with combined service"
|
| 122 |
+
git push
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
### Step 5: Wait for Build
|
| 126 |
+
- β±οΈ Build takes **5-10 minutes**
|
| 127 |
+
- Watch the build logs in your Space settings
|
| 128 |
+
- Look for: `β
Ingestion service: READY` and `β
Query service: READY`
|
| 129 |
+
|
| 130 |
+
### Step 6: Test Your Deployment
|
| 131 |
+
```bash
|
| 132 |
+
# Set your URL
|
| 133 |
+
export BASE_URL="https://YOUR_USERNAME-multimodal-rag.hf.space"
|
| 134 |
+
|
| 135 |
+
# Test health
|
| 136 |
+
curl $BASE_URL/health
|
| 137 |
+
|
| 138 |
+
# Test home
|
| 139 |
+
curl $BASE_URL/
|
| 140 |
+
|
| 141 |
+
# Upload a PDF
|
| 142 |
+
curl -X POST "$BASE_URL/ingest" \
|
| 143 |
+
-F "file=@sample.pdf"
|
| 144 |
+
|
| 145 |
+
# Query
|
| 146 |
+
curl -X POST "$BASE_URL/query?question=What%20is%20this%20about?&k=5"
|
| 147 |
+
```
|
| 148 |
+
|
| 149 |
+
---
|
| 150 |
+
|
| 151 |
+
## π― Your API Endpoints
|
| 152 |
+
|
| 153 |
+
Once deployed at `https://YOUR_USERNAME-multimodal-rag.hf.space`:
|
| 154 |
+
|
| 155 |
+
| Endpoint | Method | Purpose |
|
| 156 |
+
|----------|--------|---------|
|
| 157 |
+
| `/` | GET | Service info & documentation |
|
| 158 |
+
| `/health` | GET | Health check |
|
| 159 |
+
| `/docs` | GET | Interactive API docs (Swagger) |
|
| 160 |
+
| `/ingest` | POST | Upload & process PDF |
|
| 161 |
+
| `/ingest/stats` | GET | Ingestion statistics |
|
| 162 |
+
| `/query` | POST | Query the RAG system |
|
| 163 |
+
| `/query/details` | POST | Query with detailed info |
|
| 164 |
+
| `/query/stats` | GET | Query statistics |
|
| 165 |
+
| `/stats` | GET | Combined statistics |
|
| 166 |
+
|
| 167 |
+
---
|
| 168 |
+
|
| 169 |
+
## π§ͺ Testing Examples
|
| 170 |
+
|
| 171 |
+
### Using cURL
|
| 172 |
+
|
| 173 |
+
```bash
|
| 174 |
+
BASE_URL="https://YOUR_USERNAME-multimodal-rag.hf.space"
|
| 175 |
+
|
| 176 |
+
# 1. Check if service is running
|
| 177 |
+
curl $BASE_URL/health
|
| 178 |
+
|
| 179 |
+
# 2. Upload a PDF
|
| 180 |
+
curl -X POST "$BASE_URL/ingest" \
|
| 181 |
+
-F "file=@document.pdf"
|
| 182 |
+
|
| 183 |
+
# 3. Check stats
|
| 184 |
+
curl "$BASE_URL/stats"
|
| 185 |
+
|
| 186 |
+
# 4. Query the system
|
| 187 |
+
curl -X POST "$BASE_URL/query?question=Summarize%20the%20document&k=5"
|
| 188 |
+
|
| 189 |
+
# 5. Detailed query
|
| 190 |
+
curl -X POST "$BASE_URL/query/details?question=What%20are%20the%20key%20points?&k=3"
|
| 191 |
+
```
|
| 192 |
+
|
| 193 |
+
### Using Python
|
| 194 |
+
|
| 195 |
+
```python
|
| 196 |
+
import requests
|
| 197 |
+
|
| 198 |
+
BASE_URL = "https://YOUR_USERNAME-multimodal-rag.hf.space"
|
| 199 |
+
|
| 200 |
+
# 1. Health check
|
| 201 |
+
response = requests.get(f"{BASE_URL}/health")
|
| 202 |
+
print(response.json())
|
| 203 |
+
|
| 204 |
+
# 2. Upload PDF
|
| 205 |
+
with open("document.pdf", "rb") as f:
|
| 206 |
+
files = {"file": ("document.pdf", f, "application/pdf")}
|
| 207 |
+
response = requests.post(f"{BASE_URL}/ingest", files=files)
|
| 208 |
+
print(response.json())
|
| 209 |
+
|
| 210 |
+
# 3. Query
|
| 211 |
+
params = {"question": "What is the main topic?", "k": 5}
|
| 212 |
+
response = requests.post(f"{BASE_URL}/query", params=params)
|
| 213 |
+
print(response.json())
|
| 214 |
+
```
|
| 215 |
+
|
| 216 |
+
### Using JavaScript (Frontend)
|
| 217 |
+
|
| 218 |
+
```javascript
|
| 219 |
+
const BASE_URL = "https://YOUR_USERNAME-multimodal-rag.hf.space";
|
| 220 |
+
|
| 221 |
+
// Upload PDF
|
| 222 |
+
async function uploadPDF(file) {
|
| 223 |
+
const formData = new FormData();
|
| 224 |
+
formData.append('file', file);
|
| 225 |
+
|
| 226 |
+
const response = await fetch(`${BASE_URL}/ingest`, {
|
| 227 |
+
method: 'POST',
|
| 228 |
+
body: formData
|
| 229 |
+
});
|
| 230 |
+
|
| 231 |
+
return await response.json();
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
// Query
|
| 235 |
+
async function queryRAG(question, k = 5) {
|
| 236 |
+
const response = await fetch(
|
| 237 |
+
`${BASE_URL}/query?question=${encodeURIComponent(question)}&k=${k}`,
|
| 238 |
+
{ method: 'POST' }
|
| 239 |
+
);
|
| 240 |
+
|
| 241 |
+
return await response.json();
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
// Usage
|
| 245 |
+
const result = await queryRAG("What is this document about?");
|
| 246 |
+
console.log(result.answer);
|
| 247 |
+
```
|
| 248 |
+
|
| 249 |
+
---
|
| 250 |
+
|
| 251 |
+
## β οΈ Important Notes
|
| 252 |
+
|
| 253 |
+
### 1. **Persistent Storage** (Critical!)
|
| 254 |
+
By default, Hugging Face Spaces are ephemeral. Your vectorstore will disappear on restart!
|
| 255 |
+
|
| 256 |
+
**Solution Options:**
|
| 257 |
+
- **Enable Persistent Storage** (in Space settings - paid feature)
|
| 258 |
+
- **Use External Vector DB** (Pinecone, Weaviate, Qdrant)
|
| 259 |
+
- **Rebuild on startup** (include sample docs in repo)
|
| 260 |
+
|
| 261 |
+
### 2. **Memory Management**
|
| 262 |
+
Free tier has limited RAM. If you get OOM errors:
|
| 263 |
+
- Reduce `k` parameter in queries
|
| 264 |
+
- Process smaller PDFs
|
| 265 |
+
- Upgrade to paid tier
|
| 266 |
+
|
| 267 |
+
### 3. **Build Time**
|
| 268 |
+
First deployment takes longer. Subsequent deployments are faster (Docker layer caching).
|
| 269 |
+
|
| 270 |
---
|
| 271 |
+
|
| 272 |
+
## π What You Get
|
| 273 |
+
|
| 274 |
+
With `main.py`, your service will:
|
| 275 |
+
|
| 276 |
+
β
Handle PDF ingestion (`/ingest`)
|
| 277 |
+
β
Handle queries (`/query`)
|
| 278 |
+
β
Provide statistics (`/stats`)
|
| 279 |
+
β
Serve interactive API docs (`/docs`)
|
| 280 |
+
β
Share vectorstore between services
|
| 281 |
+
β
Run on a single endpoint
|
| 282 |
+
β
Cost-effective (one space instead of two)
|
| 283 |
+
|
| 284 |
+
---
|
| 285 |
+
|
| 286 |
+
## π Troubleshooting
|
| 287 |
+
|
| 288 |
+
### Build Fails
|
| 289 |
+
```bash
|
| 290 |
+
# Check these:
|
| 291 |
+
- Dockerfile CMD points to "main:app" β
|
| 292 |
+
- All 7 files are present β
|
| 293 |
+
- requirements.txt has no typos β
|
| 294 |
+
```
|
| 295 |
+
|
| 296 |
+
### Service Won't Start
|
| 297 |
+
```bash
|
| 298 |
+
# In Space logs, look for:
|
| 299 |
+
β
Loaded ingestion service (split.py)
|
| 300 |
+
β
Loaded query service (query_service.py)
|
| 301 |
+
β
Ingestion service: READY
|
| 302 |
+
β
Query service: READY
|
| 303 |
+
|
| 304 |
+
# If you see errors, they'll show here
|
| 305 |
+
```
|
| 306 |
+
|
| 307 |
+
### No Documents Found
|
| 308 |
+
```
|
| 309 |
+
Problem: Query returns "No relevant documents found"
|
| 310 |
+
Solution:
|
| 311 |
+
1. Check if you uploaded PDFs via /ingest
|
| 312 |
+
2. Enable persistent storage
|
| 313 |
+
3. Check /stats to see vectorstore_count
|
| 314 |
+
```
|
| 315 |
+
|
| 316 |
+
### Slow Responses
|
| 317 |
+
```
|
| 318 |
+
Problem: Queries take too long
|
| 319 |
+
Solution:
|
| 320 |
+
1. Reduce k parameter (try k=3 instead of k=5)
|
| 321 |
+
2. Upgrade Space hardware
|
| 322 |
+
3. Check external API endpoints (QWEN, BLIP) are fast
|
| 323 |
+
```
|
| 324 |
+
|
| 325 |
+
---
|
| 326 |
+
|
| 327 |
+
## π Additional Resources
|
| 328 |
+
|
| 329 |
+
- [Hugging Face Spaces Docs](https://huggingface.co/docs/hub/spaces)
|
| 330 |
+
- [FastAPI Documentation](https://fastapi.tiangolo.com/)
|
| 331 |
+
- [Your API Docs](https://YOUR_USERNAME-multimodal-rag.hf.space/docs) (after deployment)
|
| 332 |
+
|
| 333 |
+
---
|
| 334 |
+
|
| 335 |
+
## β
Final Checklist
|
| 336 |
+
|
| 337 |
+
Before pushing:
|
| 338 |
+
- [ ] Created `.gitignore`
|
| 339 |
+
- [ ] Created `.dockerignore`
|
| 340 |
+
- [ ] Copied `Dockerfile`
|
| 341 |
+
- [ ] Copied `requirements.txt`
|
| 342 |
+
- [ ] Created `main.py` β
|
| 343 |
+
- [ ] Have `split.py`
|
| 344 |
+
- [ ] Have `query_service.py`
|
| 345 |
+
- [ ] Space SDK is set to **Docker**
|
| 346 |
+
- [ ] Ready to `git push`
|
| 347 |
+
|
| 348 |
+
After deployment:
|
| 349 |
+
- [ ] Build completed successfully
|
| 350 |
+
- [ ] `/health` endpoint responds
|
| 351 |
+
- [ ] Can upload PDF via `/ingest`
|
| 352 |
+
- [ ] Can query via `/query`
|
| 353 |
+
- [ ] Considered enabling persistent storage
|
| 354 |
+
|
| 355 |
+
---
|
| 356 |
+
|
| 357 |
+
## π― Quick Command Summary
|
| 358 |
+
|
| 359 |
+
```bash
|
| 360 |
+
# Clone
|
| 361 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/multimodal-rag
|
| 362 |
+
cd multimodal-rag
|
| 363 |
+
|
| 364 |
+
# Add files (copy all 7 files here)
|
| 365 |
+
|
| 366 |
+
# Deploy
|
| 367 |
+
git add .
|
| 368 |
+
git commit -m "Deploy combined RAG service"
|
| 369 |
+
git push
|
| 370 |
+
|
| 371 |
+
# Test (after build completes)
|
| 372 |
+
curl https://YOUR_USERNAME-multimodal-rag.hf.space/health
|
| 373 |
+
```
|
| 374 |
+
|
| 375 |
---
|
| 376 |
|
| 377 |
+
**You're all set! π** Once you push, wait ~10 minutes and your service will be live!
|