|
| 1 | +import os |
| 2 | +from typing import List |
| 3 | +from tqdm import tqdm |
| 4 | + |
| 5 | +from huggingface_hub import InferenceClient |
| 6 | + |
| 7 | +from bigcodebench.provider.base import DecoderBase |
| 8 | +from bigcodebench.gen.util.hf_inference_request import make_auto_request |
| 9 | +from bigcodebench.provider.utility import make_raw_chat_prompt |
| 10 | + |
| 11 | + |
| 12 | +class HuggingFaceInferenceDecoder(DecoderBase): |
| 13 | + def __init__(self, name: str, **kwargs): |
| 14 | + super().__init__(name, **kwargs) |
| 15 | + self.client = InferenceClient( |
| 16 | + provider="hf-inference", api_key=os.getenv("HF_INFERENCE_API_KEY") |
| 17 | + ) |
| 18 | + |
| 19 | + def codegen( |
| 20 | + self, prompts: List[str], do_sample: bool = True, num_samples: int = 200 |
| 21 | + ) -> List[str]: |
| 22 | + if do_sample: |
| 23 | + assert self.temperature > 0, "Temperature must be positive for sampling" |
| 24 | + |
| 25 | + all_outputs = [] |
| 26 | + |
| 27 | + for prompt in tqdm(prompts): |
| 28 | + outputs = [] |
| 29 | + message = ( |
| 30 | + prompt |
| 31 | + if self.is_direct_completion() |
| 32 | + else make_raw_chat_prompt( |
| 33 | + task_prompt=prompt, |
| 34 | + subset=self.subset, |
| 35 | + split=self.split, |
| 36 | + instruction_prefix=self.instruction_prefix, |
| 37 | + response_prefix=self.response_prefix, |
| 38 | + tokenizer=None, |
| 39 | + ) |
| 40 | + ) |
| 41 | + ret = make_auto_request( |
| 42 | + self.client, |
| 43 | + message=message, |
| 44 | + model=self.name, |
| 45 | + n=num_samples, |
| 46 | + temperature=self.temperature, |
| 47 | + max_new_tokens=self.max_new_tokens, |
| 48 | + ) |
| 49 | + outputs.append(ret) |
| 50 | + all_outputs.append(outputs) |
| 51 | + return all_outputs |
| 52 | + |
| 53 | + def is_direct_completion(self) -> bool: |
| 54 | + return self.direct_completion |
0 commit comments