Spaces:
Sleeping
Sleeping
Download src/post_extraction_tools/jsonparser.py from Subhajit01/SmartLead: direct link, hf CLI and curl.
- Browser
- Download file 583 Bytes
-
https://huggingface.co/spaces/Subhajit01/SmartLead/resolve/main/src/post_extraction_tools/jsonparser.py
- Command line
-
hf download hf://spaces/Subhajit01/SmartLead/src/post_extraction_tools/jsonparser.py
-
curl -L -o jsonparser.py https://huggingface.co/spaces/Subhajit01/SmartLead/resolve/main/src/post_extraction_tools/jsonparser.py
583 Bytes
| import re | |
| import json | |
| class JSONOutputParser: | |
| """ | |
| Handles code fences like ```json or ````json. | |
| """ | |
| CODE_BLOCK_REGEX = re.compile(r"^\s*`{3,4}json\s*|\s*`{3,4}\s*$" ,flags=re.DOTALL) | |
| def parse(llm_output: str) -> object: | |
| cleaned = JSONOutputParser.CODE_BLOCK_REGEX.sub("", llm_output.strip()) | |
| cleaned = cleaned.strip() | |
| try: | |
| data = json.loads(cleaned) | |
| except json.JSONDecodeError as e: | |
| raise ValueError(f"Failed to decode JSON: {str(e)}\nRaw output:\n{llm_output}") | |
| return data | |