Spaces:
Runtime error
Runtime error
Download app/chunking.py from RamshaAnwar/contextquery-api: direct link, hf CLI and curl.
- Browser
- Download file 662 Bytes
-
https://huggingface.co/spaces/RamshaAnwar/contextquery-api/resolve/main/app/chunking.py
- Command line
-
hf download hf://spaces/RamshaAnwar/contextquery-api/app/chunking.py
-
curl -L -o chunking.py https://huggingface.co/spaces/RamshaAnwar/contextquery-api/resolve/main/app/chunking.py
662 Bytes
| def chunk_text(text: str, size_tokens: int = 500, overlap_tokens: int = 50) -> list[tuple[str, int]]: | |
| """Splits text into chunks of `size_tokens` with `overlap_tokens` overlap.""" | |
| if not text.strip(): | |
| return [] | |
| # Safety check: overlap must be strictly less than size | |
| if overlap_tokens >= size_tokens: | |
| overlap_tokens = size_tokens - 1 | |
| words = text.split() | |
| chunks = [] | |
| step = size_tokens - overlap_tokens | |
| i = 0 | |
| while i < len(words): | |
| chunk_words = words[i:i + size_tokens] | |
| chunks.append((" ".join(chunk_words), len(chunk_words))) | |
| i += step | |
| return chunks |