Phi3-LoRA-GSM8k / README.md
Vijayendra's picture
Update README.md
e1d3122 verified
|
raw
history blame
2.71 kB
metadata
base_model: unsloth/phi-3-mini-4k-instruct-bnb-4bit
library_name: peft

How to use :


import torch
from transformers import TextStreamer
from unsloth import FastLanguageModel

# Hugging Face repository details
model_name = "Vijayendra/Phi3-LoRA-GSM8k"  

# Function to generate and solve problems using the fine-tuned model
def generate_and_solve_problems(model, tokenizer, num_problems=5):
    """
    Generate and solve math and reasoning problems using the fine-tuned model.

    Parameters:
        model: Fine-tuned language model
        tokenizer: Corresponding tokenizer
        num_problems: Number of problems to generate and solve
    """
    # Prompt template
    test_prompt = """Below is a math problem. Solve the problem step by step and provide a detailed explanation.

### Problem:
{}

### Solution:"""

    test_problems = [
        "A car travels at 40 mph for 2 hours, then at 60 mph for another 3 hours. How far does it travel in total?",
        "If the sum of three consecutive integers is 72, what are the integers?",
        "A train leaves Station A at 10:00 AM traveling at 50 mph. Another train leaves Station A at 12:00 PM traveling at 70 mph on the same track. At what time will the second train catch up to the first?",
        "A rectangle has a length of 12 units and a width of 8 units. If the length is increased by 50% and the width is reduced by 25%, what is the new area of the rectangle?",
        "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?"
    ]

    # Adjust for the requested number of problems
    test_problems = test_problems[:num_problems]

    # Generate solutions
    model.eval()  # Set model to evaluation mode
    device = "cuda" if torch.cuda.is_available() else "cpu"
    model.to(device)  # Move model to the appropriate device

    for idx, problem in enumerate(test_problems, 1):
        # Format the problem into the prompt
        input_text = test_prompt.format(problem)
        inputs = tokenizer(input_text, return_tensors="pt").to(device)

        # Generate a response
        print(f"\nProblem {idx}: {problem}\nSolution:")
        streamer = TextStreamer(tokenizer)
        _ = model.generate(**inputs, streamer=streamer, max_new_tokens=512)
        print("\n" + "=" * 80 + "\n")

# Load the fine-tuned model and tokenizer from Hugging Face
model, tokenizer = FastLanguageModel.from_pretrained(model_name, max_seq_length=2048)

# Prepare the model for inference
FastLanguageModel.for_inference(model)

# Test the model by generating and solving problems
generate_and_solve_problems(model, tokenizer, num_problems=5)