Fix Coordinator Concrete ML import error for Python 3.13
Some checks failed
Cross-Node Transaction Testing / transaction-test (push) Has been cancelled
Deploy to Testnet / deploy-testnet (push) Has been cancelled
Integration Tests / test-service-integration (push) Has been cancelled
Multi-Node Stress Testing / stress-test (push) Has been cancelled
Python Tests / test-python (push) Has been cancelled
Security Scanning / security-scan (push) Has been cancelled

Coordinator API:
- Changed FHEService from module-level instantiation to lazy loading
- Added get_fhe_service() function to instantiate on first use
- Updated fhe_ml_inference endpoint to use lazy-loaded service
- Prevents import-time Concrete ML errors on Python 3.13

Concrete ML requires Python <3.13, but current version is 3.13.5.
The FHEService gracefully handles the ImportError, but the module-level
instantiation was causing the error during app startup. Lazy loading
defers the instantiation until the endpoint is actually called.
This commit is contained in:
aitbc
2026-05-14 23:03:12 +02:00
parent 57f7330bee
commit 2ccf80ad5e

View File

@@ -10,7 +10,16 @@ from ....services.zk_proofs import ZKProofService
router = APIRouter(prefix="/v1/ml-zk", tags=["ml-zk"])
zk_service = ZKProofService()
fhe_service = FHEService()
# Lazy instantiation of FHEService to avoid import-time errors
_fhe_service: FHEService | None = None
def get_fhe_service() -> FHEService:
"""Get or create FHEService instance"""
global _fhe_service
if _fhe_service is None:
_fhe_service = FHEService()
return _fhe_service
@router.post("/prove/training")
@@ -105,6 +114,8 @@ async def verify_ml_inference(request: Request, verification_request: dict) -> d
async def fhe_ml_inference(request: Request, fhe_request: dict) -> dict[str, Any]:
"""Perform ML inference on encrypted data"""
try:
fhe_service = get_fhe_service()
# Setup FHE context
context = fhe_service.generate_fhe_context(
scheme=fhe_request.get("scheme", "ckks"), provider=fhe_request.get("provider", "tenseal")