|
|
|
""" |
|
Hugging Face Inference Endpoint Handler for SciGuru PPO+LoRA Model |
|
Handles inference requests for the fine-tuned Llama model with LoRA adapters |
|
""" |
|
|
|
import torch |
|
from typing import Dict, List, Any |
|
from transformers import ( |
|
AutoTokenizer, |
|
AutoModelForCausalLM, |
|
BitsAndBytesConfig |
|
) |
|
from peft import PeftModel |
|
import logging |
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
class EndpointHandler: |
|
def __init__(self, path=""): |
|
""" |
|
Initialize the handler by loading the model and tokenizer. |
|
|
|
Args: |
|
path: Path to the model directory containing the LoRA adapters |
|
""" |
|
logger.info(f"Initializing EndpointHandler with path: {path}") |
|
|
|
|
|
self.base_model_name = "meta-llama/Llama-3.2-3B-Instruct" |
|
self.max_seq_length = 512 |
|
|
|
|
|
logger.info("Loading tokenizer...") |
|
self.tokenizer = AutoTokenizer.from_pretrained(self.base_model_name) |
|
|
|
|
|
if self.tokenizer.pad_token is None: |
|
self.tokenizer.pad_token = self.tokenizer.eos_token |
|
self.tokenizer.padding_side = "right" |
|
|
|
|
|
bnb_config = BitsAndBytesConfig( |
|
load_in_4bit=True, |
|
bnb_4bit_compute_dtype=torch.float16, |
|
bnb_4bit_use_double_quant=True, |
|
bnb_4bit_quant_type="nf4" |
|
) |
|
|
|
|
|
logger.info("Loading base model with 4-bit quantization...") |
|
self.model = AutoModelForCausalLM.from_pretrained( |
|
self.base_model_name, |
|
quantization_config=bnb_config, |
|
device_map="auto", |
|
torch_dtype=torch.float16, |
|
trust_remote_code=True |
|
) |
|
|
|
|
|
logger.info(f"Loading LoRA adapters from {path}...") |
|
self.model = PeftModel.from_pretrained( |
|
self.model, |
|
path, |
|
torch_dtype=torch.float16 |
|
) |
|
|
|
|
|
self.model.eval() |
|
|
|
|
|
if torch.cuda.is_available(): |
|
torch.backends.cuda.matmul.allow_tf32 = True |
|
torch.backends.cudnn.allow_tf32 = True |
|
|
|
logger.info("Model initialization complete!") |
|
|
|
def format_prompt(self, question: str) -> str: |
|
""" |
|
Format the question with the same prompt template used during training. |
|
|
|
Args: |
|
question: The scientific question to answer |
|
|
|
Returns: |
|
Formatted prompt string |
|
""" |
|
prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|> |
|
You are an expert science educator. Explain scientific concepts clearly and simply, using analogies and everyday examples when possible. Make your explanations accessible to someone with basic high school science knowledge.<|eot_id|> |
|
<|start_header_id|>user<|end_header_id|> |
|
{question}<|eot_id|> |
|
<|start_header_id|>assistant<|end_header_id|> |
|
""" |
|
return prompt |
|
|
|
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: |
|
""" |
|
Handle inference requests. |
|
|
|
Args: |
|
data: Input data containing 'inputs' and optional 'parameters' |
|
- inputs: Can be a string or list of strings (questions) |
|
- parameters: Optional generation parameters |
|
|
|
Returns: |
|
List of dictionaries containing generated explanations |
|
""" |
|
|
|
inputs = data.get("inputs", "") |
|
parameters = data.get("parameters", {}) |
|
|
|
|
|
if isinstance(inputs, str): |
|
questions = [inputs] |
|
elif isinstance(inputs, list): |
|
questions = inputs |
|
else: |
|
raise ValueError("Inputs must be a string or list of strings") |
|
|
|
|
|
max_new_tokens = parameters.get("max_new_tokens", 256) |
|
temperature = parameters.get("temperature", 0.7) |
|
top_p = parameters.get("top_p", 0.9) |
|
do_sample = parameters.get("do_sample", True) |
|
num_return_sequences = parameters.get("num_return_sequences", 1) |
|
|
|
|
|
results = [] |
|
|
|
for question in questions: |
|
try: |
|
|
|
formatted_prompt = self.format_prompt(question) |
|
|
|
|
|
inputs_encoded = self.tokenizer( |
|
formatted_prompt, |
|
return_tensors="pt", |
|
truncation=True, |
|
max_length=self.max_seq_length // 2, |
|
padding=False |
|
).to(self.model.device) |
|
|
|
prompt_length = inputs_encoded.input_ids.shape[1] |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = self.model.generate( |
|
inputs_encoded.input_ids, |
|
attention_mask=inputs_encoded.attention_mask, |
|
max_new_tokens=max_new_tokens, |
|
do_sample=do_sample, |
|
temperature=temperature, |
|
top_p=top_p, |
|
pad_token_id=self.tokenizer.pad_token_id, |
|
eos_token_id=self.tokenizer.eos_token_id, |
|
num_return_sequences=num_return_sequences |
|
) |
|
|
|
|
|
generated_texts = [] |
|
for output in outputs: |
|
|
|
generated_ids = output[prompt_length:] |
|
generated_text = self.tokenizer.decode( |
|
generated_ids, |
|
skip_special_tokens=True, |
|
clean_up_tokenization_spaces=True |
|
) |
|
generated_texts.append(generated_text.strip()) |
|
|
|
|
|
if num_return_sequences == 1: |
|
results.append({ |
|
"question": question, |
|
"explanation": generated_texts[0] |
|
}) |
|
else: |
|
results.append({ |
|
"question": question, |
|
"explanations": generated_texts |
|
}) |
|
|
|
except Exception as e: |
|
logger.error(f"Error processing question '{question}': {str(e)}") |
|
results.append({ |
|
"question": question, |
|
"error": str(e) |
|
}) |
|
|
|
|
|
if torch.cuda.is_available(): |
|
torch.cuda.empty_cache() |
|
|
|
return results |