Spaces:
Sleeping
Sleeping
Download app/json_utils.py from mjpsm/activity-progression-api: direct link, hf CLI and curl.
- Browser
- Download file 507 Bytes
-
https://huggingface.co/spaces/mjpsm/activity-progression-api/resolve/main/app/json_utils.py
- Command line
-
hf download hf://spaces/mjpsm/activity-progression-api/app/json_utils.py
-
curl -L -o json_utils.py https://huggingface.co/spaces/mjpsm/activity-progression-api/resolve/main/app/json_utils.py
507 Bytes
| import json | |
| import re | |
| def extract_json(text: str) -> dict | None: | |
| """ | |
| Match the JSON extraction logic used during checkpoint evaluation: | |
| find the first decodable JSON object in the generated text. | |
| """ | |
| decoder = json.JSONDecoder() | |
| for match in re.finditer(r"\{", text): | |
| try: | |
| obj, _ = decoder.raw_decode(text[match.start():]) | |
| if isinstance(obj, dict): | |
| return obj | |
| except json.JSONDecodeError: | |
| pass | |
| return None | |