Download chat_data.py from Valeztiro/Micro-llm: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/Valeztiro/Micro-llm/resolve/main/chat_data.py
- Command line
-
hf download hf://Valeztiro/Micro-llm/chat_data.py
-
curl -L -o chat_data.py https://huggingface.co/Valeztiro/Micro-llm/resolve/main/chat_data.py
2.58 kB
| """Helpers for formatting Hugging Face chat datasets as training text.""" | |
| from __future__ import annotations | |
| import time | |
| from collections.abc import Iterator | |
| from typing import Any | |
| from datasets import load_dataset | |
| MAX_STREAM_RETRIES = 10 | |
| ROLE_LABELS = { | |
| "assistant": "Assistant", | |
| "system": "System", | |
| "user": "User", | |
| } | |
| def stream_hf_rows( | |
| dataset_name: str, split: str, config: str | None = None | |
| ) -> Iterator[dict[str, Any]]: | |
| processed_rows = 0 | |
| retries = 0 | |
| while True: | |
| try: | |
| load_args = (dataset_name,) if config is None else (dataset_name, config) | |
| dataset = load_dataset(*load_args, split=split, streaming=True) | |
| rows = iter(dataset) | |
| for _ in range(processed_rows): | |
| next(rows) | |
| except (OSError, ConnectionError, TimeoutError) as error: | |
| retries += 1 | |
| if retries > MAX_STREAM_RETRIES: | |
| raise | |
| wait_seconds = min(2**retries, 60) | |
| print( | |
| f"HF stream setup failed ({error}); reopening {split} and resuming " | |
| f"after {processed_rows:,} rows in {wait_seconds}s " | |
| f"[{retries}/{MAX_STREAM_RETRIES}]" | |
| ) | |
| time.sleep(wait_seconds) | |
| continue | |
| while True: | |
| try: | |
| row = next(rows) | |
| except StopIteration: | |
| return | |
| except (OSError, ConnectionError, TimeoutError) as error: | |
| retries += 1 | |
| if retries > MAX_STREAM_RETRIES: | |
| raise | |
| wait_seconds = min(2**retries, 60) | |
| print( | |
| f"HF stream interrupted ({error}); reopening {split} and resuming " | |
| f"after {processed_rows:,} rows in {wait_seconds}s " | |
| f"[{retries}/{MAX_STREAM_RETRIES}]" | |
| ) | |
| time.sleep(wait_seconds) | |
| break | |
| processed_rows += 1 | |
| retries = 0 | |
| yield row | |
| def format_conversation(row: dict[str, Any]) -> str | None: | |
| if str(row.get("category", "")).lower() == "generation": | |
| return None | |
| messages = row.get("messages") | |
| if not isinstance(messages, list): | |
| return None | |
| system_message: str | None = None | |
| user_message: str | None = None | |
| for message in messages: | |
| if not isinstance(message, dict): | |
| return None | |
| role = ROLE_LABELS.get(str(message.get("role", "")).lower()) | |
| content = message.get("content") | |
| if role is None or not isinstance(content, str) or not content.strip(): | |
| return None | |
| content = content.strip() | |
| if role == "System" and system_message is None: | |
| system_message = content | |
| elif role == "User": | |
| user_message = content | |
| elif role == "Assistant" and user_message is not None: | |
| turns = [] | |
| if system_message is not None: | |
| turns.append(f"System: {system_message}") | |
| turns.extend((f"User: {user_message}", f"Assistant: {content}")) | |
| return "\n".join(turns) | |
| return None |