File size: 8,533 Bytes
4718630
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# SPARKNET Deployment Guide

## Architecture Overview

SPARKNET supports a hybrid deployment architecture:

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Streamlit Cloud         β”‚         β”‚    GPU Server (Lytos)       β”‚
β”‚     (Frontend/UI)           β”‚  HTTPS  β”‚    FastAPI Backend          β”‚
β”‚                             β”‚ ◄─────► β”‚                             β”‚
β”‚  sparknet.streamlit.app     β”‚   API   β”‚  - PaddleOCR (GPU)          β”‚
β”‚                             β”‚         β”‚  - Document Processing       β”‚
β”‚  - User Interface           β”‚         β”‚  - RAG + Embeddings          β”‚
β”‚  - Authentication           β”‚         β”‚  - Ollama LLM                β”‚
β”‚  - Cloud LLM fallback       β”‚         β”‚  - ChromaDB Vector Store     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Deployment Options

### Option 1: Full Stack on GPU Server (Recommended for Production)

Run both frontend and backend on Lytos with GPU acceleration.

### Option 2: Hybrid (Streamlit Cloud + GPU Backend)

- **Frontend**: Streamlit Cloud (free hosting, easy sharing)
- **Backend**: Lytos GPU server (full processing power)

### Option 3: Streamlit Cloud Only (Demo Mode)

- Uses cloud LLM providers (Groq, Gemini, etc.)
- Limited functionality (no OCR, no RAG indexing)

---

## Option 2: Hybrid Deployment (Recommended)

### Step 1: Setup Backend on Lytos (GPU Server)

#### 1.1 SSH into Lytos
```bash
ssh user@lytos.server.address
```

#### 1.2 Clone the repository
```bash
git clone https://github.com/your-repo/sparknet.git
cd sparknet
```

#### 1.3 Create virtual environment
```bash
python -m venv venv
source venv/bin/activate
```

#### 1.4 Install backend dependencies
```bash
pip install -r backend/requirements.txt
```

#### 1.5 Install Ollama (for LLM inference)
```bash
curl -fsSL https://ollama.com/install.sh | sh

# Pull required models
ollama pull llama3.2:latest
ollama pull nomic-embed-text
```

#### 1.6 Start the backend server
```bash
# Development mode
cd backend
uvicorn api:app --host 0.0.0.0 --port 8000 --reload

# Production mode (with multiple workers)
uvicorn api:app --host 0.0.0.0 --port 8000 --workers 4
```

#### 1.7 (Optional) Run with systemd for auto-restart
```bash
sudo nano /etc/systemd/system/sparknet-backend.service
```

Add:
```ini
[Unit]
Description=SPARKNET Backend API
After=network.target

[Service]
Type=simple
User=your-user
WorkingDirectory=/path/to/sparknet/backend
Environment=PATH=/path/to/sparknet/venv/bin
ExecStart=/path/to/sparknet/venv/bin/uvicorn api:app --host 0.0.0.0 --port 8000 --workers 4
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
```

Enable and start:
```bash
sudo systemctl enable sparknet-backend
sudo systemctl start sparknet-backend
```

#### 1.8 Configure firewall (allow port 8000)
```bash
sudo ufw allow 8000/tcp
```

#### 1.9 (Optional) Setup HTTPS with nginx
```bash
sudo apt install nginx certbot python3-certbot-nginx

sudo nano /etc/nginx/sites-available/sparknet
```

Add:
```nginx
server {
    listen 80;
    server_name api.sparknet.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_cache_bypass $http_upgrade;
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
    }
}
```

Enable and get SSL:
```bash
sudo ln -s /etc/nginx/sites-available/sparknet /etc/nginx/sites-enabled/
sudo certbot --nginx -d api.sparknet.yourdomain.com
sudo systemctl restart nginx
```

### Step 2: Configure Streamlit Cloud

#### 2.1 Update Streamlit secrets

In Streamlit Cloud dashboard β†’ Settings β†’ Secrets, add:

```toml
[auth]
password = "SPARKNET@2026"

# Backend URL (your Lytos server)
BACKEND_URL = "https://api.sparknet.yourdomain.com"
# Or without HTTPS:
# BACKEND_URL = "http://lytos-ip-address:8000"

# Fallback cloud providers (optional, used if backend unavailable)
GROQ_API_KEY = "your-groq-key"
GOOGLE_API_KEY = "your-google-key"
```

#### 2.2 Deploy to Streamlit Cloud

Push your code and Streamlit Cloud will auto-deploy:
```bash
git add .
git commit -m "Add backend support"
git push origin main
```

### Step 3: Verify Deployment

#### 3.1 Test backend directly
```bash
# Health check
curl https://api.sparknet.yourdomain.com/api/health

# System status
curl https://api.sparknet.yourdomain.com/api/status
```

#### 3.2 Test from Streamlit
Visit your Streamlit app and check:
- Status bar should show "Backend" instead of "Demo Mode"
- GPU indicator should appear
- Document processing should use full pipeline

---

## Backend API Endpoints

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/api/health` | GET | Health check |
| `/api/status` | GET | System status (Ollama, GPU, RAG) |
| `/api/process` | POST | Process document (OCR, layout) |
| `/api/index` | POST | Index document to RAG |
| `/api/query` | POST | Query RAG system |
| `/api/search` | POST | Search similar chunks |
| `/api/documents` | GET | List indexed documents |
| `/api/documents/{id}` | DELETE | Delete document |

### API Documentation

Once backend is running, visit:
- Swagger UI: `http://lytos:8000/docs`
- ReDoc: `http://lytos:8000/redoc`

---

## Environment Variables

### Backend (Lytos)

```bash
# Optional: Configure Ollama host if not localhost
export OLLAMA_HOST=http://localhost:11434

# Optional: GPU device selection
export CUDA_VISIBLE_DEVICES=0
```

### Frontend (Streamlit)

Set in `secrets.toml` or Streamlit Cloud secrets:

```toml
# Required for hybrid mode
BACKEND_URL = "https://your-backend-url"

# Authentication
[auth]
password = "your-password"

# Fallback cloud providers
GROQ_API_KEY = "..."
GOOGLE_API_KEY = "..."
```

---

## Troubleshooting

### Backend not reachable

1. Check if backend is running:
   ```bash
   curl http://localhost:8000/api/health
   ```

2. Check firewall:
   ```bash
   sudo ufw status
   ```

3. Check nginx logs:
   ```bash
   sudo tail -f /var/log/nginx/error.log
   ```

### GPU not detected

1. Check CUDA:
   ```bash
   nvidia-smi
   python -c "import torch; print(torch.cuda.is_available())"
   ```

2. Check PaddlePaddle GPU:
   ```bash
   python -c "import paddle; print(paddle.device.is_compiled_with_cuda())"
   ```

### Ollama not working

1. Check Ollama status:
   ```bash
   ollama list
   curl http://localhost:11434/api/tags
   ```

2. Restart Ollama:
   ```bash
   sudo systemctl restart ollama
   ```

### Document processing fails

1. Check backend logs:
   ```bash
   journalctl -u sparknet-backend -f
   ```

2. Test processing directly:
   ```bash
   curl -X POST http://localhost:8000/api/process \
     -F "file=@test.pdf" \
     -F "ocr_engine=paddleocr"
   ```

---

## Security Considerations

### Production Checklist

- [ ] Enable HTTPS for backend API
- [ ] Configure CORS properly (restrict origins)
- [ ] Use strong authentication password
- [ ] Enable rate limiting
- [ ] Set up monitoring and alerts
- [ ] Configure backup for ChromaDB data
- [ ] Review GDPR compliance for data handling

### CORS Configuration

In `backend/api.py`, update for production:

```python
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://sparknet.streamlit.app"],  # Your Streamlit URL
    allow_credentials=True,
    allow_methods=["GET", "POST", "DELETE"],
    allow_headers=["*"],
)
```

---

## Performance Tuning

### Backend Workers

Adjust based on CPU cores:
```bash
uvicorn api:app --workers $(nproc)
```

### GPU Memory

For large documents, monitor GPU memory:
```bash
watch -n 1 nvidia-smi
```

### ChromaDB Optimization

For large document collections:
```python
store_config = VectorStoreConfig(
    persist_directory="data/sparknet_unified_rag",
    collection_name="sparknet_documents",
    similarity_threshold=0.0,
    # Add indexing options for better performance
)
```

---

## Contact & Support

- **Project**: VISTA/Horizon EU
- **Framework**: SPARKNET - Strategic Patent Acceleration & Research Kinetics NETwork
- **Issues**: https://github.com/your-repo/sparknet/issues