File size: 9,248 Bytes
2945a34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaf4201
 
 
 
 
 
 
 
2945a34
 
 
 
 
 
 
 
 
 
 
aaf4201
2945a34
 
 
 
 
 
 
 
 
aaf4201
 
2945a34
 
aaf4201
 
2945a34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aaf4201
 
 
 
2945a34
 
 
 
 
 
 
aaf4201
2945a34
 
 
aaf4201
 
 
 
 
 
 
 
 
2945a34
 
aaf4201
 
 
 
 
 
 
 
2945a34
aaf4201
2945a34
aaf4201
2945a34
aaf4201
2945a34
aaf4201
2945a34
aaf4201
 
 
 
2945a34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# Flask API Setup Guide

## Overview

The Flask API (`flask_api.py`) provides a simple REST API interface to the Research AI Assistant orchestrator. It runs on port 5001, separate from Gradio (port 7860).

## Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Express.js UI  β”‚ Port 3000
β”‚  (or other      β”‚ - User interface
β”‚   frontend)     β”‚ - Session management
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ HTTP POST /api/chat
         ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Flask API      β”‚ Port 5001
β”‚  flask_api.py   β”‚ - REST endpoints
β”‚                 β”‚ - Imports from app.py
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ - Direct Python access
         β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Orchestrator   β”‚ Python
β”‚  (from app.py)  β”‚ - AI agents
β”‚                 β”‚ - Context management
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Gradio UI      β”‚ Port 7860
β”‚  (optional)     β”‚ - Alternative UI
β”‚  app.py         β”‚ - Can run alongside
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## Quick Start

### Automatic Start (Hugging Face Spaces)

**Flask API starts automatically** when you deploy to HF Spaces using `main.py` as the entry point.

No additional configuration needed - it runs in the background alongside Gradio.

### Manual Start (Local Development)

### Step 1: Install Dependencies

```bash
pip install flask flask-cors
```

Or install all requirements:
```bash
pip install -r requirements.txt
```

### Step 2: Start Flask API (Optional - Only if not using main.py)

```bash
# Default port 5001
python flask_api.py

# Or specify custom port
FLASK_PORT=5001 python flask_api.py
```

**Note:** If using `main.py` (HF Spaces entry point), Flask API starts automatically. Only run `flask_api.py` directly if you want Flask API without Gradio.

You should see:
```
INITIALIZING FLASK API ORCHESTRATOR
βœ“ AI ORCHESTRATOR READY (shared with Gradio)
Starting Flask API on port 5001
```

### Step 3: Test the API

```bash
# Health check
curl http://localhost:5001/health

# Chat endpoint
curl -X POST http://localhost:5001/api/chat \
  -H "Content-Type: application/json" \
  -d '{
    "message": "Hello, how are you?",
    "session_id": "test-session-123",
    "user_id": "test-user"
  }'
```

## API Endpoints

### `GET /health`

Health check endpoint.

**Response:**
```json
{
  "status": "ok",
  "orchestrator_available": true,
  "service": "flask_api"
}
```

### `POST /api/chat`

Main chat endpoint for processing user messages.

**Request:**
```json
{
  "message": "What is machine learning?",
  "history": [],  // optional
  "session_id": "session-123",  // optional, auto-generated if not provided
  "user_id": "user-123"  // optional, defaults to "Admin_J"
}
```

**Response:**
```json
{
  "response": "Machine learning is...",
  "history": [
    {"role": "user", "content": "What is machine learning?"},
    {"role": "assistant", "content": "Machine learning is..."}
  ],
  "reasoning": {
    "chain_of_thought": {...},
    "confidence_calibration": {...}
  },
  "performance": {
    "processing_time": 1.23,
    "tokens_used": 456
  },
  "context": {
    "session_context": "...",
    "relevant_interactions": [...]
  },
  "session_id": "session-123",
  "skills": "<div>...</div>"  // HTML for skills display
}
```

### `GET /api/orchestrator/status`

Check if orchestrator is available.

**Response:**
```json
{
  "orchestrator_available": true,
  "orchestrator_type": "MVPOrchestrator"
}
```

## Configuration

### Environment Variables

```bash
# Flask configuration (for HF Spaces or manual start)
ENABLE_FLASK_API=true   # Enable/disable Flask API (default: true)
FLASK_PORT=5001          # Port for Flask API (default: 5001)
FLASK_HOST=0.0.0.0      # Host to bind to (default: 0.0.0.0)

# AI configuration (used by orchestrator)
HF_TOKEN=your_token_here  # Hugging Face token for API access
```

### Ports

- **Flask API**: 5001 (configurable via `FLASK_PORT`, starts automatically with `main.py`)
- **Gradio UI**: 7860 (separate, can run alongside)
- **Express.js** (external): 3000 (your frontend)

### Disable Flask API

To disable Flask API on HF Spaces:
```bash
ENABLE_FLASK_API=false
```

This prevents Flask from starting (only Gradio will run).

## Running with Gradio

### On Hugging Face Spaces (Automatic)

When using `main.py` as the entry point:
- **Gradio UI**: Runs on port 7860 (primary interface)
- **Flask API**: Starts automatically in background thread on port 5001
- Both run together, sharing the same orchestrator instance

### Local Development (Manual)

**Option 1: Use main.py (recommended - matches HF Spaces)**
```bash
python main.py
```
This starts both Gradio and Flask API automatically.

**Option 2: Run separately**
```bash
# Terminal 1: Flask API
python flask_api.py

# Terminal 2: Gradio UI
python app.py
```

Both can run simultaneously on different ports.

## Integration Examples

### Express.js Integration

See `EXPRESS_FLASK_INTEGRATION.md` for Express.js setup.

### Python Client

```python
import requests

def chat_with_flask(message, session_id=None):
    url = "http://localhost:5001/api/chat"
    data = {
        "message": message,
        "session_id": session_id or "python-client-session"
    }
    response = requests.post(url, json=data)
    return response.json()

# Usage
result = chat_with_flask("Hello, how are you?")
print(result["response"])
```

### JavaScript/Node.js Client

```javascript
const axios = require('axios');

async function chatWithFlask(message, sessionId = null) {
    const response = await axios.post('http://localhost:5001/api/chat', {
        message: message,
        session_id: sessionId || `node-client-session-${Date.now()}`
    });
    return response.data;
}

// Usage
chatWithFlask("Hello, how are you?")
    .then(result => console.log(result.response))
    .catch(error => console.error(error));
```

## Troubleshooting

### Issue: "Orchestrator not available"

**Check:**
1. Flask API logs show initialization errors
2. `HF_TOKEN` environment variable is set
3. All dependencies are installed

**Solution:**
```bash
# Check logs
# Look for "INITIALIZING AI ORCHESTRATOR" section

# Verify HF token
echo $HF_TOKEN

# Reinstall dependencies
pip install -r requirements.txt
```

### Issue: "Port already in use"

**Solution:**
```bash
# Use different port
FLASK_PORT=5002 python flask_api.py
```

### Issue: "Module not found"

**Check:**
1. You're in the correct directory (same as `app.py`)
2. All imports are available

**Solution:**
```bash
# Make sure you're in the project root
cd /path/to/Research_AI_Assistant

# Verify Python path
python -c "import app; print('βœ“ app.py importable')"
```

### Issue: "CORS errors"

**Solution:** Flask-CORS is enabled by default. If issues persist:

```python
# In flask_api.py, customize CORS:
from flask_cors import CORS
CORS(app, resources={r"/api/*": {"origins": "*"}})
```

## Advantages Over Gradio API

| Feature | Flask API | Gradio HTTP |
|---------|-----------|-------------|
| **Simplicity** | βœ… Simple JSON | ⚠️ Gradio-specific format |
| **Control** | βœ… Full control | ⚠️ Limited by Gradio |
| **Path issues** | βœ… None | ⚠️ Can have path problems |
| **Dependencies** | βœ… Just Flask | ⚠️ Requires Gradio client |
| **API design** | βœ… You design it | ⚠️ Gradio's design |
| **Startup** | βœ… Fast | ⚠️ Slower (Gradio overhead) |

## Production Deployment

### Using PM2 (Process Manager)

```bash
# Install PM2
npm install -g pm2

# Start Flask API
pm2 start flask_api.py --interpreter python3 --name flask-api

# Start Express (if using)
pm2 start server.js --name express

# View status
pm2 status
```

### Using systemd (Linux)

Create `/etc/systemd/system/flask-api.service`:
```ini
[Unit]
Description=Research AI Assistant Flask API
After=network.target

[Service]
Type=simple
User=your-user
WorkingDirectory=/path/to/Research_AI_Assistant
Environment="FLASK_PORT=5001"
Environment="HF_TOKEN=your-token"
ExecStart=/usr/bin/python3 flask_api.py
Restart=always

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

Then:
```bash
sudo systemctl enable flask-api
sudo systemctl start flask-api
```

## Security Considerations

1. **CORS**: Currently allows all origins (`*`). In production, restrict to your frontend domain:
   ```python
   CORS(app, origins=["https://your-frontend.com"])
   ```

2. **Rate Limiting**: Consider adding rate limiting:
   ```bash
   pip install flask-limiter
   ```

3. **Authentication**: Add API keys or tokens for production:
   ```python
   @app.before_request
   def check_api_key():
       api_key = request.headers.get('X-API-Key')
       if api_key != os.getenv('API_KEY'):
           return jsonify({'error': 'Unauthorized'}), 401
   ```

## Summary

βœ… **Simple**: Just Flask, no complex wrappers  
βœ… **Flexible**: Your API design, your control  
βœ… **Compatible**: Works alongside Gradio  
βœ… **Standard**: Standard REST API pattern  
βœ… **Fast**: Direct Python imports, no overhead  

Start Flask, integrate your frontend, done! πŸš€