Spaces:
Paused
Paused
Download web2json/postprocessor.py from garage-lab/MCP_HTML2JSON: direct link, hf CLI and curl.
- Browser
- Download file 944 Bytes
-
https://huggingface.co/spaces/garage-lab/MCP_HTML2JSON/resolve/6fcfebce6e7e072e0a22ddd7fc6d90bc0af9aa30/web2json/postprocessor.py
- Command line
-
hf download hf://spaces/garage-lab/MCP_HTML2JSON@6fcfebce6e7e072e0a22ddd7fc6d90bc0af9aa30/web2json/postprocessor.py
-
curl -L -o postprocessor.py https://huggingface.co/spaces/garage-lab/MCP_HTML2JSON/resolve/6fcfebce6e7e072e0a22ddd7fc6d90bc0af9aa30/web2json/postprocessor.py
944 Bytes
| 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 | |