Views
No views yet
NousResearch/llama-2-7b-chat-hf, designed to solve reasoning-heavy questions in English and provide step-by-step solutions in Spanish. The model uses LoRA (Low-Rank Adaptation) for efficient fine-tuning and 4-bit quantization, making it suitable for deployment in resource-constrained environments like Kaggle.1!pip install transformers torch peft bitsandbytes
2
3# Step 1: Import necessary libraries
4from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig
5from peft import PeftModel
6import torch
7
8# Step 2: Define repository and load tokenizer
9repo_id = "anasakram/llama_fine_tuned_spanish"
10tokenizer = AutoTokenizer.from_pretrained(repo_id)
11
12# Step 3: Set up quantization config (matches training setup)
13bnb_config = BitsAndBytesConfig(
14 load_in_4bit=True,
15 bnb_4bit_quant_type="nf4",
16 bnb_4bit_compute_dtype=torch.float16,
17)
18
19# Step 4: Load the base model with quantization
20base_model = AutoModelForCausalLM.from_pretrained(
21 "NousResearch/llama-2-7b-chat-hf",
22 quantization_config=bnb_config,
23 device_map={"": 0} # Map to GPU 0
24)
25
26# Step 5: Load the fine-tuned LoRA weights
27model = PeftModel.from_pretrained(base_model, repo_id)
28
29# Step 6: Create text generation pipeline
30gen = pipeline('text-generation', model=model, tokenizer=tokenizer, max_length=500) # Max length for generation
31
32# Step 7: Define system message and prompt
33system_message = (
34 "Given a puzzle-like, reasoning-heavy question in English, provide an accurate, "
35 "step-by-step solution in Spanish. For multi-part problems, calculate each segment "
36 "separately, sum distances and times correctly, and verify all steps logically to "
37 "ensure the final answer is correct."
38)
39prompt = (
40 f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n"
41 "A car travels 100 km at 50 km/h and 50 km at 25 km/h. What is the average speed? [/INST]"#Enter Your Puzzle Here
42)
43
44# Step 8: Generate and print response
45result = gen(prompt)
46print("Generated Response:")
47print(result[0]['generated_text'].replace(prompt, '')) # Print only the generated parttrain.jsonl), 20 validation examples (test.jsonl)