First commit
Browse files- .gitignore +1 -0
- handler.py +26 -0
- requirements.txt +1 -0
- usage_example.py +19 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
handler.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Dict, List
|
| 2 |
+
|
| 3 |
+
import holidays
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class EndpointHandler:
|
| 7 |
+
def __init__(self, path=""):
|
| 8 |
+
self.holidays = holidays.US()
|
| 9 |
+
|
| 10 |
+
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 11 |
+
"""
|
| 12 |
+
data args:
|
| 13 |
+
inputs (:obj: `str`)
|
| 14 |
+
date (:obj: `str`)
|
| 15 |
+
Return:
|
| 16 |
+
A :obj:`list` | `dict`: will be serialized and returned
|
| 17 |
+
"""
|
| 18 |
+
# get inputs
|
| 19 |
+
inputs = data.pop("inputs", data)
|
| 20 |
+
date = data.pop("date", None)
|
| 21 |
+
|
| 22 |
+
# check if date exists and if it is a holiday
|
| 23 |
+
if date is not None and date in self.holidays:
|
| 24 |
+
return [{"label": "happy", "score": 1}]
|
| 25 |
+
|
| 26 |
+
return [{"label": "sad", "score": 1}]
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
holidays
|
usage_example.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from handler import EndpointHandler
|
| 2 |
+
|
| 3 |
+
# init handler
|
| 4 |
+
my_handler = EndpointHandler(path=".")
|
| 5 |
+
|
| 6 |
+
# prepare sample payload
|
| 7 |
+
non_holiday_payload = {
|
| 8 |
+
"inputs": "I am quite excited how this will turn out",
|
| 9 |
+
"date": "2022-08-08",
|
| 10 |
+
}
|
| 11 |
+
holiday_payload = {"inputs": "Today is a though day", "date": "2022-07-04"}
|
| 12 |
+
|
| 13 |
+
# test the handler
|
| 14 |
+
non_holiday_pred = my_handler(non_holiday_payload)
|
| 15 |
+
holiday_payload = my_handler(holiday_payload)
|
| 16 |
+
|
| 17 |
+
# show results
|
| 18 |
+
print("non_holiday_pred", non_holiday_pred)
|
| 19 |
+
print("holiday_payload", holiday_payload)
|