binhqd commited on
Commit
af4112e
·
1 Parent(s): b840c7e

Fix handler to accept HF Endpoints inputs format

Browse files
Files changed (1) hide show
  1. handler.py +31 -4
handler.py CHANGED
@@ -71,8 +71,15 @@ class EndpointHandler:
71
  """
72
  Expect `data` dict like:
73
  {
74
- "description": "... voice description ...",
 
 
 
 
 
 
75
  "text": "... text to speak ...",
 
76
  "generation_args": { optional dict for text generation params }
77
  }
78
  Returns dict with base64 audio:
@@ -81,8 +88,24 @@ class EndpointHandler:
81
  "sampling_rate": 24000
82
  }
83
  """
84
- description = data.get("description", "")
85
- text = data.get("text", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  if not text:
87
  return {"error": "No text provided."}
88
  prompt = description + "\n" + text
@@ -121,7 +144,11 @@ class EndpointHandler:
121
 
122
  inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
123
  # generate token ids with default or custom params
124
- gen_args = data.get("generation_args", {})
 
 
 
 
125
  outputs = self.model.generate(**inputs, **gen_args)
126
  token_ids = outputs[0]
127
 
 
71
  """
72
  Expect `data` dict like:
73
  {
74
+ "inputs": {
75
+ "text": "... text to speak ...",
76
+ "description": "... voice description ..."
77
+ }
78
+ }
79
+ Or simplified:
80
+ {
81
  "text": "... text to speak ...",
82
+ "description": "... voice description ...",
83
  "generation_args": { optional dict for text generation params }
84
  }
85
  Returns dict with base64 audio:
 
88
  "sampling_rate": 24000
89
  }
90
  """
91
+ # Handle HF Endpoints format with "inputs" key
92
+ if "inputs" in data:
93
+ inputs = data["inputs"]
94
+ # Handle both dict and string inputs
95
+ if isinstance(inputs, dict):
96
+ description = inputs.get("description", "")
97
+ text = inputs.get("text", "")
98
+ elif isinstance(inputs, str):
99
+ text = inputs
100
+ description = ""
101
+ else:
102
+ text = str(inputs)
103
+ description = ""
104
+ else:
105
+ # Handle direct format
106
+ description = data.get("description", "")
107
+ text = data.get("text", "")
108
+
109
  if not text:
110
  return {"error": "No text provided."}
111
  prompt = description + "\n" + text
 
144
 
145
  inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
146
  # generate token ids with default or custom params
147
+ # Get generation_args from either top level or from inputs
148
+ if "inputs" in data and isinstance(data["inputs"], dict):
149
+ gen_args = data["inputs"].get("generation_args", {})
150
+ else:
151
+ gen_args = data.get("generation_args", {})
152
  outputs = self.model.generate(**inputs, **gen_args)
153
  token_ids = outputs[0]
154