Vijayendra commited on
Commit
3c47b25
·
verified ·
1 Parent(s): e1d3122

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +39 -27
README.md CHANGED
@@ -4,13 +4,9 @@ library_name: peft
4
  ---
5
  How to use :
6
  ```python
7
-
8
- import torch
9
- from transformers import TextStreamer
10
- from unsloth import FastLanguageModel
11
-
12
- # Hugging Face repository details
13
- model_name = "Vijayendra/Phi3-LoRA-GSM8k"
14
 
15
  # Function to generate and solve problems using the fine-tuned model
16
  def generate_and_solve_problems(model, tokenizer, num_problems=5):
@@ -30,6 +26,7 @@ def generate_and_solve_problems(model, tokenizer, num_problems=5):
30
 
31
  ### Solution:"""
32
 
 
33
  test_problems = [
34
  "A car travels at 40 mph for 2 hours, then at 60 mph for another 3 hours. How far does it travel in total?",
35
  "If the sum of three consecutive integers is 72, what are the integers?",
@@ -38,30 +35,45 @@ def generate_and_solve_problems(model, tokenizer, num_problems=5):
38
  "If a person invests $1000 in a savings account that earns 5% annual interest compounded yearly, how much money will be in the account after 10 years?"
39
  ]
40
 
41
- # Adjust for the requested number of problems
42
  test_problems = test_problems[:num_problems]
43
 
44
- # Generate solutions
45
- model.eval() # Set model to evaluation mode
46
- device = "cuda" if torch.cuda.is_available() else "cpu"
47
- model.to(device) # Move model to the appropriate device
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
- for idx, problem in enumerate(test_problems, 1):
50
- # Format the problem into the prompt
51
- input_text = test_prompt.format(problem)
52
- inputs = tokenizer(input_text, return_tensors="pt").to(device)
53
 
54
- # Generate a response
55
- print(f"\nProblem {idx}: {problem}\nSolution:")
56
- streamer = TextStreamer(tokenizer)
57
- _ = model.generate(**inputs, streamer=streamer, max_new_tokens=512)
58
- print("\n" + "=" * 80 + "\n")
59
 
60
- # Load the fine-tuned model and tokenizer from Hugging Face
61
- model, tokenizer = FastLanguageModel.from_pretrained(model_name, max_seq_length=2048)
 
62
 
63
- # Prepare the model for inference
64
- FastLanguageModel.for_inference(model)
65
 
66
- # Test the model by generating and solving problems
67
- generate_and_solve_problems(model, tokenizer, num_problems=5)
 
4
  ---
5
  How to use :
6
  ```python
7
+ !pip install peft accelerate bitsandbytes
8
+ from peft import PeftModel, PeftConfig
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
 
10
 
11
  # Function to generate and solve problems using the fine-tuned model
12
  def generate_and_solve_problems(model, tokenizer, num_problems=5):
 
26
 
27
  ### Solution:"""
28
 
29
+ # Sample test problems
30
  test_problems = [
31
  "A car travels at 40 mph for 2 hours, then at 60 mph for another 3 hours. How far does it travel in total?",
32
  "If the sum of three consecutive integers is 72, what are the integers?",
 
35
  "If a person invests $1000 in a savings account that earns 5% annual interest compounded yearly, how much money will be in the account after 10 years?"
36
  ]
37
 
38
+ # Use only the specified number of problems
39
  test_problems = test_problems[:num_problems]
40
 
41
+ for problem in test_problems:
42
+ # Create the prompt
43
+ prompt = test_prompt.format(problem)
44
+
45
+ # Tokenize and generate response
46
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True).to("cuda")
47
+ outputs = model.generate(
48
+ input_ids=inputs["input_ids"],
49
+ attention_mask=inputs["attention_mask"],
50
+ max_length=512,
51
+ temperature=0.7,
52
+ top_p=0.9,
53
+ do_sample=True,
54
+ )
55
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
56
+
57
+ # Print the problem and the solution
58
+ print("### Problem:")
59
+ print(problem)
60
+ print("### Solution:")
61
+ print(response)
62
+ print("\n" + "="*50 + "\n")
63
+
64
+ # Example usage with model and tokenizer
65
 
66
+ base_model_name = "unsloth/phi-3-mini-4k-instruct-bnb-4bit"
67
+ lora_model_name = "Vijayendra/Phi3-LoRA-GSM8k"
 
 
68
 
69
+ # Load base model and tokenizer
70
+ base_model = AutoModelForCausalLM.from_pretrained(base_model_name, device_map="auto", torch_dtype="auto")
71
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name)
 
 
72
 
73
+ # Load the fine-tuned LoRA model
74
+ model = PeftModel.from_pretrained(base_model, lora_model_name)
75
+ model.eval()
76
 
77
+ # Call the function to solve problems
78
+ generate_and_solve_problems(model, tokenizer)
79