Last active
September 6, 2026 23:32
-
-
Save sachadee/20de580a4efb927102192fbdb1689c3b to your computer and use it in GitHub Desktop.
io.BytesIO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io | |
| import base64 | |
| import requests | |
| import onnxruntime as ort | |
| from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | |
| from cryptography.hazmat.backends import default_backend | |
| SERVER_URL = "http://127.0.0.1:8000" | |
| def decrypt_data(encryptedModel, decryptKey) -> bytes: | |
| if isinstance(decryptKey, str): | |
| decryptKey = decryptKey.encode('utf-8') | |
| data = base64.b64decode(encryptedModel) | |
| iv, tag, encrypted = data[:16], data[16:32], data[32:] | |
| cipher = Cipher(algorithms.AES(decryptKey), modes.GCM(iv, tag), backend=default_backend()) | |
| decryptor = cipher.decryptor() | |
| return decryptor.update(encrypted) + decryptor.finalize() | |
| def getModel(decryptKey, apiKey, onnxConfig, modelName): | |
| payload_decrypt_str = {"apiKey": apiKey, "decryptKey": decryptKey, "modelName": modelName} | |
| resp = requests.post(f"{SERVER_URL}/getEncryptedModel", json=payload_decrypt_str) | |
| if resp.status_code != 200: | |
| raise RuntimeError(f"Erro ao buscar modelo do servidor: {resp.json().get('detail')}") | |
| encryptedModel = resp.json().get("data") | |
| decrypted_data = decrypt_data(encryptedModel, decryptKey) | |
| model_io = io.BytesIO(decrypted_data) | |
| session = ort.InferenceSession(model_io.getvalue(), onnxConfig) | |
| return session # Disponível para o cliente no script principal como dll.getModel(...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment