File size: 2,590 Bytes
5646b48
6599942
9017320
 
edc5024
bfbb9c5
 
 
 
 
 
 
5646b48
 
 
2fbe6e1
 
936abb7
2fbe6e1
 
 
 
 
bfbb9c5
936abb7
 
2fbe6e1
 
 
 
 
 
bfbb9c5
f068e5f
2fbe6e1
 
 
 
 
 
bfbb9c5
936abb7
bfbb9c5
c1f9d07
bfbb9c5
 
 
6883faa
2786dad
382d521
5646b48
fb898da
5646b48
 
 
 
e461154
5646b48
 
 
 
 
 
 
 
 
 
936abb7
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
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: <presentation>, <slide>, <h1>, <h2>, <p>, <ul>, <ol>, <li> 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