File size: 944 Bytes
f2a2588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from json_repair import repair_json
import json

class PostProcessor:

    def process(self, response: str) -> dict:
        json_response = {}
        try:
            # Extract the JSON from the generated text.  Handle variations in output format.
            json_string = response
            if "```json" in response:
                json_string = response.split("```json")[1].split("```")[0]
            elif "{" in response and "}" in response:
                # try to grab the json
                start_index = response.find("{")
                end_index = response.rfind("}") + 1
                json_string = response[start_index:end_index]

            json_response = json.loads(repair_json(json_string)) # Added for robustness
        except Exception as e:
            print(f"Error parsing JSON: {e}")
            print(f"Generated text: {response}")
            json_response = {}

            
        return json_response