import re from rest_framework.exceptions import PermissionDenied, ValidationError from rest_framework.serializers import UUIDField from rest_framework.views import APIView from common.model_runtime_client import verify_request from common.auth.authenticate import AnonymousAuthentication from common.result import result from local_model.runtime import manager class LocalModelRuntime(APIView): authentication_classes = [AnonymousAuthentication] permission_classes = [] def post(self, request): try: data = verify_request(request.body, request.headers.get('X-Model-Runtime-Signature')) except (ValueError, TypeError): raise PermissionDenied('模型服务认证失败') model_id = str(UUIDField().run_validation(data.get('model_id'))) operation = data.get('operation') try: if operation == 'release': lease = data.get('lease', '') if not isinstance(lease, str) or not re.fullmatch('[0-9a-f]{32}', lease): raise ValueError('请求标识无效') manager.release(model_id, lease) return result.success({'released': True}) if operation == 'acquire': lease = manager.acquire(model_id, remote=True) try: return result.success({**manager.status(model_id), 'lease': lease}) except Exception: manager.release(model_id, lease) raise if operation == 'load': manager.submit(model_id) elif operation == 'unload': manager.unload(model_id) elif operation == 'configure': manager.configure(model_id) elif operation != 'status': raise ValueError('模型服务操作无效') return result.success(manager.status(model_id)) except (ValueError, TimeoutError) as exc: raise ValidationError(str(exc))