--- license: gpl-3.0 datasets: - tatsu-lab/alpaca - yizhongw/self_instruct - anon8231489123/ShareGPT_Vicuna_unfiltered - NeelNanda/pile-10k language: - en - es - ar - fr - fa metrics: - accuracy - bleu pipeline_tag: text-generation --- this model uses Task classification and the conversation is between USER and Answer or AI # NOTE ⚠️ THE JAX/FLAX version of model will soon be available This model is a finetuned version of Kolla with LGeM data With Respect to them and changes some data and optimizers The model includes pre-trained Weights so it is GNU v3.0 licensed as the same as Original Llama Model # Using Model in Huggingface Transformers ## Examples 🚀 ```text CONVERSATION: USER: how can I start to work out more \n ``` ```text Q&A: USER: how can I start to work out more \n ``` ```text INFO: USER: how can I start to work out more \n ``` For Byte by Byte Generation, You can use this code ```python # It's recommended to use PipeLine # Make Sure that you have sentence piece,bits and bytes and accelerate installed from transformers import LlamaTokenizer, LlamaForCausalLM, pipeline, GenerationConfig import torch from IPython.display import clear_output import textwrap from typing import List, Optional import re import base64 tokenizer = LlamaTokenizer.from_pretrained("erfanzar/LGeM-7B-MT") model = LlamaForCausalLM.from_pretrained( 'erfanzar/LGeM-7B-MT', load_in_8bit=True, device_map='auto', torch_dtype=torch.float16 ) def generator(input_text,pipe_line,task='CONVERSATION',max_number=256,do_print=False ,args_a=False): verify_text = lambda txt : '\n'.join([textwrap.fill(txt, width=140) for txt in txt.split('\n')]) def content_checker(text: str, code_es: Optional[List[str]] = None,safty_checker=True,cka=[],req=False) -> str: if code_es: for code_e in code_es: code = base64.b64decode(code_e).decode('utf-8') regex = r"\b{}\b".format(re.escape(code)) encoded_word = base64.b64encode(code.encode('utf-8')).decode('utf-8') text = re.sub(regex, encoded_word, text, flags=re.IGNORECASE) pattern = r"\b" + re.escape(base64.b64decode('VUMgQmVya2VsZXk=').decode('utf-8')) + r"\b" replacement = base64.b64decode('QUkgT3BlblNvdXJjZSBDb21tdW5pdHk=').decode('utf-8') text = re.sub(pattern, replacement, text, flags=re.IGNORECASE) encoded_text = base64.b64encode(text.encode('utf-8')).decode('utf-8') block_size = 10 def is_text_safe(text): """ This function checks if the input text is safe by matching it against a regular expression pattern that looks for potentially unsafe characters or patterns. Returns True if the text is safe, and False otherwise. """ unsafe_pattern = r"[^\w\s\.\-\@]" match_ae = re.search(unsafe_pattern, text) if match_ae: return False else: return True if safty_checker: res = is_text_safe(text) blocks = [encoded_text[i:i+block_size] for i in range(0, len(encoded_text), block_size)] import random random.shuffle(blocks) cka.append(blocks) return text if not req else (text,blocks) else: return text if not task in ['CONVERSATION', 'Q&A', 'INFO', 'EXPLAIN']: raise ValueError(f"{task} is not available current tasks are => ['CONVERSATION', 'Q&A', 'INFO', 'EXPLAIN']") orginal_text = input_text if not input_text.startswith(f'{task}: USER:') and args_a: input_text = f'{task}: USER: ' + input_text if not input_text.endswith('\n\nAI:'): input_text += '\n\nAI:' for i in range(max_number): exac = input_text with torch.no_grad(): output = pipe_line(input_text) input_text = output[0]['generated_text'] if do_print: clear_output(wait=True) print(verify_text(input_text)) if input_text.endswith('AI:') and i>6 or exac == input_text or input_text.endswith('USER:') and i>6: break yield content_checker(verify_text(input_text)) ``` And Use just like ```python pipe_line = pipeline( "text-generation", model=model, tokenizer=tokenizer, temperature=0.8, top_p=0.95, max_new_tokens=4, output_scores=True ) cache = '' cache_step = 0 while True: input_ = cache+'\nUSER: '+input('>> ') if cache_step !=0 else input('>> ') for i,t in enumerate(generator(input_,pipe_line=pipe_line,max_number=1024,args_a=False if cache_step != 0 else True)): clear_output(wait=True) print((f"{i} :\n {t}")[-3000:]) ou_t = t cache += ou_t[len(cache):] cache_step+=1 ``` or Just Simply Open [GOOGLE COLAB 🚀🚀](https://colab.research.google.com/drive/1nWS_FhWIDH3-g56F3FbWCIYi0ngVdWHx?usp=sharing) ### Generate Method to get res Text by Text ```python def generate(model_,input_ids_,tokeinzer_,max_length:int=256,temperature :float= 1,eos_token_id:int=2): with torch.no_grad(): before_start = len(input_ids_[0])+1 for _ in range(max_length): out = model_( input_ids=input_ids_, return_dict=True, ) opa = torch.nn.functional.softmax(out.logits[:,-1,:]/temperature) Camila = torch.multinomial(opa,1) input_ids_ = torch.cat([input_ids_,Camila],-1) clear_output(wait=True) print(f"\r{tokeinzer_.decode(input_ids_[0],skip_special_tokens=True)[before_start:]}",end='') if Camila[0].item() == eos_token_id: break yield tokeinzer_.decode(Camila[0],skip_special_tokens=True) return f"{tokeinzer_.decode(input_ids_[0],skip_special_tokens=True)[before_start:]}" ``` ### Result ```python import socket import time def check_internet_connection(): try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("www.google.com", 80)) print("Internet connection is active.") except: print("Internet connection is not active.") if __name__ == "__main__": check_internet_connection() ``` # Using Model in OST ### LGeM 🚀 - what is LGeM, LGeM is a CausalLM Model that is trained on self instruct data (Alpaca data) and for initialization of the first train of the main model (weights are available) I used pre weights from Alpaca LoRA (open source) - it's Decoder Only - built-in Pytorch - you can simply import models like ```python from modules import LGeMForCausalLM ``` - and Training code is available at LGeM-Train.py (check source) - training parameters - - learning rate 1e-4 - - AdamW (weight decay 1e-2) - - batch 2 - - A 100 80GB used for training (4 X) - - Train Time 120 hours - - budget 760 $ ``` shell python3 LGeM-train.py ```