MCPcopy Create free account
hub / github.com/modelscope/FunASR / to_python

Function to_python

runtime/python/websocket/funasr_wss_server.py:17–38  ·  view source on GitHub ↗

递归地把 numpy / torch 等类型转成纯 Python,可 JSON 序列化。

(obj)

Source from the content-addressed store, hash-verified

15
16
17def to_python(obj):
18 """递归地把 numpy / torch 等类型转成纯 Python,可 JSON 序列化。"""
19 try:
20 import numpy as np # noqa
21 import torch # noqa
22 except Exception:
23 np = None
24 torch = None
25
26 if np is not None and isinstance(obj, np.generic):
27 return obj.item()
28 if np is not None and isinstance(obj, np.ndarray):
29 return obj.tolist()
30 if torch is not None and isinstance(obj, torch.Tensor):
31 return obj.cpu().tolist()
32
33 if isinstance(obj, dict):
34 return {k: to_python(v) for k, v in obj.items()}
35 if isinstance(obj, (list, tuple)):
36 return [to_python(v) for v in obj]
37
38 return obj
39
40
41parser = argparse.ArgumentParser()

Callers 1

async_asrFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…