Hammad712 commited on
Commit
b0b0d13
·
verified ·
1 Parent(s): 127a3b7

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +15 -0
utils.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ def extract_json_from_output(output_str: str) -> dict:
4
+ """
5
+ Extracts a JSON object from a string containing extra text.
6
+ """
7
+ start = output_str.find('{')
8
+ end = output_str.rfind('}')
9
+ if start == -1 or end == -1:
10
+ return None
11
+ json_str = output_str[start:end+1]
12
+ try:
13
+ return json.loads(json_str)
14
+ except json.JSONDecodeError:
15
+ return None