Spaces:
Running
on
T4
Running
on
T4
Commit
·
041d087
1
Parent(s):
6b8dbdd
Update
Browse files- opentools/models/utils.py +73 -0
opentools/models/utils.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import json
|
2 |
+
|
3 |
+
# def truncate_result(result, max_length: int = 100000, truncation_indicator: str = "...") -> str:
|
4 |
+
# """
|
5 |
+
# Truncate the result to specified length while preserving JSON structure when possible.
|
6 |
+
|
7 |
+
# Args:
|
8 |
+
# result: The result to truncate (can be str, list, dict, or other types)
|
9 |
+
# max_length: Maximum length of the output string (default: 1000)
|
10 |
+
# truncation_indicator: String to indicate truncation (default: "...")
|
11 |
+
|
12 |
+
# Returns:
|
13 |
+
# str: Truncated string representation of the result
|
14 |
+
# """
|
15 |
+
# if isinstance(result, (dict, list)):
|
16 |
+
# try:
|
17 |
+
# result_str = json.dumps(result, ensure_ascii=False)
|
18 |
+
# except:
|
19 |
+
# result_str = str(result)
|
20 |
+
# else:
|
21 |
+
# result_str = str(result)
|
22 |
+
|
23 |
+
# indicator_length = len(truncation_indicator)
|
24 |
+
|
25 |
+
# if len(result_str) > max_length:
|
26 |
+
# # For JSON-like strings, try to find the last complete structure
|
27 |
+
# if result_str.startswith('{') or result_str.startswith('['):
|
28 |
+
# # Find last complete element
|
29 |
+
# pos = max_length - indicator_length
|
30 |
+
# while pos > 0 and not (
|
31 |
+
# result_str[pos] in ',]}' and
|
32 |
+
# result_str[pos:].count('"') % 2 == 0
|
33 |
+
# ):
|
34 |
+
# pos -= 1
|
35 |
+
# if pos > 0:
|
36 |
+
# return result_str[:pos + 1] + truncation_indicator
|
37 |
+
|
38 |
+
# # Default truncation if not JSON or no suitable truncation point found
|
39 |
+
# return result_str[:max_length - indicator_length] + truncation_indicator
|
40 |
+
|
41 |
+
# return result_str
|
42 |
+
|
43 |
+
def make_json_serializable(obj):
|
44 |
+
if isinstance(obj, (str, int, float, bool, type(None))):
|
45 |
+
return obj
|
46 |
+
elif isinstance(obj, dict):
|
47 |
+
return {make_json_serializable(key): make_json_serializable(value) for key, value in obj.items()}
|
48 |
+
elif isinstance(obj, list):
|
49 |
+
return [make_json_serializable(element) for element in obj]
|
50 |
+
elif hasattr(obj, '__dict__'):
|
51 |
+
return make_json_serializable(obj.__dict__)
|
52 |
+
else:
|
53 |
+
return str(obj)
|
54 |
+
|
55 |
+
|
56 |
+
def make_json_serializable_truncated(obj, max_length: int = 100000):
|
57 |
+
if isinstance(obj, (int, float, bool, type(None))):
|
58 |
+
if isinstance(obj, (int, float)) and len(str(obj)) > max_length:
|
59 |
+
return str(obj)[:max_length - 3] + "..."
|
60 |
+
return obj
|
61 |
+
elif isinstance(obj, str):
|
62 |
+
return obj if len(obj) <= max_length else obj[:max_length - 3] + "..."
|
63 |
+
elif isinstance(obj, dict):
|
64 |
+
return {make_json_serializable_truncated(key, max_length): make_json_serializable_truncated(value, max_length)
|
65 |
+
for key, value in obj.items()}
|
66 |
+
elif isinstance(obj, list):
|
67 |
+
return [make_json_serializable_truncated(element, max_length) for element in obj]
|
68 |
+
elif hasattr(obj, '__dict__'):
|
69 |
+
return make_json_serializable_truncated(obj.__dict__, max_length)
|
70 |
+
else:
|
71 |
+
result = str(obj)
|
72 |
+
return result if len(result) <= max_length else result[:max_length - 3] + "..."
|
73 |
+
|