| import os |
| import requests |
| from glob import glob |
| import time |
|
|
| from datasets import Dataset |
|
|
| langs = [ |
| "asm", |
| "awa", |
| |
| "ben", |
| "bho", |
| |
| "brx", |
| |
| "guj", |
| |
| "hin", |
| "kan", |
| "kha", |
| "kok", |
| |
| "mai", |
| "mal", |
| "mar", |
| "mni", |
| "nep", |
| |
| "ori", |
| "pan", |
| "pli", |
| |
| "san", |
| "sat", |
| |
| "snd", |
| "tam", |
| "tel", |
| "urd", |
| ] |
|
|
| HF_REPO = "sarvamai/tatoeba-indic" |
| BASE_URL = "https://object.pouta.csc.fi/Tatoeba-Challenge-v2023-09-26/{direction}.tar" |
|
|
| for lang in langs: |
| direction = f"eng-{lang}" |
| url1 = BASE_URL.format(direction=direction) |
| |
| response = requests.head(url1) |
| if response.status_code == 200: |
| print(f"File exists: {url1}") |
| download_url = url1 |
| else: |
| direction = f"{lang}-eng" |
| url2 = BASE_URL.format(direction=direction) |
| response = requests.head(url2) |
| if response.status_code == 200: |
| print(f"File exists: {url2}") |
| download_url = url2 |
| else: |
| raise ValueError(f"File does not exist: {url1} or {url2}") |
| |
| file_name = download_url.split("/")[-1] |
| if not os.path.exists(file_name): |
| |
| os.system(f"wget {download_url}") |
|
|
| |
| os.system(f"tar -xvf {file_name}") |
|
|
| src_lang, tgt_lang = direction.split("-") |
|
|
| sets = ["test", "dev"] |
|
|
| for split in sets: |
| src_files = glob(f"data/release/**/{direction}/{split}.src") |
| if not src_files: |
| print(f"No {split} src files found for {direction}") |
| continue |
|
|
| tgt_files = glob(f"data/release/**/{direction}/{split}.trg") |
|
|
| src_lines, tgt_lines = [], [] |
| for src_line in open(src_files[0]): |
| src_lines.append(src_line.strip()) |
|
|
| for tgt_line in open(tgt_files[0]): |
| tgt_lines.append(tgt_line.strip()) |
|
|
| assert len(src_lines) == len(tgt_lines), f"Lengths of {split} src and tgt files are not equal for {direction}" |
|
|
| if src_lang != "eng": |
| |
| src_lines, tgt_lines = tgt_lines, src_lines |
|
|
| dataset = Dataset.from_dict({"src": src_lines, "tgt": tgt_lines}) |
| dataset.push_to_hub(HF_REPO, lang, split=split, commit_message=f"Add {split} split for {lang} language"); time.sleep(2) |
|
|