Commit
Β·
aaf4201
1
Parent(s):
2945a34
api endpoints for UI migration v11
Browse files- FLASK_API_SETUP.md +43 -12
- flask_api.py +36 -13
- main.py +52 -0
FLASK_API_SETUP.md
CHANGED
|
@@ -35,6 +35,14 @@ The Flask API (`flask_api.py`) provides a simple REST API interface to the Resea
|
|
| 35 |
|
| 36 |
## Quick Start
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
### Step 1: Install Dependencies
|
| 39 |
|
| 40 |
```bash
|
|
@@ -46,7 +54,7 @@ Or install all requirements:
|
|
| 46 |
pip install -r requirements.txt
|
| 47 |
```
|
| 48 |
|
| 49 |
-
### Step 2: Start Flask API
|
| 50 |
|
| 51 |
```bash
|
| 52 |
# Default port 5001
|
|
@@ -56,11 +64,12 @@ python flask_api.py
|
|
| 56 |
FLASK_PORT=5001 python flask_api.py
|
| 57 |
```
|
| 58 |
|
|
|
|
|
|
|
| 59 |
You should see:
|
| 60 |
```
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
β AI ORCHESTRATOR READY
|
| 64 |
Starting Flask API on port 5001
|
| 65 |
```
|
| 66 |
|
|
@@ -151,9 +160,10 @@ Check if orchestrator is available.
|
|
| 151 |
### Environment Variables
|
| 152 |
|
| 153 |
```bash
|
| 154 |
-
# Flask configuration
|
| 155 |
-
|
| 156 |
-
|
|
|
|
| 157 |
|
| 158 |
# AI configuration (used by orchestrator)
|
| 159 |
HF_TOKEN=your_token_here # Hugging Face token for API access
|
|
@@ -161,21 +171,42 @@ HF_TOKEN=your_token_here # Hugging Face token for API access
|
|
| 161 |
|
| 162 |
### Ports
|
| 163 |
|
| 164 |
-
- **Flask API**: 5001 (configurable via `FLASK_PORT`)
|
| 165 |
- **Gradio UI**: 7860 (separate, can run alongside)
|
| 166 |
- **Express.js** (external): 3000 (your frontend)
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
## Running with Gradio
|
| 169 |
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
-
**
|
| 173 |
```bash
|
| 174 |
-
python
|
| 175 |
```
|
|
|
|
| 176 |
|
| 177 |
-
**
|
| 178 |
```bash
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
python app.py
|
| 180 |
```
|
| 181 |
|
|
|
|
| 35 |
|
| 36 |
## Quick Start
|
| 37 |
|
| 38 |
+
### Automatic Start (Hugging Face Spaces)
|
| 39 |
+
|
| 40 |
+
**Flask API starts automatically** when you deploy to HF Spaces using `main.py` as the entry point.
|
| 41 |
+
|
| 42 |
+
No additional configuration needed - it runs in the background alongside Gradio.
|
| 43 |
+
|
| 44 |
+
### Manual Start (Local Development)
|
| 45 |
+
|
| 46 |
### Step 1: Install Dependencies
|
| 47 |
|
| 48 |
```bash
|
|
|
|
| 54 |
pip install -r requirements.txt
|
| 55 |
```
|
| 56 |
|
| 57 |
+
### Step 2: Start Flask API (Optional - Only if not using main.py)
|
| 58 |
|
| 59 |
```bash
|
| 60 |
# Default port 5001
|
|
|
|
| 64 |
FLASK_PORT=5001 python flask_api.py
|
| 65 |
```
|
| 66 |
|
| 67 |
+
**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.
|
| 68 |
+
|
| 69 |
You should see:
|
| 70 |
```
|
| 71 |
+
INITIALIZING FLASK API ORCHESTRATOR
|
| 72 |
+
β AI ORCHESTRATOR READY (shared with Gradio)
|
|
|
|
| 73 |
Starting Flask API on port 5001
|
| 74 |
```
|
| 75 |
|
|
|
|
| 160 |
### Environment Variables
|
| 161 |
|
| 162 |
```bash
|
| 163 |
+
# Flask configuration (for HF Spaces or manual start)
|
| 164 |
+
ENABLE_FLASK_API=true # Enable/disable Flask API (default: true)
|
| 165 |
+
FLASK_PORT=5001 # Port for Flask API (default: 5001)
|
| 166 |
+
FLASK_HOST=0.0.0.0 # Host to bind to (default: 0.0.0.0)
|
| 167 |
|
| 168 |
# AI configuration (used by orchestrator)
|
| 169 |
HF_TOKEN=your_token_here # Hugging Face token for API access
|
|
|
|
| 171 |
|
| 172 |
### Ports
|
| 173 |
|
| 174 |
+
- **Flask API**: 5001 (configurable via `FLASK_PORT`, starts automatically with `main.py`)
|
| 175 |
- **Gradio UI**: 7860 (separate, can run alongside)
|
| 176 |
- **Express.js** (external): 3000 (your frontend)
|
| 177 |
|
| 178 |
+
### Disable Flask API
|
| 179 |
+
|
| 180 |
+
To disable Flask API on HF Spaces:
|
| 181 |
+
```bash
|
| 182 |
+
ENABLE_FLASK_API=false
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
This prevents Flask from starting (only Gradio will run).
|
| 186 |
+
|
| 187 |
## Running with Gradio
|
| 188 |
|
| 189 |
+
### On Hugging Face Spaces (Automatic)
|
| 190 |
+
|
| 191 |
+
When using `main.py` as the entry point:
|
| 192 |
+
- **Gradio UI**: Runs on port 7860 (primary interface)
|
| 193 |
+
- **Flask API**: Starts automatically in background thread on port 5001
|
| 194 |
+
- Both run together, sharing the same orchestrator instance
|
| 195 |
+
|
| 196 |
+
### Local Development (Manual)
|
| 197 |
|
| 198 |
+
**Option 1: Use main.py (recommended - matches HF Spaces)**
|
| 199 |
```bash
|
| 200 |
+
python main.py
|
| 201 |
```
|
| 202 |
+
This starts both Gradio and Flask API automatically.
|
| 203 |
|
| 204 |
+
**Option 2: Run separately**
|
| 205 |
```bash
|
| 206 |
+
# Terminal 1: Flask API
|
| 207 |
+
python flask_api.py
|
| 208 |
+
|
| 209 |
+
# Terminal 2: Gradio UI
|
| 210 |
python app.py
|
| 211 |
```
|
| 212 |
|
flask_api.py
CHANGED
|
@@ -29,38 +29,45 @@ CORS(app) # Enable CORS for Express.js and other frontends
|
|
| 29 |
# This imports without triggering Gradio launch
|
| 30 |
_orchestrator = None
|
| 31 |
_process_message_async = None
|
|
|
|
| 32 |
|
| 33 |
def initialize_orchestrator():
|
| 34 |
"""Initialize the orchestrator from app.py"""
|
| 35 |
-
global _orchestrator, _process_message_async
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
try:
|
| 38 |
logger.info("=" * 60)
|
| 39 |
-
logger.info("
|
| 40 |
-
logger.info("=" * 60)
|
| 41 |
-
logger.info("INITIALIZING AI ORCHESTRATOR")
|
| 42 |
logger.info("=" * 60)
|
| 43 |
|
| 44 |
# Import from app.py (this won't trigger app.py's launch code)
|
|
|
|
| 45 |
from app import orchestrator, process_message_async
|
| 46 |
|
| 47 |
_orchestrator = orchestrator
|
| 48 |
_process_message_async = process_message_async
|
| 49 |
|
| 50 |
if _orchestrator is not None:
|
| 51 |
-
logger.info("β AI ORCHESTRATOR READY")
|
| 52 |
else:
|
| 53 |
logger.warning("β Orchestrator not available - some features may be limited")
|
| 54 |
|
|
|
|
| 55 |
logger.info("=" * 60)
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
logger.error(f"Failed to initialize orchestrator: {e}", exc_info=True)
|
| 59 |
_orchestrator = None
|
| 60 |
_process_message_async = None
|
|
|
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
@app.route('/health', methods=['GET'])
|
| 66 |
def health_check():
|
|
@@ -94,6 +101,10 @@ def chat():
|
|
| 94 |
"skills": "..." // skills HTML
|
| 95 |
}
|
| 96 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
try:
|
| 98 |
data = request.get_json()
|
| 99 |
|
|
@@ -170,13 +181,25 @@ def orchestrator_status():
|
|
| 170 |
'orchestrator_type': type(_orchestrator).__name__ if _orchestrator else None
|
| 171 |
})
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
logger.info(f"Health check: http://{host}:{port}/health")
|
| 179 |
logger.info(f"Chat endpoint: http://{host}:{port}/api/chat")
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
|
|
|
| 29 |
# This imports without triggering Gradio launch
|
| 30 |
_orchestrator = None
|
| 31 |
_process_message_async = None
|
| 32 |
+
_initialized = False
|
| 33 |
|
| 34 |
def initialize_orchestrator():
|
| 35 |
"""Initialize the orchestrator from app.py"""
|
| 36 |
+
global _orchestrator, _process_message_async, _initialized
|
| 37 |
+
|
| 38 |
+
if _initialized:
|
| 39 |
+
# Already initialized, return
|
| 40 |
+
return
|
| 41 |
|
| 42 |
try:
|
| 43 |
logger.info("=" * 60)
|
| 44 |
+
logger.info("INITIALIZING FLASK API ORCHESTRATOR")
|
|
|
|
|
|
|
| 45 |
logger.info("=" * 60)
|
| 46 |
|
| 47 |
# Import from app.py (this won't trigger app.py's launch code)
|
| 48 |
+
# The orchestrator is already initialized in app.py's module-level code
|
| 49 |
from app import orchestrator, process_message_async
|
| 50 |
|
| 51 |
_orchestrator = orchestrator
|
| 52 |
_process_message_async = process_message_async
|
| 53 |
|
| 54 |
if _orchestrator is not None:
|
| 55 |
+
logger.info("β AI ORCHESTRATOR READY (shared with Gradio)")
|
| 56 |
else:
|
| 57 |
logger.warning("β Orchestrator not available - some features may be limited")
|
| 58 |
|
| 59 |
+
_initialized = True
|
| 60 |
logger.info("=" * 60)
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
logger.error(f"Failed to initialize orchestrator: {e}", exc_info=True)
|
| 64 |
_orchestrator = None
|
| 65 |
_process_message_async = None
|
| 66 |
+
_initialized = False
|
| 67 |
|
| 68 |
+
# Only initialize if running directly (not when imported)
|
| 69 |
+
if __name__ == '__main__':
|
| 70 |
+
initialize_orchestrator()
|
| 71 |
|
| 72 |
@app.route('/health', methods=['GET'])
|
| 73 |
def health_check():
|
|
|
|
| 101 |
"skills": "..." // skills HTML
|
| 102 |
}
|
| 103 |
"""
|
| 104 |
+
# Ensure orchestrator is initialized (should already be from app.py)
|
| 105 |
+
if not _initialized:
|
| 106 |
+
initialize_orchestrator()
|
| 107 |
+
|
| 108 |
try:
|
| 109 |
data = request.get_json()
|
| 110 |
|
|
|
|
| 181 |
'orchestrator_type': type(_orchestrator).__name__ if _orchestrator else None
|
| 182 |
})
|
| 183 |
|
| 184 |
+
def start_flask_server(host='0.0.0.0', port=5001, debug=False):
|
| 185 |
+
"""
|
| 186 |
+
Start Flask server (can be called programmatically)
|
| 187 |
|
| 188 |
+
Args:
|
| 189 |
+
host: Host to bind to (default: 0.0.0.0)
|
| 190 |
+
port: Port to bind to (default: 5001)
|
| 191 |
+
debug: Enable debug mode (default: False)
|
| 192 |
+
"""
|
| 193 |
+
# Initialize orchestrator before starting server
|
| 194 |
+
initialize_orchestrator()
|
| 195 |
+
|
| 196 |
+
logger.info(f"Starting Flask API on {host}:{port}")
|
| 197 |
logger.info(f"Health check: http://{host}:{port}/health")
|
| 198 |
logger.info(f"Chat endpoint: http://{host}:{port}/api/chat")
|
| 199 |
+
app.run(host=host, port=port, debug=debug, use_reloader=False)
|
| 200 |
+
|
| 201 |
+
if __name__ == '__main__':
|
| 202 |
+
port = int(os.getenv('FLASK_PORT', 5001))
|
| 203 |
+
host = os.getenv('FLASK_HOST', '0.0.0.0')
|
| 204 |
+
start_flask_server(host=host, port=port, debug=False)
|
| 205 |
|
main.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
"""
|
| 2 |
Main integration file for HF Spaces deployment
|
| 3 |
Wires together UI, agents, and orchestrator
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import logging
|
| 8 |
import os
|
|
|
|
| 9 |
from typing import Dict, Any
|
| 10 |
|
| 11 |
# Configure logging
|
|
@@ -120,6 +122,51 @@ def create_event_handlers(demo, components):
|
|
| 120 |
|
| 121 |
return get_response_handler
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
def main():
|
| 124 |
"""Main entry point for HF Spaces"""
|
| 125 |
logger.info("π Starting AI Research Assistant MVP")
|
|
@@ -130,6 +177,9 @@ def main():
|
|
| 130 |
if not hf_token:
|
| 131 |
logger.warning("HF_TOKEN not found in environment. Some features may be limited.")
|
| 132 |
|
|
|
|
|
|
|
|
|
|
| 133 |
# Import the already-configured interface from app.py
|
| 134 |
# This imports the interface without triggering app.py's launch code
|
| 135 |
# (app.py's launch only runs when app.py is executed directly, not when imported)
|
|
@@ -161,6 +211,8 @@ def main():
|
|
| 161 |
|
| 162 |
logger.info("=" * 60)
|
| 163 |
logger.info("β
Application ready for launch")
|
|
|
|
|
|
|
| 164 |
logger.info("=" * 60)
|
| 165 |
return demo.launch(**launch_config)
|
| 166 |
|
|
|
|
| 1 |
"""
|
| 2 |
Main integration file for HF Spaces deployment
|
| 3 |
Wires together UI, agents, and orchestrator
|
| 4 |
+
Starts Flask API in background thread for external integrations
|
| 5 |
"""
|
| 6 |
|
| 7 |
import gradio as gr
|
| 8 |
import logging
|
| 9 |
import os
|
| 10 |
+
import threading
|
| 11 |
from typing import Dict, Any
|
| 12 |
|
| 13 |
# Configure logging
|
|
|
|
| 122 |
|
| 123 |
return get_response_handler
|
| 124 |
|
| 125 |
+
def start_flask_background():
|
| 126 |
+
"""Start Flask API in background thread"""
|
| 127 |
+
try:
|
| 128 |
+
# Check if Flask API should be enabled
|
| 129 |
+
enable_flask = os.getenv('ENABLE_FLASK_API', 'true').lower() == 'true'
|
| 130 |
+
if not enable_flask:
|
| 131 |
+
logger.info("Flask API disabled via ENABLE_FLASK_API=false")
|
| 132 |
+
return None
|
| 133 |
+
|
| 134 |
+
logger.info("Starting Flask API in background thread...")
|
| 135 |
+
|
| 136 |
+
# Import Flask API startup function
|
| 137 |
+
from flask_api import start_flask_server, initialize_orchestrator
|
| 138 |
+
|
| 139 |
+
# Initialize orchestrator (shared with Gradio)
|
| 140 |
+
initialize_orchestrator()
|
| 141 |
+
|
| 142 |
+
# Get Flask configuration
|
| 143 |
+
flask_port = int(os.getenv('FLASK_PORT', 5001))
|
| 144 |
+
flask_host = os.getenv('FLASK_HOST', '0.0.0.0')
|
| 145 |
+
|
| 146 |
+
# Start Flask in background thread
|
| 147 |
+
flask_thread = threading.Thread(
|
| 148 |
+
target=start_flask_server,
|
| 149 |
+
args=(flask_host, flask_port, False),
|
| 150 |
+
daemon=True, # Daemon thread so it stops when main thread stops
|
| 151 |
+
name="FlaskAPIThread"
|
| 152 |
+
)
|
| 153 |
+
flask_thread.start()
|
| 154 |
+
|
| 155 |
+
# Give Flask a moment to start
|
| 156 |
+
import time
|
| 157 |
+
time.sleep(2)
|
| 158 |
+
|
| 159 |
+
logger.info(f"β Flask API started in background on port {flask_port}")
|
| 160 |
+
logger.info(f" Health: http://{flask_host}:{flask_port}/health")
|
| 161 |
+
logger.info(f" Chat: http://{flask_host}:{flask_port}/api/chat")
|
| 162 |
+
|
| 163 |
+
return flask_thread
|
| 164 |
+
|
| 165 |
+
except Exception as e:
|
| 166 |
+
logger.warning(f"Failed to start Flask API: {e}")
|
| 167 |
+
logger.info("Continuing without Flask API (Gradio will still work)")
|
| 168 |
+
return None
|
| 169 |
+
|
| 170 |
def main():
|
| 171 |
"""Main entry point for HF Spaces"""
|
| 172 |
logger.info("π Starting AI Research Assistant MVP")
|
|
|
|
| 177 |
if not hf_token:
|
| 178 |
logger.warning("HF_TOKEN not found in environment. Some features may be limited.")
|
| 179 |
|
| 180 |
+
# Start Flask API in background (for external integrations)
|
| 181 |
+
flask_thread = start_flask_background()
|
| 182 |
+
|
| 183 |
# Import the already-configured interface from app.py
|
| 184 |
# This imports the interface without triggering app.py's launch code
|
| 185 |
# (app.py's launch only runs when app.py is executed directly, not when imported)
|
|
|
|
| 211 |
|
| 212 |
logger.info("=" * 60)
|
| 213 |
logger.info("β
Application ready for launch")
|
| 214 |
+
if flask_thread:
|
| 215 |
+
logger.info("β Flask API running in background")
|
| 216 |
logger.info("=" * 60)
|
| 217 |
return demo.launch(**launch_config)
|
| 218 |
|