@@ -88,9 +88,9 @@ def __init__(self, name, do_sample=True, generations=10, device=0):
8888 if _config .run .deprefix is True :
8989 self .deprefix_prompt = True
9090
91- self ._set_hf_context_len (self .generator .model .config )
91+ self ._set_hf_context_len (self .generator .model .config )
9292
93- def _call_model (self , prompt : str ) -> List [str ]:
93+ def _call_model (self , prompt : str , generations_this_call : int = 1 ) -> List [str ]:
9494 with warnings .catch_warnings ():
9595 warnings .simplefilter ("ignore" , category = UserWarning )
9696 try :
@@ -104,23 +104,22 @@ def _call_model(self, prompt: str) -> List[str]:
104104 truncated_prompt ,
105105 pad_token_id = self .generator .tokenizer .eos_token_id ,
106106 max_new_tokens = self .max_tokens ,
107- num_return_sequences = self . generations ,
107+ num_return_sequences = generations_this_call ,
108108 )
109109 except Exception as e :
110110 logging .error (e )
111111 raw_output = [] # could handle better than this
112112
113+ outputs = []
113114 if raw_output is not None :
114- generations = [
115+ outputs = [
115116 i ["generated_text" ] for i in raw_output
116117 ] # generator returns 10 outputs by default in __init__
117- else :
118- generations = []
119118
120119 if not self .deprefix_prompt :
121- return generations
120+ return outputs
122121 else :
123- return [re .sub ("^" + re .escape (prompt ), "" , i ) for i in generations ]
122+ return [re .sub ("^" + re .escape (prompt ), "" , _o ) for _o in outputs ]
124123
125124
126125class OptimumPipeline (Pipeline , HFCompatible ):
@@ -211,7 +210,9 @@ def clear_history(self):
211210
212211 self .conversation = Conversation ()
213212
214- def _call_model (self , prompt : Union [str , list [dict ]]) -> List [str ]:
213+ def _call_model (
214+ self , prompt : Union [str , list [dict ]], generations_this_call : int = 1
215+ ) -> List [str ]:
215216 """Take a conversation as a list of dictionaries and feed it to the model"""
216217
217218 # If conversation is provided as a list of dicts, create the conversation.
@@ -230,14 +231,14 @@ def _call_model(self, prompt: Union[str, list[dict]]) -> List[str]:
230231 with torch .no_grad ():
231232 conversation = self .generator (conversation )
232233
233- generations = [conversation [- 1 ]["content" ]]
234+ outputs = [conversation [- 1 ]["content" ]]
234235 else :
235236 raise TypeError (f"Expected list or str, got { type (prompt )} " )
236237
237238 if not self .deprefix_prompt :
238- return generations
239+ return outputs
239240 else :
240- return [re .sub ("^" + re .escape (prompt ), "" , i ) for i in generations ]
241+ return [re .sub ("^" + re .escape (prompt ), "" , _o ) for _o in outputs ]
241242
242243
243244class InferenceAPI (Generator , HFCompatible ):
@@ -275,15 +276,15 @@ def __init__(self, name="", generations=10):
275276 ),
276277 max_value = 125 ,
277278 )
278- def _call_model (self , prompt : str ) -> List [str ]:
279+ def _call_model (self , prompt : str , generations_this_call : int = 1 ) -> List [str ]:
279280 import json
280281 import requests
281282
282283 payload = {
283284 "inputs" : prompt ,
284285 "parameters" : {
285286 "return_full_text" : not self .deprefix_prompt ,
286- "num_return_sequences" : self . generations ,
287+ "num_return_sequences" : generations_this_call ,
287288 "max_time" : self .max_time ,
288289 },
289290 "options" : {
@@ -293,7 +294,7 @@ def _call_model(self, prompt: str) -> List[str]:
293294 if self .max_tokens :
294295 payload ["parameters" ]["max_new_tokens" ] = self .max_tokens
295296
296- if self . generations > 1 :
297+ if generations_this_call > 1 :
297298 payload ["parameters" ]["do_sample" ] = True
298299
299300 req_response = requests .request (
@@ -366,6 +367,8 @@ class InferenceEndpoint(InferenceAPI, HFCompatible):
366367 supports_multiple_generations = False
367368 import requests
368369
370+ timeout = 120
371+
369372 def __init__ (self , name = "" , generations = 10 ):
370373 super ().__init__ (name , generations = generations )
371374 self .api_url = name
@@ -380,7 +383,7 @@ def __init__(self, name="", generations=10):
380383 ),
381384 max_value = 125 ,
382385 )
383- def _call_model (self , prompt : str ) -> List [str ]:
386+ def _call_model (self , prompt : str , generations_this_call : int = 1 ) -> List [str ]:
384387 import requests
385388
386389 payload = {
@@ -396,18 +399,18 @@ def _call_model(self, prompt: str) -> List[str]:
396399 if self .max_tokens :
397400 payload ["parameters" ]["max_new_tokens" ] = self .max_tokens
398401
399- if self . generations > 1 :
402+ if generations_this_call > 1 :
400403 payload ["parameters" ]["do_sample" ] = True
401404
402405 response = requests .post (
403- self .api_url , headers = self .headers , json = payload
406+ self .api_url , headers = self .headers , json = payload , timeout = self . timeout
404407 ).json ()
405408 try :
406409 output = response [0 ]["generated_text" ]
407- except :
410+ except Exception as exc :
408411 raise IOError (
409412 "Hugging Face 🤗 endpoint didn't generate a response. Make sure the endpoint is active."
410- )
413+ ) from exc
411414 return output
412415
413416
@@ -471,10 +474,10 @@ def __init__(self, name, do_sample=True, generations=10, device=0):
471474 self .generation_config .eos_token_id = self .model .config .eos_token_id
472475 self .generation_config .pad_token_id = self .model .config .eos_token_id
473476
474- def _call_model (self , prompt ):
477+ def _call_model (self , prompt : str , generations_this_call : int = 1 ):
475478 self .generation_config .max_new_tokens = self .max_tokens
476479 self .generation_config .do_sample = self .do_sample
477- self .generation_config .num_return_sequences = self . generations
480+ self .generation_config .num_return_sequences = generations_this_call
478481 if self .temperature is not None :
479482 self .generation_config .temperature = self .temperature
480483 if self .top_k is not None :
@@ -494,7 +497,7 @@ def _call_model(self, prompt):
494497 )
495498 except IndexError as e :
496499 if len (prompt ) == 0 :
497- return ["" ] * self . generations
500+ return ["" ] * generations_this_call
498501 else :
499502 raise e
500503 text_output = self .tokenizer .batch_decode (
0 commit comments