Categorizer Handlers

This module defines methods to perform content categorization using different techniques. It provides a base handler (BaseCategorizeHandler) for core logic and specialized handlers for Streamlit and FastAPI integration.

BaseCategorizeHandler

Abstract base class that implements categorization logic.

In evaluation mode, it accepts a list of evaluation techniques. In production mode it inspects the provided ground truth examples and then: - If there are enough training examples for a given category, uses Many Shot. - Else if there are a few examples available, uses Few Shot. - Otherwise, falls back to Zero Shot.

UI/transport-specific request parsing should be done in child classes.

Source code in LabeLMaker/categorize_handler.py
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
class BaseCategorizeHandler:
    """
    Abstract base class that implements categorization logic.

    In evaluation mode, it accepts a list of evaluation techniques.
    In production mode it inspects the provided ground truth examples and then:
      - If there are enough training examples for a given category, uses Many Shot.
      - Else if there are a few examples available, uses Few Shot.
      - Otherwise, falls back to Zero Shot.

    UI/transport-specific request parsing should be done in child classes.
    """

    def __init__(self, azure_key: Optional[str] = None) -> None:
        self.config = Config  # Expose configuration constants.
        self.azure_key = azure_key

    def _prepare_ground_truth_examples(
        self,
        df: pd.DataFrame,
        id_col: str,
        text_col: str,
        gt_col: str,
        few_shot_count: int = Config.MIN_SAMPLES_FEW_SHOT,
        many_shot_train_ratio: float = Config.MANY_SHOT_TRAIN_RATIO,
    ) -> Tuple[List[Example], Set[str], List[Example], Set[str]]:
        few_shot_examples: List[Example] = []
        few_shot_ids: Set[str] = set()
        many_shot_examples: List[Example] = []
        many_shot_test_ids: Set[str] = set()

        df_gt = df[[id_col, text_col, gt_col]].copy()
        df_gt[gt_col] = df_gt[gt_col].astype(str).str.lower()

        # Group by the ground truth label.
        grouped = df_gt.groupby(gt_col)
        for _, group in grouped:
            records = group.to_dict(orient="records")
            # Select a few-shot sample up to the provided count.
            count = min(few_shot_count, len(records))
            if count > 0:
                sampled = random.sample(records, count)
                for rec in sampled:
                    few_shot_examples.append(
                        Example(text_with_label=str(rec[text_col]), label=str(rec[gt_col]))
                    )
                    few_shot_ids.add(str(rec[id_col]))

            # Prepare many-shot data if there are multiple records.
            if len(records) > 1:
                shuffled = records.copy()
                random.shuffle(shuffled)
                train_size = max(1, int(many_shot_train_ratio * len(records)))
                train_examples = shuffled[:train_size]
                test_examples = shuffled[train_size:]
                for rec in train_examples:
                    many_shot_examples.append(
                        Example(text_with_label=str(rec[text_col]), label=str(rec[gt_col]))
                    )
                for rec in test_examples:
                    many_shot_test_ids.add(str(rec[id_col]))

        return few_shot_examples, few_shot_ids, many_shot_examples, many_shot_test_ids

    def categorize_data(
        self,
        df: pd.DataFrame,
        mode: str,
        index_column: Optional[str],
        text_column: str,
        ground_truth_column: str,
        examples_column: str,
        categories_dict: Dict[str, Any],
        zs_prompty: Path,
        fs_prompty: Path,
        evaluation_techniques: Optional[List[str]] = None,
        few_shot_count: int = Config.FEW_SHOT_COUNT,
        many_shot_train_ratio: float = Config.MANY_SHOT_TRAIN_RATIO,
    ) -> pd.DataFrame:
        """
        The heart of the abstract categorization logic.

        If mode is "evaluation", it prepares ground truth examples and then applies
        the chosen evaluation techniques.

        Otherwise (production mode), it selects among zero, few, or many shot modes depending
        on whether a ground truth column is provided and on the number of examples available.
        """
        # If no dedicated index is passed in, add one.
        if not index_column:
            df["index"] = df.index.astype(str)
            index_column = "index"

        # Prepare a list of texts and corresponding unique ids.
        text_to_label = df[text_column].astype(str).tolist()
        unique_ids = df[index_column].astype(str).tolist()

        if mode.lower() == "evaluation" and evaluation_techniques:
            # Evaluation mode: run all requested techniques (e.g., Zero Shot, Few Shot, Many Shot)
            categorization_request = CategoryManager.create_request(
                unique_ids, text_to_label, categories_dict
            )
            predictions = {}

            (
                few_shot_examples,
                few_shot_ids,
                many_shot_examples,
                many_shot_test_ids,
            ) = self._prepare_ground_truth_examples(
                df,
                index_column,
                text_column,
                ground_truth_column,
                few_shot_count,
                many_shot_train_ratio,
            )

            for tech in evaluation_techniques:
                if tech == "Zero Shot":
                    zs_categorizer = ZeroShotCategorizer(
                        prompty_path=zs_prompty, category_request=categorization_request
                    )
                    results = zs_categorizer.process()
                    predictions["Zero Shot"] = results
                elif tech == "Few Shot":
                    fs_request = CategoryManager.create_request(
                        unique_ids, text_to_label, categories_dict, few_shot_examples
                    )
                    fs_categorizer = FewShotCategorizer(
                        prompty_path=fs_prompty, category_request=fs_request
                    )
                    results = fs_categorizer.process()
                    # Remove any examples already in the few-shot gold set.
                    predictions["Few Shot"] = [r for r in results if str(r[0]) not in few_shot_ids]
                elif tech == "Many Shot":
                    ms_request = CategoryManager.create_request(
                        unique_ids, text_to_label, categories_dict, many_shot_examples
                    )
                    ms_categorizer = ManyshotClassifier(
                        categorization_request=ms_request,
                        min_class_count=self.config.MIN_SAMPLES_MANY_SHOT,
                    )
                    results = ms_categorizer.process()
                    predictions["Many Shot"] = [r for r in results if str(r[0]) in many_shot_test_ids]
                else:
                    raise ValueError(f"Unsupported technique '{tech}' in evaluation mode.")

            # Merge the results into the dataframe: one additional set of columns per technique.
            merged_df = df.copy()
            for technique, results in predictions.items():
                tech_pred_df = pd.DataFrame(
                    [(row[0], row[2], row[3]) for row in results],
                    columns=[index_column,
                             f"Predicted Category ({technique})",
                             f"Rationale ({technique})"],
                )
                merged_df[index_column] = merged_df[index_column].astype(str)
                tech_pred_df[index_column] = tech_pred_df[index_column].astype(str)
                merged_df = pd.merge(merged_df, tech_pred_df, on=index_column, how="left")
            return merged_df

        else:
            print("gt col - ", examples_column)
            print("df cols - ", df.columns)
            # Production mode: choose the most appropriate technique based on the provided examples.
            if examples_column and examples_column in df.columns:
                (
                    few_shot_examples,
                    few_shot_ids,
                    many_shot_examples,
                    many_shot_test_ids,
                ) = self._prepare_ground_truth_examples(
                    df,
                    index_column,
                    text_column,
                    examples_column,
                    few_shot_count,
                    many_shot_train_ratio,
                )
                print("FS Examples - ", few_shot_examples)
                print("MS Examples - ", few_shot_examples)
                # Prefer Many Shot if we have enough training examples.
                if len(many_shot_examples) >= self.config.MIN_SAMPLES_MANY_SHOT:
                    ms_request = CategoryManager.create_request(
                        unique_ids, text_to_label, categories_dict, many_shot_examples
                    )
                    ms_categorizer = ManyshotClassifier(
                        categorization_request=ms_request,
                        min_class_count=self.config.MIN_SAMPLES_MANY_SHOT,
                    )
                    predictions = ms_categorizer.process()
                    # In production, we assume predictions are for test examples only.
                    results = [r for r in predictions if str(r[0]) in many_shot_test_ids]
                # Else, try Few Shot if any examples exist.
                elif len(few_shot_examples) > 0:
                    fs_request = CategoryManager.create_request(
                        unique_ids, text_to_label, categories_dict, few_shot_examples
                    )
                    fs_categorizer = FewShotCategorizer(
                        prompty_path=fs_prompty, category_request=fs_request
                    )
                    predictions = fs_categorizer.process()
                    results = predictions
                else:
                    # Fallback to Zero Shot if no ground truth examples are available.
                    zs_request = CategoryManager.create_request(
                        unique_ids, text_to_label, categories_dict
                    )
                    zs_categorizer = ZeroShotCategorizer(
                        prompty_path=zs_prompty, category_request=zs_request
                    )
                    results = zs_categorizer.process()
            else:
                # No ground truth column provided: use Zero Shot as default.
                zs_request = CategoryManager.create_request(
                    unique_ids, text_to_label, categories_dict
                )
                zs_categorizer = ZeroShotCategorizer(
                    prompty_path=zs_prompty, category_request=zs_request
                )
                results = zs_categorizer.process()

            # In production we assume a single set of predicted results.
            results_df = pd.DataFrame(
                [(row[0], row[2], row[3]) for row in results],
                columns=[index_column, "Category", "Rationale"],
            )
            # Ensure columns are strings for a proper merge.
            df[index_column] = df[index_column].astype(str)
            results_df[index_column] = results_df[index_column].astype(str)
            merged_df = pd.merge(df, results_df, on=index_column, how="left")

            final_columns = list(df.columns) + ["Category", "Rationale"]
            return merged_df[final_columns]

categorize_data(df, mode, index_column, text_column, ground_truth_column, examples_column, categories_dict, zs_prompty, fs_prompty, evaluation_techniques=None, few_shot_count=Config.FEW_SHOT_COUNT, many_shot_train_ratio=Config.MANY_SHOT_TRAIN_RATIO)

The heart of the abstract categorization logic.

If mode is "evaluation", it prepares ground truth examples and then applies the chosen evaluation techniques.

Otherwise (production mode), it selects among zero, few, or many shot modes depending on whether a ground truth column is provided and on the number of examples available.

Source code in LabeLMaker/categorize_handler.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
def categorize_data(
    self,
    df: pd.DataFrame,
    mode: str,
    index_column: Optional[str],
    text_column: str,
    ground_truth_column: str,
    examples_column: str,
    categories_dict: Dict[str, Any],
    zs_prompty: Path,
    fs_prompty: Path,
    evaluation_techniques: Optional[List[str]] = None,
    few_shot_count: int = Config.FEW_SHOT_COUNT,
    many_shot_train_ratio: float = Config.MANY_SHOT_TRAIN_RATIO,
) -> pd.DataFrame:
    """
    The heart of the abstract categorization logic.

    If mode is "evaluation", it prepares ground truth examples and then applies
    the chosen evaluation techniques.

    Otherwise (production mode), it selects among zero, few, or many shot modes depending
    on whether a ground truth column is provided and on the number of examples available.
    """
    # If no dedicated index is passed in, add one.
    if not index_column:
        df["index"] = df.index.astype(str)
        index_column = "index"

    # Prepare a list of texts and corresponding unique ids.
    text_to_label = df[text_column].astype(str).tolist()
    unique_ids = df[index_column].astype(str).tolist()

    if mode.lower() == "evaluation" and evaluation_techniques:
        # Evaluation mode: run all requested techniques (e.g., Zero Shot, Few Shot, Many Shot)
        categorization_request = CategoryManager.create_request(
            unique_ids, text_to_label, categories_dict
        )
        predictions = {}

        (
            few_shot_examples,
            few_shot_ids,
            many_shot_examples,
            many_shot_test_ids,
        ) = self._prepare_ground_truth_examples(
            df,
            index_column,
            text_column,
            ground_truth_column,
            few_shot_count,
            many_shot_train_ratio,
        )

        for tech in evaluation_techniques:
            if tech == "Zero Shot":
                zs_categorizer = ZeroShotCategorizer(
                    prompty_path=zs_prompty, category_request=categorization_request
                )
                results = zs_categorizer.process()
                predictions["Zero Shot"] = results
            elif tech == "Few Shot":
                fs_request = CategoryManager.create_request(
                    unique_ids, text_to_label, categories_dict, few_shot_examples
                )
                fs_categorizer = FewShotCategorizer(
                    prompty_path=fs_prompty, category_request=fs_request
                )
                results = fs_categorizer.process()
                # Remove any examples already in the few-shot gold set.
                predictions["Few Shot"] = [r for r in results if str(r[0]) not in few_shot_ids]
            elif tech == "Many Shot":
                ms_request = CategoryManager.create_request(
                    unique_ids, text_to_label, categories_dict, many_shot_examples
                )
                ms_categorizer = ManyshotClassifier(
                    categorization_request=ms_request,
                    min_class_count=self.config.MIN_SAMPLES_MANY_SHOT,
                )
                results = ms_categorizer.process()
                predictions["Many Shot"] = [r for r in results if str(r[0]) in many_shot_test_ids]
            else:
                raise ValueError(f"Unsupported technique '{tech}' in evaluation mode.")

        # Merge the results into the dataframe: one additional set of columns per technique.
        merged_df = df.copy()
        for technique, results in predictions.items():
            tech_pred_df = pd.DataFrame(
                [(row[0], row[2], row[3]) for row in results],
                columns=[index_column,
                         f"Predicted Category ({technique})",
                         f"Rationale ({technique})"],
            )
            merged_df[index_column] = merged_df[index_column].astype(str)
            tech_pred_df[index_column] = tech_pred_df[index_column].astype(str)
            merged_df = pd.merge(merged_df, tech_pred_df, on=index_column, how="left")
        return merged_df

    else:
        print("gt col - ", examples_column)
        print("df cols - ", df.columns)
        # Production mode: choose the most appropriate technique based on the provided examples.
        if examples_column and examples_column in df.columns:
            (
                few_shot_examples,
                few_shot_ids,
                many_shot_examples,
                many_shot_test_ids,
            ) = self._prepare_ground_truth_examples(
                df,
                index_column,
                text_column,
                examples_column,
                few_shot_count,
                many_shot_train_ratio,
            )
            print("FS Examples - ", few_shot_examples)
            print("MS Examples - ", few_shot_examples)
            # Prefer Many Shot if we have enough training examples.
            if len(many_shot_examples) >= self.config.MIN_SAMPLES_MANY_SHOT:
                ms_request = CategoryManager.create_request(
                    unique_ids, text_to_label, categories_dict, many_shot_examples
                )
                ms_categorizer = ManyshotClassifier(
                    categorization_request=ms_request,
                    min_class_count=self.config.MIN_SAMPLES_MANY_SHOT,
                )
                predictions = ms_categorizer.process()
                # In production, we assume predictions are for test examples only.
                results = [r for r in predictions if str(r[0]) in many_shot_test_ids]
            # Else, try Few Shot if any examples exist.
            elif len(few_shot_examples) > 0:
                fs_request = CategoryManager.create_request(
                    unique_ids, text_to_label, categories_dict, few_shot_examples
                )
                fs_categorizer = FewShotCategorizer(
                    prompty_path=fs_prompty, category_request=fs_request
                )
                predictions = fs_categorizer.process()
                results = predictions
            else:
                # Fallback to Zero Shot if no ground truth examples are available.
                zs_request = CategoryManager.create_request(
                    unique_ids, text_to_label, categories_dict
                )
                zs_categorizer = ZeroShotCategorizer(
                    prompty_path=zs_prompty, category_request=zs_request
                )
                results = zs_categorizer.process()
        else:
            # No ground truth column provided: use Zero Shot as default.
            zs_request = CategoryManager.create_request(
                unique_ids, text_to_label, categories_dict
            )
            zs_categorizer = ZeroShotCategorizer(
                prompty_path=zs_prompty, category_request=zs_request
            )
            results = zs_categorizer.process()

        # In production we assume a single set of predicted results.
        results_df = pd.DataFrame(
            [(row[0], row[2], row[3]) for row in results],
            columns=[index_column, "Category", "Rationale"],
        )
        # Ensure columns are strings for a proper merge.
        df[index_column] = df[index_column].astype(str)
        results_df[index_column] = results_df[index_column].astype(str)
        merged_df = pd.merge(df, results_df, on=index_column, how="left")

        final_columns = list(df.columns) + ["Category", "Rationale"]
        return merged_df[final_columns]

FastAPICategorizeHandler

Bases: BaseCategorizeHandler

Provides categorization functionality for FastAPI endpoints.

Source code in LabeLMaker/categorize_handler.py
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
class FastAPICategorizeHandler(BaseCategorizeHandler):
    """
    Provides categorization functionality for FastAPI endpoints.
    """

    def __init__(self, azure_key: Optional[str] = None) -> None:
        super().__init__(azure_key=azure_key)

    def fastapi_categorize(
        self, data: pd.DataFrame, request: Any, zs_prompty: Path, fs_prompty: Path
    ) -> pd.DataFrame:
        """
        Extract values from the FastAPI request and pass them to BaseCategorizeHandler.
        (Here we assume the request object carries attributes like index_column, text_column, etc.)
        """
        index_column = request.index_column
        text_column = request.text_column
        # ex_label_column may be empty or None.
        gt_column = request.ex_label_column if getattr(request, "ex_label_column", None) else ""
        examples_column=gt_column
        categories_dict = {cat.name: cat.description for cat in request.categories}

        return self.categorize_data(
            df=data,
            mode=request.mode,
            index_column=index_column,
            text_column=text_column,
            ground_truth_column=gt_column,
            examples_column=examples_column,
            categories_dict=categories_dict,
            zs_prompty=zs_prompty,
            fs_prompty=fs_prompty,
            evaluation_techniques=getattr(request, "model", None),  # Could be a list of techniques.
            few_shot_count=int(request.few_shot_count),
            many_shot_train_ratio=float(request.many_shot_train_ratio),
        )

fastapi_categorize(data, request, zs_prompty, fs_prompty)

Extract values from the FastAPI request and pass them to BaseCategorizeHandler. (Here we assume the request object carries attributes like index_column, text_column, etc.)

Source code in LabeLMaker/categorize_handler.py
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
def fastapi_categorize(
    self, data: pd.DataFrame, request: Any, zs_prompty: Path, fs_prompty: Path
) -> pd.DataFrame:
    """
    Extract values from the FastAPI request and pass them to BaseCategorizeHandler.
    (Here we assume the request object carries attributes like index_column, text_column, etc.)
    """
    index_column = request.index_column
    text_column = request.text_column
    # ex_label_column may be empty or None.
    gt_column = request.ex_label_column if getattr(request, "ex_label_column", None) else ""
    examples_column=gt_column
    categories_dict = {cat.name: cat.description for cat in request.categories}

    return self.categorize_data(
        df=data,
        mode=request.mode,
        index_column=index_column,
        text_column=text_column,
        ground_truth_column=gt_column,
        examples_column=examples_column,
        categories_dict=categories_dict,
        zs_prompty=zs_prompty,
        fs_prompty=fs_prompty,
        evaluation_techniques=getattr(request, "model", None),  # Could be a list of techniques.
        few_shot_count=int(request.few_shot_count),
        many_shot_train_ratio=float(request.many_shot_train_ratio),
    )

StreamlitCategorizeHandler

Bases: BaseCategorizeHandler

Thin wrapper for Streamlit usage. Expects that UI parameters (collected via the UI) are passed in a dictionary.

Source code in LabeLMaker/categorize_handler.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
class StreamlitCategorizeHandler(BaseCategorizeHandler):
    """
    Thin wrapper for Streamlit usage.
    Expects that UI parameters (collected via the UI) are passed in a dictionary.
    """

    def __init__(self, azure_key: Optional[str] = None) -> None:
        super().__init__(azure_key=azure_key)

    def streamlit_categorize(
        self,
        df: pd.DataFrame,
        ui_params: Dict[str, Any],
        zs_prompty: Path,
        fs_prompty: Path,
    ) -> pd.DataFrame:
        """
        Extract values from the Streamlit UI dictionary and pass them to BaseCategorizeHandler.
        """
        return self.categorize_data(
            df=df,
            mode=ui_params.get("mode", "production"),
            index_column=ui_params.get("index_column"),
            text_column=ui_params.get("categorizing_column"),
            ground_truth_column=ui_params.get("ground_truth_column", ""),
            examples_column=ui_params.get("examples_column",""),
            categories_dict=ui_params.get("categories_dict", {}),
            zs_prompty=zs_prompty,
            fs_prompty=fs_prompty,
            evaluation_techniques=ui_params.get("evaluation_techniques"),
            few_shot_count=ui_params.get("few_shot_count", Config.FEW_SHOT_COUNT),
            many_shot_train_ratio=ui_params.get(
                "many_shot_train_ratio", Config.MANY_SHOT_TRAIN_RATIO
            ),
        )

streamlit_categorize(df, ui_params, zs_prompty, fs_prompty)

Extract values from the Streamlit UI dictionary and pass them to BaseCategorizeHandler.

Source code in LabeLMaker/categorize_handler.py
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
def streamlit_categorize(
    self,
    df: pd.DataFrame,
    ui_params: Dict[str, Any],
    zs_prompty: Path,
    fs_prompty: Path,
) -> pd.DataFrame:
    """
    Extract values from the Streamlit UI dictionary and pass them to BaseCategorizeHandler.
    """
    return self.categorize_data(
        df=df,
        mode=ui_params.get("mode", "production"),
        index_column=ui_params.get("index_column"),
        text_column=ui_params.get("categorizing_column"),
        ground_truth_column=ui_params.get("ground_truth_column", ""),
        examples_column=ui_params.get("examples_column",""),
        categories_dict=ui_params.get("categories_dict", {}),
        zs_prompty=zs_prompty,
        fs_prompty=fs_prompty,
        evaluation_techniques=ui_params.get("evaluation_techniques"),
        few_shot_count=ui_params.get("few_shot_count", Config.FEW_SHOT_COUNT),
        many_shot_train_ratio=ui_params.get(
            "many_shot_train_ratio", Config.MANY_SHOT_TRAIN_RATIO
        ),
    )