File size: 978 Bytes
a8a9d46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# processing_yield.py

import json
from transformers.processing_utils import ProcessorMixin


class YieldProcessor(ProcessorMixin):
    attributes = []
    valid_kwargs = []

    def __init__(self, **kwargs):
        super().__init__()

    def __call__(self, inputs=None, **kwargs):
        """
        Accept either:
        1. the existing dictionary input, or
        2. a JSON string containing that dictionary.
        """
        if inputs is None:
            inputs = kwargs

        if isinstance(inputs, str):
            try:
                inputs = json.loads(inputs)
            except json.JSONDecodeError as e:
                raise ValueError(
                    "YieldProcessor expected a JSON string "
                    "representing the model input."
                ) from e

        if not isinstance(inputs, dict):
            raise ValueError(
                "YieldProcessor expects a dictionary or JSON string."
            )

        return inputs