|
| 1 | +import gzip |
| 2 | +import os |
| 3 | +from huggingface_hub import hf_hub_download |
| 4 | +import shutil |
| 5 | +from ... import AGENT_CACHE_DIR |
| 6 | + |
| 7 | +def download_tool_data(tool_name: str): |
| 8 | + """ |
| 9 | + This is used to download tool-related data. |
| 10 | + """ |
| 11 | + global AGENT_CACHE_DIR |
| 12 | + if tool_name == "asyncdense_retrieve": |
| 13 | + data_dir = os.path.join(AGENT_CACHE_DIR, "data", "search") |
| 14 | + corpus_file = os.path.join(data_dir, "wiki-18.jsonl") |
| 15 | + index_file = os.path.join(data_dir, "e5_Flat.index") |
| 16 | + if not os.path.exists(corpus_file): |
| 17 | + if not os.path.exists(os.path.join(data_dir, "wiki-18.jsonl.gz")): |
| 18 | + repo_id = "PeterJinGo/wiki-18-corpus" |
| 19 | + hf_hub_download( |
| 20 | + repo_id=repo_id, |
| 21 | + filename="wiki-18.jsonl.gz", |
| 22 | + repo_type="dataset", |
| 23 | + local_dir=data_dir, |
| 24 | + ) |
| 25 | + # Unzip the file |
| 26 | + print(f"Unzipping {os.path.join(data_dir, 'wiki-18.jsonl.gz')}") |
| 27 | + gz_path = os.path.join(data_dir, "wiki-18.jsonl.gz") |
| 28 | + if os.path.exists(gz_path): |
| 29 | + with gzip.open(gz_path, 'rb') as f_in, open(corpus_file, 'wb') as f_out: |
| 30 | + shutil.copyfileobj(f_in, f_out) |
| 31 | + |
| 32 | + if not os.path.exists(index_file): |
| 33 | + if not os.path.exists(os.path.join(data_dir, "part_aa")): |
| 34 | + repo_id = "PeterJinGo/wiki-18-e5-index" |
| 35 | + for file in ["part_aa", "part_ab"]: |
| 36 | + hf_hub_download( |
| 37 | + repo_id=repo_id, |
| 38 | + filename=file, # e.g., "e5_Flat.index" |
| 39 | + repo_type="dataset", |
| 40 | + local_dir=data_dir, |
| 41 | + ) |
| 42 | + print(f"Concatenating {os.path.join(data_dir, 'part_*')} > {os.path.join(data_dir, 'e5_Flat.index')}") |
| 43 | + os.system(f"cat {os.path.join(data_dir, 'part_*')} > {os.path.join(data_dir, 'e5_Flat.index')}") |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + download_tool_data("asyncdense_retrieve") |
0 commit comments