Spaces:
Sleeping
Sleeping
File size: 11,640 Bytes
01d5a5d |
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 |
import string
import secrets
import logging
import asyncio
import time
from flask import Blueprint, request, jsonify
from lpm_kernel.api.common.responses import APIResponse
from lpm_kernel.common.repository.database_session import DatabaseSession
from lpm_kernel.models.load import Load
from lpm_kernel.api.domains.loads.load_service import LoadService
from .client import RegistryClient
import threading
from lpm_kernel.api.domains.loads.dto import LoadDTO
from lpm_kernel.api.domains.trainprocess.training_params_manager import TrainingParamsManager
from lpm_kernel.file_data.document_service import document_service
from lpm_kernel.api.domains.upload.TrainingTags import TrainingTags
upload_bp = Blueprint("upload", __name__)
registry_client = RegistryClient()
logger = logging.getLogger(__name__)
@upload_bp.route("/api/upload/register", methods=["POST"])
def register_upload():
"""Register upload instance"""
try:
current_load, error, status_code = LoadService.get_current_load()
upload_name = current_load.name
instance_id = current_load.instance_id
email = current_load.email
description = current_load.description
params = TrainingParamsManager.get_latest_training_params()
model_name = params.get("model_name")
is_cot = params.get("is_cot")
document_count = len(document_service.list_documents())
tags = TrainingTags(
model_name=model_name,
is_cot=is_cot,
document_count=document_count
)
result = registry_client.register_upload(
upload_name, instance_id, description, email, tags
)
instance_id_new = result.get("instance_id")
if not instance_id_new:
return jsonify(APIResponse.error(
code=400, message="Failed to register upload instance"
))
instance_password = result.get("instance_password")
LoadService.update_instance_credentials(instance_id_new, instance_password)
return jsonify(APIResponse.success(
data=result
))
except Exception as e:
logger.error(f"An error occurred: {str(e)}", exc_info=True)
return jsonify(APIResponse.error(
code=500, message=f"An error occurred: {str(e)}"
))
@upload_bp.route("/api/upload/connect", methods=["POST"])
async def connect_upload():
"""
Establish WebSocket connection for the specified Upload instance
URL parameters:
instance_id: Instance ID
upload_name: Upload name
Returns:
{
"code": int,
"message": str,
"data": {
"ws_url": str # WebSocket connection URL
}
}
"""
try:
logger.info("Starting WebSocket connection process...")
current_load, error, status_code = LoadService.get_current_load(with_password=True)
if error:
return jsonify(APIResponse.error(
code=status_code, message=error
))
instance_id = current_load.instance_id
instance_password = current_load.instance_password
# Use thread to establish WebSocket connection asynchronously
def connect_ws():
asyncio.run(registry_client.connect_websocket(instance_id, instance_password))
thread = threading.Thread(target=connect_ws)
thread.daemon = True # Set as daemon thread, so it will end automatically when main program exits
thread.start()
result = {
"instance_id": instance_id,
"upload_name": current_load.name
}
return jsonify(APIResponse.success(
data=result,
message="WebSocket connection task started"
))
except Exception as e:
logger.error(f"Failed to establish WebSocket connection: {str(e)}")
return jsonify(APIResponse.error(
message=f"Failed to establish WebSocket connection: {str(e)}",
code=500
))
finally:
logger.info("WebSocket connection process completed.")
@upload_bp.route("/api/upload/status", methods=["GET"])
def get_upload_status():
"""
Get the status of the specified Upload instance
Returns:
{
"code": int,
"message": str,
"data": {
"upload_name": str,
"instance_id": str,
"status": str,
"description": str,
"email": str,
"is_connected": bool,
"last_ws_check": str,
"connection_alive": bool
}
}
"""
try:
current_load, error, status_code = LoadService.get_current_load()
if error:
return jsonify(APIResponse.error(
code=status_code, message=error
))
instance_id = current_load.instance_id
# Check if instance exists
detail = registry_client.get_upload_detail(instance_id)
logger.info(f"Upload status: {detail}")
# Get basic information from local
upload_data = {
"upload_name": current_load.name,
"instance_id": instance_id,
"description": current_load.description,
"email": current_load.email
}
# Process remote data, provide default values if null
if detail:
# Merge remote data
upload_data.update({
"status": "online" if detail.get("is_connected") else "offline",
"last_heartbeat": detail.get("last_heartbeat"),
"is_connected": detail.get("is_connected", False),
"last_ws_check": detail.get("last_ws_check")
})
else:
# Provide default values
upload_data.update({
"status": "unregistered",
"last_heartbeat": None,
"is_connected": False,
"last_ws_check": None
})
return jsonify(APIResponse.success(
data=upload_data,
message="Successfully retrieved Upload instance status"
))
except Exception as e:
logger.error(f"Failed to get Upload instance status: {str(e)}", exc_info=True)
return jsonify(APIResponse.error(
message=f"Failed to get status: {str(e)}",
code=500
))
@upload_bp.route("/api/upload", methods=["DELETE"])
def unregister_upload():
"""
API for unregistering Upload instance
URL parameters:
instance_id: Instance ID
upload_name: Upload name
Returns:
{
"code": int,
"message": str,
"data": {
"instance_id": str,
"upload_name": str
}
}
"""
try:
current_load, error, status_code = LoadService.get_current_load()
instance_id = current_load.instance_id
registry_client.unregister_upload(instance_id)
return jsonify(APIResponse.success(
data={
"instance_id": instance_id,
"upload_name": current_load.name
},
message="Upload instance unregistered successfully"
))
except Exception as e:
logger.error(f"Failed to unregister Upload: {str(e)}", exc_info=True)
return jsonify(APIResponse.error(
message=f"Unregistration failed: {str(e)}",
code=500
))
@upload_bp.route("/api/upload", methods=["GET"])
def list_uploads():
"""
List registered Upload instances with pagination and status filter
Query Parameters:
page_no (int): Page number, starting from 1
page_size (int): Number of items per page
status (List[str], optional): List of status to filter by
Returns:
{
"code": int,
"message": str,
"data": {
"instance_id": {
"upload_name": str,
"description": str,
"email": str,
"status": str
}
}
}
"""
try:
page_no = request.args.get("page_no", 1, type=int)
page_size = request.args.get("page_size", 10, type=int)
status = request.args.getlist("status")
result = registry_client.list_uploads(
page_no=page_no,
page_size=page_size,
status=status if status else None
)
return jsonify(APIResponse.success(
data=result,
message="Successfully retrieved Upload list"
))
except Exception as e:
logger.error(f"Failed to get Upload list: {str(e)}", exc_info=True)
return jsonify(APIResponse.error(
message=f"Failed to get list: {str(e)}",
code=500
))
@upload_bp.route("/api/upload/count", methods=["GET"])
def count_uploads():
"""
Get the number of registered Upload instances
Returns:
{
"code": int,
"message": str,
"data": {
"count": int
}
}
"""
try:
result = registry_client.count_uploads()
return jsonify(APIResponse.success(
data=result,
message="Successfully retrieved Upload count"
))
except Exception as e:
logger.error(f"Failed to get Upload count: {str(e)}", exc_info=True)
return jsonify(APIResponse.error(
message=f"Failed to get count: {str(e)}",
code=500
))
@upload_bp.route("/api/upload", methods=["PUT"])
def update_upload():
"""
API for updating Upload instance information
URL parameters:
instance_id: Instance ID
Request body:
{
"upload_name": str (optional),
"description": str (optional),
"email": str (optional)
}
Returns:
{
"code": int,
"message": str,
"data": {
"instance_id": str,
"upload_name": str,
"description": str,
"email": str,
"status": str
}
}
"""
try:
current_load, error, status_code = LoadService.get_current_load()
instance_id = current_load.instance_id
data = request.get_json()
if not data:
return jsonify(APIResponse.error(
message="Request body cannot be empty",
code=400
))
upload_name = data.get("upload_name")
description = data.get("description")
email = data.get("email")
# At least one update field is required
if upload_name is None and description is None and email is None:
return jsonify(APIResponse.error(
message="At least one update field is required: upload_name, description, or email",
code=400
))
result = registry_client.update_upload(
instance_id=instance_id,
upload_name=upload_name,
description=description,
email=email
)
return jsonify(APIResponse.success(
data=result,
message="Upload instance updated successfully"
))
except Exception as e:
logger.error(f"Failed to update Upload: {str(e)}", exc_info=True)
return jsonify(APIResponse.error(
message=f"Update failed: {str(e)}",
code=500
))
|