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
Step 1: Install Dependencies
pip install flask flask-cors
Or install all requirements:
pip install -r requirements.txt
Step 2: Start Flask API
# Default port 5001
python flask_api.py
# Or specify custom port
FLASK_PORT=5001 python flask_api.py
You should see:
STARTING FLASK API
INITIALIZING AI ORCHESTRATOR
β AI ORCHESTRATOR READY
Starting Flask API on port 5001
Step 3: Test the API
# 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:
{
"status": "ok",
"orchestrator_available": true,
"service": "flask_api"
}
POST /api/chat
Main chat endpoint for processing user messages.
Request:
{
"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:
{
"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:
{
"orchestrator_available": true,
"orchestrator_type": "MVPOrchestrator"
}
Configuration
Environment Variables
# Flask configuration
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) - Gradio UI: 7860 (separate, can run alongside)
- Express.js (external): 3000 (your frontend)
Running with Gradio
You can run Flask API alongside Gradio:
Terminal 1: Flask API
python flask_api.py
Terminal 2: Gradio UI (optional)
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
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
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:
- Flask API logs show initialization errors
HF_TOKENenvironment variable is set- All dependencies are installed
Solution:
# 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:
# Use different port
FLASK_PORT=5002 python flask_api.py
Issue: "Module not found"
Check:
- You're in the correct directory (same as
app.py) - All imports are available
Solution:
# 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:
# 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)
# 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:
[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:
sudo systemctl enable flask-api
sudo systemctl start flask-api
Security Considerations
CORS: Currently allows all origins (
*). In production, restrict to your frontend domain:CORS(app, origins=["https://your-frontend.com"])Rate Limiting: Consider adding rate limiting:
pip install flask-limiterAuthentication: Add API keys or tokens for production:
@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! π