mirror of
https://github.com/bunny-lab-io/Borealis.git
synced 2025-07-27 06:08:28 -06:00
Deprecated Old Flask Site Hosting
This commit is contained in:
@ -4,25 +4,18 @@ import eventlet
|
|||||||
# Monkey-patch stdlib for cooperative sockets
|
# Monkey-patch stdlib for cooperative sockets
|
||||||
eventlet.monkey_patch()
|
eventlet.monkey_patch()
|
||||||
|
|
||||||
from flask import Flask, request, jsonify, send_from_directory, Response
|
from flask import Flask, request, jsonify, Response
|
||||||
from flask_socketio import SocketIO, emit
|
from flask_socketio import SocketIO, emit
|
||||||
|
|
||||||
import time
|
import time
|
||||||
import os
|
|
||||||
import base64
|
|
||||||
|
|
||||||
# Borealis Python API Endpoints
|
# Borealis Python API Endpoints
|
||||||
from Python_API_Endpoints.ocr_engines import run_ocr_on_base64
|
from Python_API_Endpoints.ocr_engines import run_ocr_on_base64
|
||||||
|
|
||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
# React Frontend Hosting Configuration
|
# Flask + WebSocket Server Configuration
|
||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
build_folder = os.path.join(os.getcwd(), "web-interface", "build")
|
app = Flask(__name__)
|
||||||
if not os.path.exists(build_folder):
|
|
||||||
print("WARNING: web-interface build folder not found. Please build your React app.")
|
|
||||||
|
|
||||||
app = Flask(__name__, static_folder=build_folder, static_url_path="/")
|
|
||||||
# Use Eventlet, switch async mode, and raise HTTP buffer size to ~100 MB
|
|
||||||
socketio = SocketIO(
|
socketio = SocketIO(
|
||||||
app,
|
app,
|
||||||
cors_allowed_origins="*",
|
cors_allowed_origins="*",
|
||||||
@ -33,25 +26,18 @@ socketio = SocketIO(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@app.route("/")
|
# ---------------------------------------------
|
||||||
def serve_index():
|
# Health Check Endpoint
|
||||||
index_path = os.path.join(build_folder, "index.html")
|
# ---------------------------------------------
|
||||||
if os.path.exists(index_path):
|
@app.route("/health")
|
||||||
return send_from_directory(build_folder, "index.html")
|
def health():
|
||||||
return ("<h1>Borealis React App Code Not Found</h1>"
|
return jsonify({"status": "ok"})
|
||||||
"<p>Please re-deploy Borealis Workflow Automation Tool</p>"), 404
|
|
||||||
|
|
||||||
@app.route("/<path:path>")
|
|
||||||
def serve_react_app(path):
|
|
||||||
full_path = os.path.join(build_folder, path)
|
|
||||||
if os.path.exists(full_path):
|
|
||||||
return send_from_directory(build_folder, path)
|
|
||||||
return send_from_directory(build_folder, "index.html")
|
|
||||||
|
|
||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
# Borealis Python API Endpoints
|
# Borealis Python API Endpoints
|
||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
# Process image data into OCR text output.
|
# /api/ocr: Accepts a base64 image and OCR engine selection,
|
||||||
|
# and returns extracted text lines.
|
||||||
@app.route("/api/ocr", methods=["POST"])
|
@app.route("/api/ocr", methods=["POST"])
|
||||||
def ocr_endpoint():
|
def ocr_endpoint():
|
||||||
payload = request.get_json()
|
payload = request.get_json()
|
||||||
@ -59,7 +45,6 @@ def ocr_endpoint():
|
|||||||
engine = payload.get("engine", "tesseract").lower().strip()
|
engine = payload.get("engine", "tesseract").lower().strip()
|
||||||
backend = payload.get("backend", "cpu").lower().strip()
|
backend = payload.get("backend", "cpu").lower().strip()
|
||||||
|
|
||||||
# Normalize engine aliases
|
|
||||||
if engine in ["tesseractocr", "tesseract"]:
|
if engine in ["tesseractocr", "tesseract"]:
|
||||||
engine = "tesseract"
|
engine = "tesseract"
|
||||||
elif engine == "easyocr":
|
elif engine == "easyocr":
|
||||||
@ -76,6 +61,7 @@ def ocr_endpoint():
|
|||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
# Borealis Agent API Endpoints
|
# Borealis Agent API Endpoints
|
||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
|
# These endpoints handle agent registration, provisioning, and image streaming.
|
||||||
registered_agents = {}
|
registered_agents = {}
|
||||||
agent_configurations = {}
|
agent_configurations = {}
|
||||||
latest_images = {}
|
latest_images = {}
|
||||||
@ -88,28 +74,24 @@ def get_agents():
|
|||||||
def provision_agent():
|
def provision_agent():
|
||||||
data = request.json
|
data = request.json
|
||||||
agent_id = data.get("agent_id")
|
agent_id = data.get("agent_id")
|
||||||
roles = data.get("roles", []) # <- MODULAR ROLES ARRAY
|
roles = data.get("roles", [])
|
||||||
|
|
||||||
if not agent_id or not isinstance(roles, list):
|
if not agent_id or not isinstance(roles, list):
|
||||||
return jsonify({"error": "Missing agent_id or roles[] in provision payload."}), 400
|
return jsonify({"error": "Missing agent_id or roles[] in provision payload."}), 400
|
||||||
|
|
||||||
# Save configuration
|
|
||||||
config = {"roles": roles}
|
config = {"roles": roles}
|
||||||
agent_configurations[agent_id] = config
|
agent_configurations[agent_id] = config
|
||||||
|
|
||||||
# Update status if agent already registered
|
|
||||||
if agent_id in registered_agents:
|
if agent_id in registered_agents:
|
||||||
registered_agents[agent_id]["status"] = "provisioned"
|
registered_agents[agent_id]["status"] = "provisioned"
|
||||||
|
|
||||||
# Emit config to the agent
|
|
||||||
socketio.emit("agent_config", config)
|
socketio.emit("agent_config", config)
|
||||||
|
|
||||||
return jsonify({"status": "provisioned", "roles": roles})
|
return jsonify({"status": "provisioned", "roles": roles})
|
||||||
|
|
||||||
|
# ---------------------------------------------
|
||||||
# ----------------------------------------------
|
# Live Screenshot Viewer for Debugging
|
||||||
# Canvas Image Feed Viewer for Screenshot Agents
|
# ---------------------------------------------
|
||||||
# ----------------------------------------------
|
# Serves an HTML canvas that shows real-time screenshots from a given agent+node.
|
||||||
@app.route("/api/agent/<agent_id>/node/<node_id>/screenshot/live")
|
@app.route("/api/agent/<agent_id>/node/<node_id>/screenshot/live")
|
||||||
def screenshot_node_viewer(agent_id, node_id):
|
def screenshot_node_viewer(agent_id, node_id):
|
||||||
return f"""
|
return f"""
|
||||||
@ -166,7 +148,7 @@ def screenshot_node_viewer(agent_id, node_id):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
# WebSocket Events
|
# WebSocket Events for Real-Time Communication
|
||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
@socketio.on("agent_screenshot_task")
|
@socketio.on("agent_screenshot_task")
|
||||||
def receive_screenshot_task(data):
|
def receive_screenshot_task(data):
|
||||||
@ -178,7 +160,6 @@ def receive_screenshot_task(data):
|
|||||||
print("[WS] Screenshot task missing fields.")
|
print("[WS] Screenshot task missing fields.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Optional: Store for debugging
|
|
||||||
latest_images[f"{agent_id}:{node_id}"] = {
|
latest_images[f"{agent_id}:{node_id}"] = {
|
||||||
"image_base64": image,
|
"image_base64": image,
|
||||||
"timestamp": time.time()
|
"timestamp": time.time()
|
||||||
@ -190,7 +171,7 @@ def receive_screenshot_task(data):
|
|||||||
"image_base64": image
|
"image_base64": image
|
||||||
}, broadcast=True)
|
}, broadcast=True)
|
||||||
|
|
||||||
@socketio.on('connect_agent')
|
@socketio.on("connect_agent")
|
||||||
def connect_agent(data):
|
def connect_agent(data):
|
||||||
agent_id = data.get("agent_id")
|
agent_id = data.get("agent_id")
|
||||||
hostname = data.get("hostname", "unknown")
|
hostname = data.get("hostname", "unknown")
|
||||||
@ -203,14 +184,14 @@ def connect_agent(data):
|
|||||||
"status": "orphaned" if agent_id not in agent_configurations else "provisioned"
|
"status": "orphaned" if agent_id not in agent_configurations else "provisioned"
|
||||||
}
|
}
|
||||||
|
|
||||||
@socketio.on('request_config')
|
@socketio.on("request_config")
|
||||||
def send_agent_config(data):
|
def send_agent_config(data):
|
||||||
agent_id = data.get("agent_id")
|
agent_id = data.get("agent_id")
|
||||||
config = agent_configurations.get(agent_id)
|
config = agent_configurations.get(agent_id)
|
||||||
if config:
|
if config:
|
||||||
emit('agent_config', config)
|
emit("agent_config", config)
|
||||||
|
|
||||||
@socketio.on('screenshot')
|
@socketio.on("screenshot")
|
||||||
def receive_screenshot(data):
|
def receive_screenshot(data):
|
||||||
agent_id = data.get("agent_id")
|
agent_id = data.get("agent_id")
|
||||||
image = data.get("image_base64")
|
image = data.get("image_base64")
|
||||||
@ -222,12 +203,12 @@ def receive_screenshot(data):
|
|||||||
}
|
}
|
||||||
emit("new_screenshot", {"agent_id": agent_id, "image_base64": image}, broadcast=True)
|
emit("new_screenshot", {"agent_id": agent_id, "image_base64": image}, broadcast=True)
|
||||||
|
|
||||||
@socketio.on('disconnect')
|
@socketio.on("disconnect")
|
||||||
def on_disconnect():
|
def on_disconnect():
|
||||||
print("[WS] Agent disconnected")
|
print("[WS] Agent disconnected")
|
||||||
|
|
||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
# Server Start
|
# Server Launch
|
||||||
# ---------------------------------------------
|
# ---------------------------------------------
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import eventlet.wsgi
|
import eventlet.wsgi
|
||||||
|
@ -161,8 +161,7 @@ switch ($choice) {
|
|||||||
|
|
||||||
Write-Host "`nLaunching Borealis..." -ForegroundColor Green
|
Write-Host "`nLaunching Borealis..." -ForegroundColor Green
|
||||||
Write-Host "===================================================================================="
|
Write-Host "===================================================================================="
|
||||||
Write-Host "$($symbols.Running) Python Flask Server Started..."
|
Write-Host "$($symbols.Running) Python Flask API Server Started..."
|
||||||
Write-Host "$($symbols.Running) Preloading OCR Engines... Please be patient..."
|
|
||||||
|
|
||||||
& $py $server_py
|
& $py $server_py
|
||||||
Pop-Location
|
Pop-Location
|
||||||
|
Reference in New Issue
Block a user