from typing import Dict, List, Any import torch import re from typing import List, Tuple, Dict from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training, PeftConfig, PeftModel from transformers import ( AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TrainingArguments, pipeline ) class EndpointHandler: def __init__(self, path=""): peft_config = PeftConfig.from_pretrained(path) bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16, bnb_4bit_use_double_quant=True, ) # Load the base model base_model = AutoModelForCausalLM.from_pretrained( peft_config.base_model_name_or_path, quantization_config=bnb_config, device_map="auto", trust_remote_code=True, torch_dtype=torch.float16, ) # Load the LoRA adapter with the correct configuration self.model = PeftModel.from_pretrained( base_model, path, torch_dtype=torch.float16, ) # load tokenizer self.tokenizer = AutoTokenizer.from_pretrained( path, trust_remote_code=True, use_auth_token=True ) self.tokenizer.pad_token = self.tokenizer.eos_token self.tokenizer.padding_side = "right" def apply_chat_template_for_presentation(self, tokenizer, content, tone="professional", length="medium"): """Format input using the model's chat template""" # Create messages following Llama 3's chat format messages = [ {"role": "system", "content": f"Summarize the content into presentation slides in XML syntax with following parameters:\n Tone: {tone}\n Length: {length}\n Strictly use only the tags: , ,

,

,

,

    ,
      ,
    1. only\n Strictly use only the available formatting: bold='true'/'false', italic='true'/'false', underline='true'/'false', color='red'/'green'/'yellow'/'blue', alight='left'/'center'/'right', fontSize=integer"}, {"role": "user", "content": content} ] # Apply the chat template prompt = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True # Adds the assistant prefix for generation ) return prompt