|
| 1 | +""" |
| 2 | +This module defines transformations for reading and writing file content |
| 3 | +and an experiment pipeline configuration for generating docstrings. |
| 4 | +""" |
| 5 | + |
| 6 | +import os |
| 7 | +from dataclasses import dataclass |
| 8 | +from typing import Any |
| 9 | + |
| 10 | +from eureka_ml_insights.configs import ( |
| 11 | + DataSetConfig, |
| 12 | + ExperimentConfig, |
| 13 | + InferenceConfig, |
| 14 | + ModelConfig, |
| 15 | + PipelineConfig, |
| 16 | + PromptProcessingConfig, |
| 17 | +) |
| 18 | +from eureka_ml_insights.core import DataProcessing, Inference, PromptProcessing |
| 19 | +from eureka_ml_insights.data_utils import ( |
| 20 | + DataReader, |
| 21 | + DFTransformBase, |
| 22 | + MMDataLoader, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class FileReaderTransform(DFTransformBase): |
| 28 | + """ |
| 29 | + A transformation that reads file content from a specified file path column. |
| 30 | + """ |
| 31 | + |
| 32 | + file_path_column: str = "file_path" |
| 33 | + |
| 34 | + def transform(self, df): |
| 35 | + """ |
| 36 | + Transform the DataFrame by reading content based on file paths. |
| 37 | +
|
| 38 | + Args: |
| 39 | + df (pandas.DataFrame): The input DataFrame with the column specified |
| 40 | + by file_path_column. |
| 41 | +
|
| 42 | + Returns: |
| 43 | + pandas.DataFrame: The DataFrame with a new column 'file_content'. |
| 44 | + """ |
| 45 | + # Implement the logic to read files from the specified column |
| 46 | + df["file_content"] = df[self.file_path_column].apply(lambda x: open(x).read()) |
| 47 | + return df |
| 48 | + |
| 49 | + |
| 50 | +class FileWriterTransform(DFTransformBase): |
| 51 | + """ |
| 52 | + A transformation that writes file content to a specified file path. |
| 53 | + """ |
| 54 | + |
| 55 | + file_path_column: str = "file_path" |
| 56 | + file_content_column: str = "model_output" |
| 57 | + |
| 58 | + def transform(self, df): |
| 59 | + """ |
| 60 | + Transforms the DataFrame by writing file content to disk. |
| 61 | +
|
| 62 | + This method replaces certain path elements, creates output directories if needed, |
| 63 | + and writes the content from file_content_column to files specified by file_path_column. |
| 64 | +
|
| 65 | + Args: |
| 66 | + df (pandas.DataFrame): The input DataFrame containing file path and content columns. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + pandas.DataFrame: The original DataFrame after writing the content to disk. |
| 70 | + """ |
| 71 | + for index, row in df.iterrows(): |
| 72 | + with open(row[self.file_path_column], "w") as f: |
| 73 | + f.write(row[self.file_content_column]) |
| 74 | + return df |
| 75 | + |
| 76 | + |
| 77 | +class DOCSTR_PIPELINE(ExperimentConfig): |
| 78 | + """ |
| 79 | + An experiment configuration for a docstring generation pipeline. |
| 80 | + """ |
| 81 | + |
| 82 | + def configure_pipeline( |
| 83 | + self, model_config: ModelConfig, resume_from: str = None, **kwargs: dict[str, Any] |
| 84 | + ) -> PipelineConfig: |
| 85 | + """ |
| 86 | + Configure the pipeline components for docstring generation. |
| 87 | +
|
| 88 | + Args: |
| 89 | + model_config (ModelConfig): Configuration for the model. |
| 90 | + resume_from (str, optional): Path to a checkpoint to resume from. Defaults to None. |
| 91 | + **kwargs (dict[str, Any]): Additional keyword arguments. |
| 92 | +
|
| 93 | + Returns: |
| 94 | + PipelineConfig: The configured pipeline consisting of data processing, |
| 95 | + inference, and post-processing components. |
| 96 | + """ |
| 97 | + |
| 98 | + # input file should be a csv file with a column 'file_path' containing paths to Python files you want to add docstrings to. |
| 99 | + input_file_path = kwargs.get("input_file_path", os.path.join(os.path.dirname(__file__), "../python_file_list.csv")) |
| 100 | + |
| 101 | + # Configure the data processing component. |
| 102 | + data_processing_comp = PromptProcessingConfig( |
| 103 | + component_type=PromptProcessing, |
| 104 | + prompt_template_path=os.path.join(os.path.dirname(__file__), "../prompt_templates/doc_str.jinja"), |
| 105 | + data_reader_config=DataSetConfig( |
| 106 | + DataReader, |
| 107 | + { |
| 108 | + "path": input_file_path, |
| 109 | + "format": ".csv", |
| 110 | + "header": 0, |
| 111 | + "index_col": None, |
| 112 | + "transform": FileReaderTransform(file_path_column="file_path"), |
| 113 | + }, |
| 114 | + ), |
| 115 | + output_dir=os.path.join(self.log_dir, "data_processing_output"), |
| 116 | + ) |
| 117 | + inference_comp = InferenceConfig( |
| 118 | + component_type=Inference, |
| 119 | + model_config=model_config, |
| 120 | + data_loader_config=DataSetConfig( |
| 121 | + MMDataLoader, |
| 122 | + {"path": os.path.join(data_processing_comp.output_dir, "transformed_data.jsonl")}, |
| 123 | + ), |
| 124 | + output_dir=os.path.join(self.log_dir, "inference_output"), |
| 125 | + resume_from=resume_from, |
| 126 | + max_concurrent=5, |
| 127 | + ) |
| 128 | + post_process_comp = PromptProcessingConfig( |
| 129 | + component_type=DataProcessing, |
| 130 | + data_reader_config=DataSetConfig( |
| 131 | + DataReader, |
| 132 | + { |
| 133 | + "path": os.path.join(inference_comp.output_dir, "inference_result.jsonl"), |
| 134 | + "transform": FileWriterTransform(), |
| 135 | + }, |
| 136 | + ), |
| 137 | + output_dir=os.path.join(self.log_dir, "data_post_processing_output"), |
| 138 | + ) |
| 139 | + return PipelineConfig([data_processing_comp, inference_comp, post_process_comp]) |
0 commit comments