Delete scripts/prepare_datasets.py with huggingface_hub
Browse files- scripts/prepare_datasets.py +0 -130
scripts/prepare_datasets.py
DELETED
|
@@ -1,130 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Prepare datasets for EthioBBPE tokenizer training.
|
| 4 |
-
Extracts text from parquet files and creates combined training corpus.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
import pandas as pd
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
import argparse
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def extract_text_from_parquet(parquet_path: str, text_columns: list = None) -> list:
|
| 13 |
-
"""
|
| 14 |
-
Extract text from a parquet file.
|
| 15 |
-
|
| 16 |
-
Args:
|
| 17 |
-
parquet_path: Path to parquet file
|
| 18 |
-
text_columns: List of column names to extract. If None, auto-detect text columns.
|
| 19 |
-
|
| 20 |
-
Returns:
|
| 21 |
-
List of text strings
|
| 22 |
-
"""
|
| 23 |
-
df = pd.read_parquet(parquet_path)
|
| 24 |
-
|
| 25 |
-
if text_columns is None:
|
| 26 |
-
# Auto-detect text columns (exclude numeric columns)
|
| 27 |
-
text_columns = [col for col in df.columns if df[col].dtype == 'object']
|
| 28 |
-
print(f"Auto-detected text columns: {text_columns}")
|
| 29 |
-
|
| 30 |
-
texts = []
|
| 31 |
-
for col in text_columns:
|
| 32 |
-
if col in df.columns:
|
| 33 |
-
# Drop NaN values and convert to string
|
| 34 |
-
col_texts = df[col].dropna().astype(str).tolist()
|
| 35 |
-
texts.extend(col_texts)
|
| 36 |
-
print(f" - Column '{col}': {len(col_texts)} texts")
|
| 37 |
-
|
| 38 |
-
return texts
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def prepare_datasets(output_dir: str = "./data", output_filename: str = "combined_corpus.txt"):
|
| 42 |
-
"""
|
| 43 |
-
Prepare synaxarium and canon biblical datasets for training.
|
| 44 |
-
"""
|
| 45 |
-
output_path = Path(output_dir)
|
| 46 |
-
output_path.mkdir(parents=True, exist_ok=True)
|
| 47 |
-
|
| 48 |
-
all_texts = []
|
| 49 |
-
|
| 50 |
-
# Process Synaxarium dataset
|
| 51 |
-
synaxarium_path = output_path / "synaxarium_dataset.parquet"
|
| 52 |
-
if synaxarium_path.exists():
|
| 53 |
-
print(f"\n=== Processing Synaxarium Dataset ===")
|
| 54 |
-
print(f"File: {synaxarium_path}")
|
| 55 |
-
texts = extract_text_from_parquet(str(synaxarium_path), text_columns=['መጽሃፍ'])
|
| 56 |
-
all_texts.extend(texts)
|
| 57 |
-
print(f"Total texts from Synaxarium: {len(texts)}")
|
| 58 |
-
else:
|
| 59 |
-
print(f"Warning: Synaxarium dataset not found at {synaxarium_path}")
|
| 60 |
-
|
| 61 |
-
# Process Canon Biblical dataset
|
| 62 |
-
canon_path = output_path / "canon_biblical_am_en.parquet"
|
| 63 |
-
if canon_path.exists():
|
| 64 |
-
print(f"\n=== Processing Canon Biblical Dataset ===")
|
| 65 |
-
print(f"File: {canon_path}")
|
| 66 |
-
# Extract both Amharic and English verses
|
| 67 |
-
texts = extract_text_from_parquet(str(canon_path), text_columns=['verse', 'ጥቅስ'])
|
| 68 |
-
all_texts.extend(texts)
|
| 69 |
-
print(f"Total texts from Canon Biblical: {len(texts)}")
|
| 70 |
-
else:
|
| 71 |
-
print(f"Warning: Canon Biblical dataset not found at {canon_path}")
|
| 72 |
-
|
| 73 |
-
if not all_texts:
|
| 74 |
-
print("\nError: No texts extracted from datasets!")
|
| 75 |
-
return None
|
| 76 |
-
|
| 77 |
-
# Write combined corpus
|
| 78 |
-
output_file = output_path / output_filename
|
| 79 |
-
print(f"\n=== Writing Combined Corpus ===")
|
| 80 |
-
print(f"Total texts: {len(all_texts)}")
|
| 81 |
-
|
| 82 |
-
with open(output_file, 'w', encoding='utf-8') as f:
|
| 83 |
-
for i, text in enumerate(all_texts):
|
| 84 |
-
# Clean text: remove extra whitespace, ensure single line
|
| 85 |
-
cleaned = ' '.join(text.split())
|
| 86 |
-
if cleaned.strip(): # Only write non-empty lines
|
| 87 |
-
f.write(cleaned + '\n')
|
| 88 |
-
|
| 89 |
-
# Calculate stats
|
| 90 |
-
file_size_mb = output_file.stat().st_size / (1024 * 1024)
|
| 91 |
-
print(f"Output file: {output_file}")
|
| 92 |
-
print(f"File size: {file_size_mb:.2f} MB")
|
| 93 |
-
print(f"Total lines: {len(all_texts)}")
|
| 94 |
-
|
| 95 |
-
# Sample preview
|
| 96 |
-
print(f"\n=== Sample Preview (first 5 lines) ===")
|
| 97 |
-
with open(output_file, 'r', encoding='utf-8') as f:
|
| 98 |
-
for i, line in enumerate(f):
|
| 99 |
-
if i >= 5:
|
| 100 |
-
break
|
| 101 |
-
preview = line.strip()[:100] + "..." if len(line.strip()) > 100 else line.strip()
|
| 102 |
-
print(f"{i+1}: {preview}")
|
| 103 |
-
|
| 104 |
-
return output_file
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
def main():
|
| 108 |
-
parser = argparse.ArgumentParser(description="Prepare datasets for tokenizer training")
|
| 109 |
-
parser.add_argument("--output_dir", type=str, default="./data",
|
| 110 |
-
help="Directory containing parquet files and output location")
|
| 111 |
-
parser.add_argument("--output_filename", type=str, default="combined_corpus.txt",
|
| 112 |
-
help="Name of output combined corpus file")
|
| 113 |
-
|
| 114 |
-
args = parser.parse_args()
|
| 115 |
-
|
| 116 |
-
output_file = prepare_datasets(
|
| 117 |
-
output_dir=args.output_dir,
|
| 118 |
-
output_filename=args.output_filename
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
if output_file:
|
| 122 |
-
print(f"\n✓ Data preparation complete!")
|
| 123 |
-
print(f"Ready for training with: python scripts/train_tokenizer.py --data_dir {args.output_dir}")
|
| 124 |
-
else:
|
| 125 |
-
print("\n✗ Data preparation failed!")
|
| 126 |
-
exit(1)
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
if __name__ == "__main__":
|
| 130 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|