import pandas as pd import os import datasets XLS_FOLDER = "woori" OUTPUT_FOLDER = "data" SORT_KEY = "어휘" NUM_PROC =32 hf_access_token = "YOUR_TOKEN" hf_ID = "YOUR_ID" ds_name = "woori_spring_dict" def flatten_examples(example: dict) -> dict: text_line = "" for key in example: # some columns are empty or invalid if key == "의미 번호": continue if (single_column := example[key]) == None: continue # certain columns contain extraneous content if key == "원어·어종": single_column = single_column.removeprefix("안 밝힘 ") text_line += key + ": " + single_column.strip() + ", " return {"text": text_line.removesuffix(", ")} if os.path.exists(XLS_FOLDER): XLS_FOLDER = os.path.abspath(XLS_FOLDER) xls_list = os.listdir(XLS_FOLDER) else: raise ValueError("input folder does not exist") combined_df = pd.DataFrame() length_check = 0 for xls in sorted(xls_list): xls_path = os.path.join(XLS_FOLDER, xls) if os.path.exists(xls_path): # print(xls_path) df = pd.read_excel(xls_path, header=0, index_col=None) length_check += len(df) combined_df = pd.concat([combined_df, df], ignore_index=True) assert len(combined_df) == length_check ds = datasets.Dataset.from_pandas(combined_df) sorted_ds = ds.sort(SORT_KEY) processed_ds = sorted_ds.map(flatten_examples, num_proc=NUM_PROC).select_columns("text") processed_ds.push_to_hub(repo_id=ds_name, token=hf_access_token) # processed_ds.to_json("./woorimal.jsonl",num_proc=32,force_ascii=False,indent=1)