Instructions to use MCS12/llama-3-xml-presentation-generator with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use MCS12/llama-3-xml-presentation-generator with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("MCS12/llama-3-xml-presentation-generator", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload handler.py
Browse files- handler.py +5 -128
handler.py
CHANGED
|
@@ -15,14 +15,15 @@ class EndpointHandler:
|
|
| 15 |
def __init__(self, path=""):
|
| 16 |
peft_config = PeftConfig.from_pretrained(path)
|
| 17 |
|
| 18 |
-
|
| 19 |
bnb_config = BitsAndBytesConfig(
|
| 20 |
load_in_4bit=True,
|
| 21 |
bnb_4bit_quant_type="nf4",
|
| 22 |
bnb_4bit_compute_dtype=torch.float16,
|
| 23 |
bnb_4bit_use_double_quant=True,
|
| 24 |
)
|
| 25 |
-
|
|
|
|
| 26 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
peft_config.base_model_name_or_path,
|
| 28 |
quantization_config=bnb_config,
|
|
@@ -38,6 +39,7 @@ class EndpointHandler:
|
|
| 38 |
torch_dtype=torch.float16,
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 42 |
path,
|
| 43 |
trust_remote_code=True,
|
|
@@ -47,66 +49,6 @@ class EndpointHandler:
|
|
| 47 |
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 48 |
self.tokenizer.padding_side = "right"
|
| 49 |
|
| 50 |
-
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 51 |
-
"""
|
| 52 |
-
data args:
|
| 53 |
-
inputs (:obj: `str`)
|
| 54 |
-
date (:obj: `str`)
|
| 55 |
-
Return:
|
| 56 |
-
A :obj:`list` | `dict`: will be serialized and returned
|
| 57 |
-
"""
|
| 58 |
-
# get inputs
|
| 59 |
-
inputs = data.pop("inputs", data)
|
| 60 |
-
tone = data.pop("tone")
|
| 61 |
-
length = data.pop("length")
|
| 62 |
-
|
| 63 |
-
prompt = self.apply_chat_template_for_presentation(self.tokenizer, inputs, tone, length)
|
| 64 |
-
input_ids = self.tokenizer(prompt, return_tensors="pt")
|
| 65 |
-
input_ids = input_ids.to('cuda')
|
| 66 |
-
|
| 67 |
-
outputs = self.model.generate(
|
| 68 |
-
**input_ids,
|
| 69 |
-
max_new_tokens=8192,
|
| 70 |
-
temperature=0.7,
|
| 71 |
-
top_p=0.95,
|
| 72 |
-
do_sample=True,
|
| 73 |
-
repetition_penalty=1.15,
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
available_tags = ["presentation", "slide", "ul", "ol", "li", "h1", "h2", "h3", "p"]
|
| 77 |
-
available_stylings = ["bold", "italic", "underline", "color", "align", "fontSize"]
|
| 78 |
-
prediction = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 79 |
-
|
| 80 |
-
# start_indices = []
|
| 81 |
-
# tag_start_indices = []
|
| 82 |
-
# for i in range(len(prediction)):
|
| 83 |
-
# if (prediction.startswith("<presentation>", i)):
|
| 84 |
-
# start_indices.append(i)
|
| 85 |
-
|
| 86 |
-
# if (prediction.startswith("true", i)):
|
| 87 |
-
# if not ((ord(prediction[i - 1]) == 39 and ord(prediction[i+4]) == 39) or (ord(prediction[i-1]) == 34 and ord(prediction[i+4]) == 34)):
|
| 88 |
-
# prediction = prediction[:i] + "\'" + prediction[i:i+4] + "\'" + prediction[i+4:]
|
| 89 |
-
|
| 90 |
-
# if (prediction.startswith("false", i)):
|
| 91 |
-
# if not ((ord(prediction[i - 1]) == 39 and ord(prediction[i+5]) == 39) or (ord(prediction[i-1]) == 34 and ord(prediction[i+5]) == 34)):
|
| 92 |
-
# prediction = prediction[:i] + "\'" + prediction[i:i+5] + "\'" + prediction[i+5:]
|
| 93 |
-
|
| 94 |
-
# start_index = start_indices[-1]
|
| 95 |
-
# end_index = prediction.find("</presentation>") + 15
|
| 96 |
-
# prediction = prediction[start_index:end_index]
|
| 97 |
-
|
| 98 |
-
# # correct any syntax error
|
| 99 |
-
# brackets_indices = find_matching_brackets(prediction)
|
| 100 |
-
# offset = 0
|
| 101 |
-
# for start_end in brackets_indices:
|
| 102 |
-
# close_index = start_end[1] - offset
|
| 103 |
-
# char_before = prediction[close_index - 1]
|
| 104 |
-
# if (not (ord(char_before) <= 122 and ord(char_before) >= 97) and not(ord(char_before) == 34 or ord(char_before) == 39 or ord(char_before) == 32)):
|
| 105 |
-
# prediction = prediction[:close_index - 1] + prediction[close_index:]
|
| 106 |
-
# offset += 1
|
| 107 |
-
|
| 108 |
-
return [{"generated_text": prediction}]
|
| 109 |
-
|
| 110 |
def apply_chat_template_for_presentation(self, tokenizer, content, tone="professional", length="medium"):
|
| 111 |
"""Format input using the model's chat template"""
|
| 112 |
|
|
@@ -123,69 +65,4 @@ class EndpointHandler:
|
|
| 123 |
add_generation_prompt=True # Adds the assistant prefix for generation
|
| 124 |
)
|
| 125 |
|
| 126 |
-
return prompt
|
| 127 |
-
|
| 128 |
-
# def find_matching_brackets(text: str) -> List[Tuple[int, int]]:
|
| 129 |
-
# """
|
| 130 |
-
# Find all matching '<' and '>' pairs (including nested).
|
| 131 |
-
# Returns list of tuples (open_index, close_index).
|
| 132 |
-
# """
|
| 133 |
-
# stack = []
|
| 134 |
-
# matches = []
|
| 135 |
-
|
| 136 |
-
# for i, char in enumerate(text):
|
| 137 |
-
# if char == '<' and (start_with_valid_tag(text, i + 1)):
|
| 138 |
-
# stack.append(i)
|
| 139 |
-
# elif char == '>' and stack:
|
| 140 |
-
# open_index = stack.pop()
|
| 141 |
-
# matches.append((open_index, i))
|
| 142 |
-
|
| 143 |
-
# return matches
|
| 144 |
-
|
| 145 |
-
# def start_with_valid_tag(text, index):
|
| 146 |
-
# available_tags = ["presentation", "slide", "ul", "ol", "li", "h1", "h2", "h3", "p"]
|
| 147 |
-
# for tag in available_tags:
|
| 148 |
-
# if text.startswith(tag, index):
|
| 149 |
-
# return True
|
| 150 |
-
|
| 151 |
-
# return False
|
| 152 |
-
|
| 153 |
-
# if __name__ == "__main__":
|
| 154 |
-
# sample_xml = """<presentation]>
|
| 155 |
-
# <slide id="1" class="intro">
|
| 156 |
-
# <h1>Main Title</h1>
|
| 157 |
-
# <p style="color:blue">Some text</p>
|
| 158 |
-
# <img src="test.jpg"/>
|
| 159 |
-
# <ul>
|
| 160 |
-
# <li>Item 1</li>
|
| 161 |
-
# <li>Item 2</li>
|
| 162 |
-
# </ul>
|
| 163 |
-
# </slide>
|
| 164 |
-
# <slide id="2"[>
|
| 165 |
-
# <h2 bold=true>Second Slide</h2>
|
| 166 |
-
# <h1 italic=true>hi</h1>
|
| 167 |
-
# <h1 italic=false>hi</h1>
|
| 168 |
-
# </slide>
|
| 169 |
-
# </presentation>"""
|
| 170 |
-
|
| 171 |
-
# # # correct any syntax error
|
| 172 |
-
# brackets_indices = find_matching_brackets(sample_xml)
|
| 173 |
-
# offset = 0
|
| 174 |
-
# for start_end in brackets_indices:
|
| 175 |
-
# close_index = start_end[1] - offset
|
| 176 |
-
# char_before = sample_xml[close_index - 1]
|
| 177 |
-
# if (not (ord(char_before) <= 122 and ord(char_before) >= 97) and not(ord(char_before) == 34 or ord(char_before) == 39 or ord(char_before) == 32)):
|
| 178 |
-
# sample_xml = sample_xml[:close_index - 1] + sample_xml[close_index:]
|
| 179 |
-
# offset += 1
|
| 180 |
-
|
| 181 |
-
# for i in range(len(sample_xml)):
|
| 182 |
-
# if (sample_xml.startswith("true", i)):
|
| 183 |
-
# if not ((ord(sample_xml[i - 1]) == 39 and ord(sample_xml[i+4]) == 39) or (ord(sample_xml[i-1]) == 34 and ord(sample_xml[i+4]) == 34)):
|
| 184 |
-
# sample_xml = sample_xml[:i] + "\'" + sample_xml[i:i+4] + "\'" + sample_xml[i+4:]
|
| 185 |
-
# offset += 1
|
| 186 |
-
|
| 187 |
-
# if (sample_xml.startswith("false", i)):
|
| 188 |
-
# if not ((ord(sample_xml[i - 1]) == 39 and ord(sample_xml[i+5]) == 39) or (ord(sample_xml[i-1]) == 34 and ord(sample_xml[i+5]) == 34)):
|
| 189 |
-
# sample_xml = sample_xml[:i] + "\'" + sample_xml[i:i+5] + "\'" + sample_xml[i+5:]
|
| 190 |
-
|
| 191 |
-
# print(sample_xml)
|
|
|
|
| 15 |
def __init__(self, path=""):
|
| 16 |
peft_config = PeftConfig.from_pretrained(path)
|
| 17 |
|
| 18 |
+
|
| 19 |
bnb_config = BitsAndBytesConfig(
|
| 20 |
load_in_4bit=True,
|
| 21 |
bnb_4bit_quant_type="nf4",
|
| 22 |
bnb_4bit_compute_dtype=torch.float16,
|
| 23 |
bnb_4bit_use_double_quant=True,
|
| 24 |
)
|
| 25 |
+
|
| 26 |
+
# Load the base model
|
| 27 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
peft_config.base_model_name_or_path,
|
| 29 |
quantization_config=bnb_config,
|
|
|
|
| 39 |
torch_dtype=torch.float16,
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# load tokenizer
|
| 43 |
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 44 |
path,
|
| 45 |
trust_remote_code=True,
|
|
|
|
| 49 |
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 50 |
self.tokenizer.padding_side = "right"
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
def apply_chat_template_for_presentation(self, tokenizer, content, tone="professional", length="medium"):
|
| 53 |
"""Format input using the model's chat template"""
|
| 54 |
|
|
|
|
| 65 |
add_generation_prompt=True # Adds the assistant prefix for generation
|
| 66 |
)
|
| 67 |
|
| 68 |
+
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|