Christopher Glaze
commited on
Commit
·
fbe1af4
1
Parent(s):
9067c0f
Update data contract
Browse files- handler.py +13 -4
- tests.py +20 -2
handler.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
|
| 2 |
-
from typing import Dict, Union, Optional
|
| 3 |
from pathlib import Path
|
| 4 |
import json
|
| 5 |
import joblib
|
|
@@ -114,12 +114,19 @@ class EndpointHandler():
|
|
| 114 |
return self.response_pipeline.predict_proba(df1)[:,1]
|
| 115 |
|
| 116 |
|
| 117 |
-
def __call__(self,
|
| 118 |
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
if is_dict:
|
| 122 |
-
df = pd.DataFrame([
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
if 'dataset' not in df.columns:
|
| 125 |
df['dataset'] = ''
|
|
@@ -135,5 +142,7 @@ class EndpointHandler():
|
|
| 135 |
|
| 136 |
if is_dict:
|
| 137 |
return predictions[0]
|
|
|
|
|
|
|
| 138 |
else:
|
| 139 |
return pd.DataFrame(predictions, index=df.index)
|
|
|
|
| 1 |
|
| 2 |
+
from typing import Dict, List, Union, Optional
|
| 3 |
from pathlib import Path
|
| 4 |
import json
|
| 5 |
import joblib
|
|
|
|
| 114 |
return self.response_pipeline.predict_proba(df1)[:,1]
|
| 115 |
|
| 116 |
|
| 117 |
+
def __call__(self, data: Dict[str, Union[Dict, List, pd.DataFrame]]):
|
| 118 |
|
| 119 |
+
inputs = data['inputs']
|
| 120 |
+
|
| 121 |
+
is_dict = isinstance(inputs, dict)
|
| 122 |
+
is_list = isinstance(inputs, list)
|
| 123 |
|
| 124 |
if is_dict:
|
| 125 |
+
df = pd.DataFrame([inputs])
|
| 126 |
+
elif is_list:
|
| 127 |
+
df = pd.DataFrame(inputs)
|
| 128 |
+
else:
|
| 129 |
+
df = inputs
|
| 130 |
|
| 131 |
if 'dataset' not in df.columns:
|
| 132 |
df['dataset'] = ''
|
|
|
|
| 142 |
|
| 143 |
if is_dict:
|
| 144 |
return predictions[0]
|
| 145 |
+
elif is_list:
|
| 146 |
+
return predictions
|
| 147 |
else:
|
| 148 |
return pd.DataFrame(predictions, index=df.index)
|
tests.py
CHANGED
|
@@ -1,11 +1,29 @@
|
|
| 1 |
from handler import EndpointHandler
|
|
|
|
| 2 |
|
| 3 |
# init handler
|
| 4 |
response_model_handler = EndpointHandler()
|
| 5 |
|
| 6 |
# prepare sample payload
|
| 7 |
-
payload = {"instruction": "What are some ways to stay energized throughout the day?",
|
| 8 |
-
"response": "Drink lots of coffee!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# test the handler
|
| 11 |
pred=response_model_handler(payload)
|
|
|
|
| 1 |
from handler import EndpointHandler
|
| 2 |
+
import pandas as pd
|
| 3 |
|
| 4 |
# init handler
|
| 5 |
response_model_handler = EndpointHandler()
|
| 6 |
|
| 7 |
# prepare sample payload
|
| 8 |
+
payload = {'inputs': {"instruction": "What are some ways to stay energized throughout the day?",
|
| 9 |
+
"response": "Drink lots of coffee!"}}
|
| 10 |
+
|
| 11 |
+
# test the handler
|
| 12 |
+
pred=response_model_handler(payload)
|
| 13 |
+
|
| 14 |
+
print(pred)
|
| 15 |
+
|
| 16 |
+
payload = {'inputs': [{"instruction": "What are some ways to stay energized throughout the day?",
|
| 17 |
+
"response": "Drink lots of coffee!"}]*2}
|
| 18 |
+
|
| 19 |
+
# test the handler
|
| 20 |
+
pred=response_model_handler(payload)
|
| 21 |
+
|
| 22 |
+
print(pred)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
payload = {'inputs': pd.DataFrame([{"instruction": "What are some ways to stay energized throughout the day?",
|
| 26 |
+
"response": "Drink lots of coffee!"}]*2)}
|
| 27 |
|
| 28 |
# test the handler
|
| 29 |
pred=response_model_handler(payload)
|