Spaces:
Sleeping
Sleeping
Download utils/text_utils.py from seed-image-ai/image_ai: direct link, hf CLI and curl.
- Browser
- Download file 792 Bytes
-
https://huggingface.co/spaces/seed-image-ai/image_ai/resolve/main/utils/text_utils.py
- Command line
-
hf download hf://spaces/seed-image-ai/image_ai/utils/text_utils.py
-
curl -L -o text_utils.py https://huggingface.co/spaces/seed-image-ai/image_ai/resolve/main/utils/text_utils.py
792 Bytes
| import re | |
| def clean_text( | |
| text | |
| ): | |
| return ( | |
| str(text) | |
| .lower() | |
| .strip() | |
| ) | |
| def normalize_list( | |
| values | |
| ): | |
| if values is None: | |
| return [] | |
| if isinstance( | |
| values, | |
| str | |
| ): | |
| values = [values] | |
| return [ | |
| clean_text(value) | |
| for value in values | |
| if str(value).strip() | |
| ] | |
| def extract_json_object( | |
| text | |
| ): | |
| text = text.strip() | |
| start = text.find("{") | |
| end = text.rfind("}") | |
| if start == -1 or end == -1: | |
| raise ValueError( | |
| "No JSON object found." | |
| ) | |
| return text[ | |
| start:end + 1 | |
| ] | |
| def tokenize_relation( | |
| text | |
| ): | |
| return set( | |
| re.findall( | |
| r"[a-z0-9_]+", | |
| clean_text(text) | |
| ) | |
| ) |