|
1 | | -from vetiver.handlers import base |
| 1 | +from typing import List |
2 | 2 | from functools import singledispatch |
3 | 3 | from contextlib import suppress |
| 4 | +import pandas as pd |
4 | 5 |
|
5 | 6 | from ..prototype import vetiver_create_prototype |
6 | 7 | from ..meta import VetiverMeta |
@@ -118,6 +119,30 @@ def handler_startup(): |
118 | 119 | """ |
119 | 120 | ... |
120 | 121 |
|
| 122 | + def _prepare_data(self, pred_data) -> pd.DataFrame: |
| 123 | + """Convert prototype to dataframe data |
| 124 | +
|
| 125 | + Parameters |
| 126 | + ---------- |
| 127 | + prototype_data : pd.DataFrame, np.ndarray, or None |
| 128 | + Training data to create prototype |
| 129 | +
|
| 130 | + Returns |
| 131 | + ------- |
| 132 | + prototype : pd.DataFrame or None |
| 133 | + Zero-row DataFrame for storing data types |
| 134 | + """ |
| 135 | + if isinstance(pred_data, List): |
| 136 | + columns = pred_data[0].dict().keys() |
| 137 | + data = [line.dict() for line in pred_data] |
| 138 | + served_data = pd.DataFrame(data, columns=columns) |
| 139 | + else: |
| 140 | + served_data = [] |
| 141 | + for key, value in pred_data: |
| 142 | + served_data.append(value) |
| 143 | + |
| 144 | + return served_data |
| 145 | + |
121 | 146 | def handler_predict(self, input_data, check_prototype): |
122 | 147 | """Generates method for /predict endpoint in VetiverAPI |
123 | 148 |
|
@@ -145,7 +170,7 @@ def handler_predict(self, input_data, check_prototype): |
145 | 170 |
|
146 | 171 |
|
147 | 172 | @create_handler.register |
148 | | -def _(model: base.BaseHandler, prototype_data): |
| 173 | +def _(model: BaseHandler, prototype_data): |
149 | 174 | if model.prototype_data is None and prototype_data is not None: |
150 | 175 | model.prototype_data = prototype_data |
151 | 176 |
|
|
0 commit comments