Skip to content

Instantly share code, notes, and snippets.

@cokeSchlumpf
Created October 5, 2023 15:55
Show Gist options
  • Save cokeSchlumpf/bbefb177fc14e2577fd07517cb5e55d7 to your computer and use it in GitHub Desktop.
Save cokeSchlumpf/bbefb177fc14e2577fd07517cb5e55d7 to your computer and use it in GitHub Desktop.
import json
from pathlib import Path
import numpy as np
import certifi
import ssl
import os
from typing import List
from zurich.space.security import add_zurichca
REQUEST_TIMEOUT = int(os.getenv("REQUEST_TIMEOUT", "600"))
existing_ca = Path(certifi.where()).resolve()
add_zurichca(existing_ca, add_space_ca=True)
sslcontext = ssl.create_default_context(cafile=existing_ca)
async def preidction_function(
input: np.Array,
url: str, # see URL von
method: str = "predict",
) -> List[List[int]]:
input_str = json.dumps(input.tolist())
# Validate that input_str is in form of [[1,2,3,43, ...], [12, 3, 4,, 6]]
headers = {"Content-Type": "application/json"}
async with aiohttp.ClientSession() as session:
async with session.post(
f"{url}/{method}",
headers=headers,
data=input_str,
timeout=REQUEST_TIMEOUT,
ssl=sslcontext,
) as response:
logger.info(f"Received response [{response.status}] from [{url}]")
data = await response.json()
# data should be in form of [[0], [1], [0], ...]
if response.status >= 400:
desc = "Error during processing"
raise Exception(f"{desc} in {url}:\n{data}")
return data
def alena_prediction_wrapper(pages: List[(Image, ScannedPage)]) -> List[Split]:
# Extract Features from pages
# -> Output is feature matrix which is send to prediction model -> np.Array
# Call prediction_function from above
# --> List of labels
# Transform labels into Splits
# return Splits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment