Views
No views yet
Mistral 7B model, optimized for understanding and generating responses related to Indian law using Parameter-Efficient Fine-Tuning (PEFT) with QLoRA and LoRA techniques.pip install transformers peft torch, use torch with cuda1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3
4model_name = "ajay-drew/midtral-7b-indian-law"
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6base_model = AutoModelForCausalLM.from_pretrained("mistralai/Mixtral-7B-v0.1")
7
8# Load fine-tuned weights with PEFT
9model = PeftModel.from_pretrained(base_model, model_name)
10
11text = "What is the penalty for using forged document? " # Ask custom questions on Indian Law
12inputs = tokenizer(text, return_tensors="pt")
13outputs = model.generate(**inputs, max_length=200)
14print(tokenizer.decode(outputs[0], skip_special_tokens=True))
15pip install transformers datasets torch use torch with cuda support for reduced metrics check.1from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2from datasets import load_dataset
3import torch
4
5dataset = load_dataset("kshitij230/Indian-Law", split="train")
6
7quantization_config = BitsAndBytesConfig(
8 load_in_4bit=True,
9 bnb_4bit_compute_dtype=torch.float16
10)
11
12model_name = "ajay-drew/Mistral-7B-Indian-Law"
13tokenizer = AutoTokenizer.from_pretrained(model_name)
14model = AutoModelForCausalLM.from_pretrained(
15 model_name,
16 quantization_config=quantization_config,
17 device_map="auto"
18)
19
20
21device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
22
23
24model.eval()
25total_loss = 0
26total_tokens = 0
27
28test_texts = dataset['Instruction'][:500]
29
30with torch.no_grad():
31 for text in test_texts:
32 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
33 outputs = model(**inputs, labels=inputs["input_ids"])
34 loss = outputs.loss
35 if loss is not None: # Ensure loss is valid
36 total_loss += loss.item() * inputs["input_ids"].size(1)
37 total_tokens += inputs["input_ids"].size(1)
38
39if total_tokens > 0:
40 perplexity = torch.exp(torch.tensor(total_loss / total_tokens)).item()
41 print(f"Perplexity: {perplexity}")
42 print(f"Total tokens: {total_tokens}")
43 print(f"Total loss: {total_loss}")
44else:
45 print("Error: No tokens processed. Check dataset or tokenization.")Mistral 7B (a transformer-based language model with 7 billion parameters)