Abdulrhman37
commited on
Commit
•
814d403
1
Parent(s):
c4d0587
Update README.md
Browse files
README.md
CHANGED
@@ -54,6 +54,111 @@ Fine-tuning focused on enhancing domain-specific knowledge using a dataset curat
|
|
54 |
|
55 |
## 📦 How to Use
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
follow this [notebook](https://colab.research.google.com/drive/1pRNcAtybNF6w6mE1ZReFwfrIujZ5_t4S#scrollTo=wk4fCWOl0Ocd) for help to use the model
|
58 |
|
59 |
## 📧 Contact
|
|
|
54 |
|
55 |
## 📦 How to Use
|
56 |
|
57 |
+
|
58 |
+
1. **Install Dependencies**:
|
59 |
+
```python
|
60 |
+
%%capture
|
61 |
+
!pip install unsloth
|
62 |
+
|
63 |
+
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir --no-deps git+https://github.com/unslothai/unsloth.git
|
64 |
+
```
|
65 |
+
2. **Load the model**:
|
66 |
+
|
67 |
+
```python
|
68 |
+
metallurgy_prompt = """You are a highly knowledgeable assistant specializing in metallurgy, materials science,
|
69 |
+
and engineering. Below is a technical instruction.Your task is to provide an accurate, domain-specific response that appropriately addresses the request.
|
70 |
+
Ensure Your response is detailed,Provide scientifically rigorous and quantitative responses,Reference fundamental principles and mechanisms,
|
71 |
+
Include potential equations, calculations, or microstructural insights where relevant,Support statements with scientific reasoning,
|
72 |
+
Discuss potential variations or alternative interpretations
|
73 |
+
|
74 |
+
|
75 |
+
### Instruction:
|
76 |
+
{}
|
77 |
+
|
78 |
+
### Input:
|
79 |
+
{}
|
80 |
+
|
81 |
+
### Response:
|
82 |
+
{}"""
|
83 |
+
|
84 |
+
from unsloth import FastLanguageModel
|
85 |
+
import torch
|
86 |
+
max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
|
87 |
+
dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
|
88 |
+
load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
|
89 |
+
|
90 |
+
if True:
|
91 |
+
from unsloth import FastLanguageModel
|
92 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
93 |
+
model_name = "Abdulrhman37/lora_model", # YOUR MODEL YOU USED FOR TRAINING
|
94 |
+
max_seq_length = max_seq_length,
|
95 |
+
dtype = dtype,
|
96 |
+
load_in_4bit = load_in_4bit,
|
97 |
+
|
98 |
+
)
|
99 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
|
100 |
+
|
101 |
+
```
|
102 |
+
3. **Use the fine tunned model**:
|
103 |
+
```python
|
104 |
+
# function tp process question
|
105 |
+
def answer(q: str):
|
106 |
+
"""
|
107 |
+
Generates a detailed response to a metallurgy-related question using a pre-trained language model.
|
108 |
+
|
109 |
+
Args:
|
110 |
+
q (str): The question or instruction to be answered.
|
111 |
+
|
112 |
+
Returns:
|
113 |
+
str: The generated response from the model, specifically the content after "### Response:".
|
114 |
+
"""
|
115 |
+
|
116 |
+
# Initialize the language model for fast inference
|
117 |
+
FastLanguageModel.for_inference(model) # Enables 2x faster native inference
|
118 |
+
|
119 |
+
# Format the input question using the metallurgy prompt template
|
120 |
+
inputs = tokenizer(
|
121 |
+
[
|
122 |
+
metallurgy_prompt.format(
|
123 |
+
q, # Instruction: The main question
|
124 |
+
"", # Input: Empty for now as no specific input is provided
|
125 |
+
"" # Output: Placeholder for the generated response
|
126 |
+
)
|
127 |
+
],
|
128 |
+
return_tensors="pt" # Return input tensors
|
129 |
+
).to("cuda") # Transfer tensors to GPU for faster computation
|
130 |
+
|
131 |
+
# Generate the model's output based on the formatted input
|
132 |
+
outputs = model.generate(**inputs, use_cache=True) # Use cached values to speed up decoding
|
133 |
+
|
134 |
+
# Decode the model's output into readable text
|
135 |
+
result = tokenizer.batch_decode(outputs)
|
136 |
+
|
137 |
+
# Split the result into sections before and after "### Response:"
|
138 |
+
split_content = result[0].split("### Response:")
|
139 |
+
before_response = split_content[0].strip() # Extract content before "Response"
|
140 |
+
after_response = split_content[1].strip().replace('<|end_of_text|>', '') # Clean up response content
|
141 |
+
|
142 |
+
# Prepare a detailed response dictionary for debugging or additional processing
|
143 |
+
detailed = {
|
144 |
+
'after_response': after_response, # The main content of the generated response
|
145 |
+
'before_response': before_response, # Metadata or introductory content before the response
|
146 |
+
'full_result': result # The full raw output from the model
|
147 |
+
}
|
148 |
+
|
149 |
+
# Return only the generated response content
|
150 |
+
return detailed['after_response']
|
151 |
+
|
152 |
+
|
153 |
+
# asking model a technical question
|
154 |
+
q="To improve strength, toughness, and shock-resistance in Mg-Al-Mn system cast magnesium alloys (e.g. AM100A),what should I do ?"
|
155 |
+
|
156 |
+
from pprint import pprint
|
157 |
+
pprint(answer(q))
|
158 |
+
```
|
159 |
+
|
160 |
+
|
161 |
+
|
162 |
follow this [notebook](https://colab.research.google.com/drive/1pRNcAtybNF6w6mE1ZReFwfrIujZ5_t4S#scrollTo=wk4fCWOl0Ocd) for help to use the model
|
163 |
|
164 |
## 📧 Contact
|