Spaces:
Runtime error
Runtime error
Download src/data/components/data_preprocessing.py from Logisx/EssayEvaluation: direct link, hf CLI and curl.
- Browser
- Download file 1.91 kB
-
https://huggingface.co/spaces/Logisx/EssayEvaluation/resolve/main/src/data/components/data_preprocessing.py
- Command line
-
hf download hf://spaces/Logisx/EssayEvaluation/src/data/components/data_preprocessing.py
-
curl -L -o data_preprocessing.py https://huggingface.co/spaces/Logisx/EssayEvaluation/resolve/main/src/data/components/data_preprocessing.py
1.91 kB
| import os | |
| import pandas as pd | |
| from src.logging import logger | |
| from datasets import Dataset | |
| from src.data.entity import DataPreprocessingConfig | |
| class DataPreprocessing: | |
| def __init__(self, config: DataPreprocessingConfig): | |
| self.config = config | |
| def __form_finetuning_dataset(self, dataset_dict: dict, question_key: str, answer_key: str) -> Dataset: | |
| instruction_template = """{question}""" | |
| prompt_template = """Below is an instruction that describes a task. Write a response that appropriately completes the request. | |
| ### Instruction: | |
| {instruction} | |
| ### Response:\n""" | |
| num_samples = len(dataset_dict[question_key]) | |
| finetuning_dataset_list = [] | |
| for i in range(num_samples): | |
| question = dataset_dict[question_key][i] | |
| instruction = instruction_template.format(question=question) | |
| prompt = prompt_template.format(instruction=instruction) | |
| response = dataset_dict[answer_key][i] + "\n### End" | |
| text = prompt + response | |
| finetuning_dataset_list.append({"instruction": instruction, "response": response, "text": text}) | |
| finetuning_dataset = Dataset.from_list(finetuning_dataset_list) | |
| return finetuning_dataset | |
| def convert(self): | |
| instruction_dataset_df = pd.read_csv(self.config.raw_data_path) | |
| instruction_dataset_dict = instruction_dataset_df.to_dict() | |
| logger.info("Successfully loaded the raw dataset") | |
| finetuning_dataset = self.__form_finetuning_dataset(instruction_dataset_dict, question_key = self.config.question_key, answer_key = self.config.answer_key) | |
| finetuning_dataset_df = finetuning_dataset.to_pandas() | |
| finetuning_dataset_df.to_csv(os.path.join(self.config.root_dir,"finetuning_dataset.csv"), index=False) | |
| logger.info("Successfully saved the finetuning dataset") |