Update handler.py
Browse files- handler.py +20 -7
handler.py
CHANGED
|
@@ -7,13 +7,26 @@ class EndpointHandler:
|
|
| 7 |
|
| 8 |
def load(self):
|
| 9 |
print("Model loading skipped for Hello World API.")
|
| 10 |
-
return
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def load(self):
|
| 9 |
print("Model loading skipped for Hello World API.")
|
|
|
|
| 10 |
|
| 11 |
+
def __call__(self, inputs):
|
| 12 |
+
"""
|
| 13 |
+
Hugging Face expects the `EndpointHandler` to be callable.
|
| 14 |
+
So, we define `__call__` instead of `predict`.
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
# Hugging Face sends input as a LIST or DICT, so handle both cases
|
| 18 |
+
if isinstance(inputs, list) and len(inputs) > 0:
|
| 19 |
+
user_text = inputs[0] # Extract text from the list
|
| 20 |
+
elif isinstance(inputs, dict) and "inputs" in inputs:
|
| 21 |
+
user_text = inputs["inputs"]
|
| 22 |
+
else:
|
| 23 |
+
return {"error": "Invalid input format. Expected {'inputs': 'your text'} or ['your text']."}
|
| 24 |
|
| 25 |
+
# Generate timestamp
|
| 26 |
+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 27 |
|
| 28 |
+
# Return formatted message
|
| 29 |
+
return {"message": f"Received at {current_time}: {user_text}"}
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return {"error": f"Unexpected error: {str(e)}"}
|