File size: 5,917 Bytes
047c840
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c191dbc
 
047c840
 
 
 
 
 
fb4613c
047c840
c191dbc
047c840
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04fd021
047c840
 
 
 
 
 
 
04fd021
047c840
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
---
base_model: unsloth/meta-llama-3.1-8b-instruct-bnb-4bit
language:
- en
license: mit
tags:
- text-generation-inference
- transformers
- unsloth
- llama
- trl
datasets:
- justsomerandomdude264/ScienceQA-Dataset
library_name: transformers
---
# Model Card: Math Homework Solver

This is a Large Language Model (LLM) fine-tuned to solve math problems with detailed, step-by-step explanations and accurate answers. The base model used is Llama 3.1 with 8 billion parameters, which has been quantized to 4-bit using QLoRA (Quantized Low-Rank Adaptation) and PEFT (Parameter-Efficient Fine-Tuning) through the Unsloth framework.

Other Homework Solver Models include [Math_Homework_Solver_Llama318B](https://huggingface.co/justsomerandomdude264/Math_Homework_Solver_Llama318B) and [SocialScience_Homework_Solver_Llama318B](https://huggingface.co/justsomerandomdude264/SocialScience_Homework_Solver_Llama318B)

## Model Details

- **Base Model**: Llama 3.1 (8 Billion parameters)
- **Fine-tuning Method**: PEFT (Parameter-Efficient Fine-Tuning) with QLoRA
- **Quantization**: 4-bit quantization for reduced memory usage
- **Training Framework**: Unsloth, optimized for efficient fine-tuning of large language models
- **Training Environment**: Google Colab (free tier), NVIDIA T4 GPU (16GB VRAM), 12GB RAM
- **Dataset Used**: justsomerandomdude264/ScienceQA-Dataset, 560 selected rows
- **Git Repo**: The git repo on my github account is [justsomerandomdude264/Homework_Solver_LLM](https://github.com/justsomerandomdude264/Homework_Solver_LLM)

## Capabilities

The Science Homework Solver model is designed to assist with a broad spectrum of scientifical problems, from basics of gravity to advanced topics like quantum entanglement. It provides clear and detailed explanations, making it an excellent resource for students, educators, and anyone looking to deepen their understanding of scientifical concepts.

By leveraging the Llama 3.1 base model and fine-tuning it using PEFT and QLoRA, this model achieves high-quality performance while maintaining a relatively small computational footprint, making it accessible even on limited hardware.

## Getting Started

To start using the Science Homework Solver model, follow these steps:

1. **Clone the repo**
   ```bash
   git clone https://huggingface.co/justsomerandomdude264/Science_Homework_Solver-Llama3.18B
   ```
   
2. **Run inference**

   1. This method is recommended as its reliable and accurate:
   ```python
   from unsloth import FastLanguageModel
   import torch
    
   # Define Your Question
   question = "A tank is filled with water to a height of 12.5 cm. The apparent depth of a needle lying at the bottom of the tank is measured by a microscope to be 9.4 cm. What is the refractive index of water? If water is replaced by a liquid of refractive index 1.63 up to the same height, by what distance would the microscope have to be moved to focus on the needle again?" # Example Question, You can change it with one of your own
   # Load the model
   model, tokenizer = FastLanguageModel.from_pretrained(
           model_name = "Science_Homework_Solver_Llama318B/model_adapters", # The dir where the repo is cloned or "\\" for root
           max_seq_length = 2048,
           dtype = None,
           load_in_4bit = True,
       )
   # Set the model in inference model
   FastLanguageModel.for_inference(model)
   # QA template
   qa_template = """Question: {}
   Answer: {}"""
   # Tokenize inputs
   inputs = tokenizer(
   [
       qa_template.format(
           question, # Question
           "", # Answer - left blank for generation
       )
   ], return_tensors = "pt").to("cuda")
   # Stream the answer/output of the model
   from transformers import TextStreamer
   text_streamer = TextStreamer(tokenizer)
   _ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 512)
   ```
   
   2. Another way to run inference is to use the merged adapters (not recommend as it gives inaccurate/different answers):
   ```python
   from transformers import LlamaForCausalLM, AutoTokenizer
   # Load the model
   model = LlamaForCausalLM.from_pretrained(
       "justsomerandomdude264/Science_Homework_Solver_Llama318B",
       device_map="auto"
   )
   
   # Load the tokenizer
   tokenizer = AutoTokenizer.from_pretrained("justsomerandomdude264/Science_Homework_Solver_Llama318B")
   # Set the inputs up
   qa_template = """Question: {}
   Answer: {}"""
   
   inputs = tokenizer(
   [
       qa_template.format(
           "A tank is filled with water to a height of 12.5 cm. The apparent depth of a needle lying at the bottom of the tank is measured by a microscope to be 9.4 cm. What is the refractive index of water? If water is replaced by a liquid of refractive index 1.63 up to the same height, by what distance would the microscope have to be moved to focus on the needle again?", # instruction
           "", # output - leave this blank for generation!
       )
   ], return_tensors = "pt").to("cuda")
   
   # Do a forward pass
   outputs = model.generate(**inputs, max_new_tokens = 128, use_cache = True)
   raw_output = str(tokenizer.batch_decode(outputs))
   
   # Formtting the string
   # Removing the list brackets and splitting the string by newline characters
   formatted_string = raw_output.strip("[]").replace("<|begin_of_text|>", "").replace("<|eot_id|>", "").strip("''").split("\\n")
   # Print the lines one by one
   for line in formatted_string:
     print(line)
   ```
  

## Citation

Please use the following citation if you reference the Math Homework Solver model:

### BibTeX Citation
```bibtex
@misc{paliwal2024,
    author = {Krishna Paliwal},
    title = {Contributions to Science_Homework_Solver},
    year = {2024},
    email = {[email protected]}
}
```

### APA Citation
```plaintext
Paliwal, Krishna (2024). Contributions to Science_Homework_Solver. Email: [email protected] .
```