Reyad-Ahmmed commited on
Commit
1c414e1
·
verified ·
1 Parent(s): a91c42a

Update handler.py

Browse files
Files changed (1) hide show
  1. 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 predict(self, inputs):
13
- if not isinstance(inputs, dict) or "inputs" not in inputs:
14
- return {"error": "Invalid input format. Expected {'inputs': 'your text'}"}
 
 
 
 
 
 
 
 
 
 
15
 
16
- user_text = inputs["inputs"]
17
- current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
18
 
19
- return {"message": f"Received at {current_time}: {user_text}"}
 
 
 
 
 
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)}"}