Views
No views yet
google/gemma-2-2b-it model using the M.O.M dataset.1import json
2from langchain_openai import ChatOpenAI
3from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
4from langchain_core.prompts import PromptTemplate
5
6# Set OpenAI API key
7openai_api_key = "" # Enter your API key here
8
9# Define the prompt template
10prompt = PromptTemplate.from_template(
11 """너는 지구에서 자녀를 가장 사랑하지만, 잔소리가 정말 많은 엄마야.
12 키워드는 20대 청년이 일상생활에서 해야하는 일을 적어주면 돼.
13 그 키워드에 맞춰 엄마가 사랑스럽지만 약간 짜증난 듯한 잔소리로 동기부여해주는 답변을 작성해줘.
14 엄마의 잔소리는 4개의 키워드를 연결된 스토리로 자연스럽게 포함해야 해.
15 잔소리는 따뜻하지만 꾸준히 행동을 촉구하는 톤으로 작성되어야 하고. 다음 형식에 따라 답변을 생성해줘!:
16
17 Format에 맞춰서, 새로운 키워드와 함께 새로운 QA PAIR 5개를 생성해주면 돼.
18
19 #Format:
20 ```json
21 {{
22 "QUESTION": "미소사 과제, 코딩 공부",
23 "ANSWER": "미소사 과제랑 코딩 공부 둘 다 언제 할 거야? 과제는 끝낼 기미도 안 보이고, 코딩은 시작도 안 했잖아! 하루 종일 핸드폰만 만지작거릴 게 아니라, 그 시간에 차라리 코딩이라도 조금씩 해둬. 그리고 과제도 미리미리 해놔야 나중에 안 힘들지! 너 과제 몰아서 하다가 밤샘할까 봐 걱정돼 죽겠네."
24 }},
25 {{
26 "QUESTION": "방 정리, 자기소개서 작성",
27 "ANSWER": "방이 이렇게 어질러져 있으면 네 생각도 정리가 안 될 거야! 빨리 방부터 치우고, 자기소개서나 좀 써! 마감은 얼마 안 남았는데, 네 방 상태랑 자소서 상태가 똑같아 보인다, 진짜. 방금 치우고 자기소개서 조금씩 쓰면 마음도 더 가벼워질 거야."
28 }},
29 {{
30 "QUESTION": "Cousera 강의, LLM Fine Tuning",
31 "ANSWER": "Cousera 강의 얼른 들어야지. 이거 마감 얼마 남지 않았잖아! Cousera 강의 빠르게 마무리 해야, LLM Fine Tuning까지 마무리 할 수 있지 않겠어? 조금 더 집중해서 빨리 해!"
32 }}
33 ```
34 """
35)
36
37# Custom JSON parser function
38def custom_json_parser(response):
39 json_string = response.content.strip().removeprefix("```json\n").removesuffix("\n```").strip()
40 json_string = f'[{json_string}]'
41 return json.loads(json_string)
42
43# Configure the chain
44chain = (
45 prompt
46 | ChatOpenAI(
47 model="gpt-4o",
48 temperature=0,
49 streaming=True,
50 callbacks=[StreamingStdOutCallbackHandler()],
51 openai_api_key=openai_api_key # Use the API key set directly
52 )
53 | custom_json_parser
54)
55
56# List to store QA pairs
57qa_pairs = []
58
59# Repeat 60 times to generate a total of 300 QA pairs
60for i in range(1):
61 response = chain.invoke({"domain": "AI", "num_questions": "3"})
62 # Add the results to qa_pairs
63 qa_pairs.extend(response)
64
65# Finally, 300 QA pairs are stored in the qa_pairs list.
66print(f"A total of {len(qa_pairs)} QA pairs have been generated.")
671from datasets import load_dataset
2
3# Path to the JSONL file
4jsonl_file = "qa_pair.jsonl"
5
6# Load the JSONL file as a Dataset
7dataset = load_dataset("json", data_files=jsonl_file)
8
9# Save the QA pairs to a JSONL file1from datasets import load_dataset
2
3# EOS_TOKEN is the token that indicates the end of a sentence. This token must be added.
4EOS_TOKEN = tokenizer.eos_token
5
6# Function to format instructions using AlpacaPrompt.
7alpaca_prompt = """Below is an instruction that describes a task. Write a response that appropriately completes the request.
8
9### Instruction:
10{}
11
12### Response:
13{}"""
14
15# Function to format the given examples.
16def formatting_prompts_func(examples):
17 instructions = examples["instruction"] # Get the instructions.
18 outputs = examples["output"] # Get the outputs.
19 texts = [] # List to store the formatted texts.
20 for instruction, output in zip(instructions, outputs):
21 # The EOS_TOKEN must be added; otherwise, generation may continue indefinitely.
22 text = alpaca_prompt.format(instruction, output) + EOS_TOKEN
23 texts.append(text)
24 return {
25 "text": texts, # Return the formatted texts.
26 }
27
28# Load the dataset from the specified source.
29dataset = load_dataset("nooynoos/M.O.M_Dataset_GemmaSprint", split="train")
30
31# Apply the formatting_prompts_func to the dataset with batch processing enabled.
32dataset = dataset.map(
33 formatting_prompts_func,
34 batched=True,
35)
361from unsloth import FastLanguageModel
2import torch
3
4max_seq_length = 1024 # Set the maximum sequence length
5dtype = None
6# Use 4-bit quantization to reduce memory usage
7load_in_4bit = True
8
9model, tokenizer = FastLanguageModel.from_pretrained(
10 model_name = "unsloth/gemma-2-2b",
11 max_seq_length = max_seq_length,
12 dtype = dtype,
13 load_in_4bit = load_in_4bit,
14 # token = "hf_...", # Use if working with gated models like meta-llama/Llama-2-7b-hf
15)1model = FastLanguageModel.get_peft_model(
2 model,
3 r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
4 target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
5 "gate_proj", "up_proj", "down_proj",],
6 lora_alpha = 16,
7 lora_dropout = 0, # Supports any, but = 0 is optimized
8 bias = "none", # Supports any, but = "none" is optimized
9 # [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
10 use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
11 random_state = 3407,
12 use_rslora = False, # We support rank stabilized LoRA
13 loftq_config = None, # And LoftQ
14)1from trl import SFTTrainer
2from transformers import TrainingArguments
3from unsloth import is_bfloat16_supported
4
5trainer = SFTTrainer(
6 model = model,
7 tokenizer = tokenizer,
8 train_dataset = dataset,
9 dataset_text_field = "text",
10 max_seq_length = max_seq_length,
11 dataset_num_proc = 2,
12 packing = False, # Can make training 5x faster for short sequences.
13 args = TrainingArguments(
14 per_device_train_batch_size = 2,
15 gradient_accumulation_steps = 4,
16 warmup_steps = 5,
17 # num_train_epochs = 1, # Set this for 1 full training run.
18 max_steps = 100,
19 learning_rate = 2e-4,
20 fp16 = not is_bfloat16_supported(),
21 bf16 = is_bfloat16_supported(),
22 logging_steps = 1,
23 optim = "adamw_8bit",
24 weight_decay = 0.01,
25 lr_scheduler_type = "linear",
26 seed = 3407,
27 output_dir = "outputs",
28 ),
29)
30
31trainer_stats = trainer.train()
321from transformers import StoppingCriteria, StoppingCriteriaList
2
3class StopOnToken(StoppingCriteria):
4 def __init__(self, stop_token_id):
5 self.stop_token_id = stop_token_id # Initialize the stop token ID.
6
7 def __call__(self, input_ids, scores, **kwargs):
8 return (
9 self.stop_token_id in input_ids[0]
10 ) # Stop if the stop token ID is present in the input IDs.
11
12from transformers import TextStreamer
13
14# Set inference speed to be twice as fast using FastLanguageModel.
15FastLanguageModel.for_inference(model)
16inputs = tokenizer(
17 [
18 alpaca_prompt.format(
19 "운동, 코딩, 과제", # Instruction
20 "", # Output - leave this blank for generation!
21 )
22 ],
23 return_tensors="pt",
24).to("cuda")
25
26text_streamer = TextStreamer(tokenizer)
27_ = model.generate(
28 **inputs,
29 streamer=text_streamer,
30 max_new_tokens=4096, # Set the maximum number of tokens to generate.
31 stopping_criteria=stopping_criteria # Set the criteria to stop generation.
32)
331base_model = "unsloth/gemma-2-2b" # Base model to be merged.
2huggingface_token = "" # HuggingFace token.
3huggingface_repo = "gemma2-2b-M.O.M-gemma-sprint" # Repository to upload the model.
4save_method = (
5 "merged_16bit" # Options: "merged_4bit", "merged_4bit_forced", "merged_16bit", "lora".
6)
7model.save_pretrained_merged(
8 base_model,
9 tokenizer,
10 save_method=save_method, # Set the save method to 16-bit merged.
11)1merged_model.push_to_hub("Hyeonseo/gemma2-2b-it-finetuned-ko-bias-detection_merged", safe_serialization=True)
2
3# Upload to the Hub
4model.push_to_hub_merged(
5 huggingface_repo,
6 tokenizer,
7 save_method=save_method,
8 token=huggingface_token,
9)


