File size: 7,291 Bytes
8a830ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# handler.py
"""
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
# Configure 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}")
# Model configuration - must match training setup
self.base_model_name = "meta-llama/Llama-3.2-3B-Instruct"
self.max_seq_length = 512
# Load tokenizer
logger.info("Loading tokenizer...")
self.tokenizer = AutoTokenizer.from_pretrained(self.base_model_name)
# Set up tokenizer properly
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
self.tokenizer.padding_side = "right"
# Quantization config for memory efficiency (same as training)
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4"
)
# Load base model with quantization
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
)
# Load LoRA adapters
logger.info(f"Loading LoRA adapters from {path}...")
self.model = PeftModel.from_pretrained(
self.model,
path,
torch_dtype=torch.float16
)
# Set model to evaluation mode
self.model.eval()
# Enable memory efficient operations
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
"""
# Extract inputs
inputs = data.get("inputs", "")
parameters = data.get("parameters", {})
# Handle single string or list of strings
if isinstance(inputs, str):
questions = [inputs]
elif isinstance(inputs, list):
questions = inputs
else:
raise ValueError("Inputs must be a string or list of strings")
# Extract generation parameters with defaults matching training
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)
# Process each question
results = []
for question in questions:
try:
# Format the prompt
formatted_prompt = self.format_prompt(question)
# Tokenize
inputs_encoded = self.tokenizer(
formatted_prompt,
return_tensors="pt",
truncation=True,
max_length=self.max_seq_length // 2, # Leave room for response
padding=False
).to(self.model.device)
prompt_length = inputs_encoded.input_ids.shape[1]
# Generate response
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
)
# Decode responses
generated_texts = []
for output in outputs:
# Extract only the generated part (after the prompt)
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())
# Format result
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)
})
# Clear cache to free memory
if torch.cuda.is_available():
torch.cuda.empty_cache()
return results |