| 
22 | 22 | 
 
  | 
23 | 23 | 
 
  | 
24 | 24 | def decode_csv(inputs: Input, require_headers=True):  # type: (str) -> np.array  | 
25 |  | -    csv_string = inputs.get_as_string()  | 
 | 25 | +    csv_content = inputs.get_as_string()  | 
26 | 26 | 
 
  | 
27 | 27 |     if require_headers:  | 
28 |  | -        if not any(header in csv_string.splitlines()[0].lower()  | 
 | 28 | +        if not any(header in csv_content.splitlines()[0].lower()  | 
29 | 29 |                    for header in ["question", "context", "inputs"]):  | 
30 | 30 |             raise ValueError(  | 
31 | 31 |                 "You need to provide the correct CSV with Header columns to use it with the inference toolkit default handler.",  | 
32 | 32 |             )  | 
33 |  | -        stream = StringIO(csv_string)  | 
 | 33 | +        stream = StringIO(csv_content)  | 
34 | 34 |         request_list = list(csv.DictReader(stream))  | 
35 | 35 |         if "inputs" in request_list[0].keys():  | 
36 | 36 |             return {"inputs": [entry["inputs"] for entry in request_list]}  | 
37 | 37 |         else:  | 
38 | 38 |             return {"inputs": request_list}  | 
39 | 39 |     else:  | 
40 | 40 |         # for preditive ML inputs  | 
41 |  | -        try:  | 
42 |  | -            return np.genfromtxt(StringIO(csv_string), delimiter=",")  | 
43 |  | -        except (ValueError, TypeError) as e:  | 
44 |  | -            raise ValueError(f"Failed to parse CSV data: {str(e)}")  | 
 | 41 | +        result = np.genfromtxt(StringIO(csv_content), delimiter=",")  | 
 | 42 | +        # Check for NaN values which indicate non-numeric data  | 
 | 43 | +        if np.isnan(result).any():  | 
 | 44 | +            raise ValueError(  | 
 | 45 | +                "CSV contains non-numeric data. Please provide numeric data only."  | 
 | 46 | +            )  | 
 | 47 | +        return result  | 
45 | 48 | 
 
  | 
46 | 49 | 
 
  | 
47 | 50 | def encode_csv(content):  # type: (str) -> np.array  | 
 | 
0 commit comments