Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/opengradient/client/_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,59 +112,67 @@
return number_tensors, string_tensors


def convert_to_model_output(event_data: AttributeDict) -> Dict[str, np.ndarray]:
"""
Converts inference output into a user-readable output.
Expects the inference node to return a dict with the format:
key: output_name (str)
value: (output_array (list), shape (list)) (tuple)

We need to reshape each output array using the shape parameter in order to get the array
back into its original shape.
"""
output_dict = {}
output = event_data.get("output", {})

if isinstance(output, (AttributeDict, dict)):
for tensor in output.get("numbers", []):
if isinstance(tensor, (AttributeDict, dict)):
name = tensor.get("name")
shape = tensor.get("shape")
values = []
# Convert from fixed point back into np.float32
for v in tensor.get("values", []):
if isinstance(v, (AttributeDict, dict)):
values.append(convert_to_float32(value=int(v.get("value")), decimals=int(v.get("decimals"))))
else:
logging.warning(f"Unexpected number type: {type(v)}")
output_dict[name] = np.array(values).reshape(shape)
else:
logging.warning(f"Unexpected tensor type: {type(tensor)}")

# Parse strings
for tensor in output.get("strings", []):
if isinstance(tensor, (AttributeDict, dict)):
name = tensor.get("name")
shape = tensor.get("shape")
values = tensor.get("values", [])
output_dict[name] = np.array(values).reshape(shape)
else:
logging.warning(f"Unexpected tensor type: {type(tensor)}")

# Parse JSON dicts
for tensor in output.get("jsons", []):
if isinstance(tensor, (AttributeDict, dict)):
name = tensor.get("name")
value = tensor.get("value")
output_dict[name] = np.array(json.loads(value))
if name is None:
logging.warning("JSON tensor is missing 'name' field; skipping.")
continue
try:
output_dict[name] = np.array(json.loads(value))
except (json.JSONDecodeError, TypeError) as e:
logging.warning(
f"Failed to parse JSON tensor '{name}' — value={value!r}: {e}. Skipping."
)
else:
logging.warning(f"Unexpected tensor type: {type(tensor)}")

else:
logging.warning(f"Unexpected output type: {type(output)}")

logging.debug(f"Parsed output: {output_dict}")
return output_dict

Check notice on line 175 in src/opengradient/client/_conversions.py

View check run for this annotation

codefactor.io / CodeFactor

src/opengradient/client/_conversions.py#L115-L175

Complex Method


def convert_array_to_model_output(array_data: List) -> ModelOutput:
Expand Down Expand Up @@ -203,7 +211,15 @@
for tensor in array_data[2]:
name = tensor[0]
value = tensor[1]
json_data[name] = np.array(json.loads(value))
if name is None:
logging.warning("JSON tensor entry is missing a name; skipping.")
continue
try:
json_data[name] = np.array(json.loads(value))
except (json.JSONDecodeError, TypeError) as e:
logging.warning(
f"Failed to parse JSON tensor '{name}' — value={value!r}: {e}. Skipping."
)

return ModelOutput(
numbers=number_data,
Expand Down
Loading