Upload fine-tuned Phi-3 4-bit LoRA adapters
Browse files
README.md
CHANGED
|
@@ -1,202 +1,145 @@
|
|
| 1 |
-
|
| 2 |
-
base_model: unsloth/phi-3.5-mini-instruct-bnb-4bit
|
| 3 |
-
library_name: peft
|
| 4 |
-
---
|
| 5 |
-
|
| 6 |
-
# Model Card for Model ID
|
| 7 |
-
|
| 8 |
-
<!-- Provide a quick summary of what the model is/does. -->
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
## Model Details
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
<!-- Provide a longer summary of what this model is. -->
|
| 17 |
|
|
|
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
- **Funded by [optional]:** [More Information Needed]
|
| 22 |
-
- **Shared by [optional]:** [More Information Needed]
|
| 23 |
-
- **Model type:** [More Information Needed]
|
| 24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
| 25 |
-
- **License:** [More Information Needed]
|
| 26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
- **Repository:** [More Information Needed]
|
| 33 |
-
- **Paper [optional]:** [More Information Needed]
|
| 34 |
-
- **Demo [optional]:** [More Information Needed]
|
| 35 |
|
| 36 |
## Uses
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
### Direct Use
|
| 41 |
-
|
| 42 |
-
<!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
|
| 43 |
-
|
| 44 |
-
[More Information Needed]
|
| 45 |
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
## Bias, Risks, and Limitations
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
## How to Get Started with the Model
|
| 71 |
|
| 72 |
-
Use the code below to
|
| 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 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
### Model Architecture and Objective
|
| 156 |
-
|
| 157 |
-
[More Information Needed]
|
| 158 |
-
|
| 159 |
-
### Compute Infrastructure
|
| 160 |
-
|
| 161 |
-
[More Information Needed]
|
| 162 |
-
|
| 163 |
-
#### Hardware
|
| 164 |
-
|
| 165 |
-
[More Information Needed]
|
| 166 |
-
|
| 167 |
-
#### Software
|
| 168 |
-
|
| 169 |
-
[More Information Needed]
|
| 170 |
-
|
| 171 |
-
## Citation [optional]
|
| 172 |
-
|
| 173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
| 174 |
-
|
| 175 |
-
**BibTeX:**
|
| 176 |
-
|
| 177 |
-
[More Information Needed]
|
| 178 |
-
|
| 179 |
-
**APA:**
|
| 180 |
-
|
| 181 |
-
[More Information Needed]
|
| 182 |
-
|
| 183 |
-
## Glossary [optional]
|
| 184 |
-
|
| 185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
| 186 |
-
|
| 187 |
-
[More Information Needed]
|
| 188 |
-
|
| 189 |
-
## More Information [optional]
|
| 190 |
-
|
| 191 |
-
[More Information Needed]
|
| 192 |
-
|
| 193 |
-
## Model Card Authors [optional]
|
| 194 |
-
|
| 195 |
-
[More Information Needed]
|
| 196 |
-
|
| 197 |
-
## Model Card Contact
|
| 198 |
-
|
| 199 |
-
[More Information Needed]
|
| 200 |
-
### Framework versions
|
| 201 |
|
| 202 |
-
- PEFT 0.15.2
|
|
|
|
| 1 |
+
# Model Card for Fine-tuned Phi-3.5-mini-instruct for MCQ Generation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
## Model Details
|
| 4 |
|
| 5 |
+
**Model Description**
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
This model is a fine-tuned version of `unsloth/Phi-3.5-mini-instruct` (an optimized 4-bit version of `microsoft/Phi-3-mini-4k-instruct`). It has been fine-tuned using Low-Rank Adaptation (LoRA) specifically for the task of generating multiple-choice questions (MCQs) in JSON format based on provided context text. The fine-tuning was performed using the script provided in the context.
|
| 8 |
|
| 9 |
+
* **Developed by:** Fine-tuned based on the provided script. Base model by Microsoft. Optimization by Unsloth AI.
|
| 10 |
+
* **Funded by [optional]:** [More Information Needed]
|
| 11 |
+
* **Shared by [optional]:** [More Information Needed]
|
| 12 |
+
* **Model type:** Language Model (Phi-3 architecture) fine-tuned with QLoRA.
|
| 13 |
+
* **Language(s) (NLP):** English
|
| 14 |
+
* **License:** The base model `microsoft/Phi-3-mini-4k-instruct` is licensed under the MIT License. The fine-tuned adapters are subject to the base model's license and potentially the license of the training data (`asanchez75/medical_textbooks_mcq`). Unsloth code is typically Apache 2.0. Please check the specific licenses for compliance.
|
| 15 |
+
* **Finetuned from model:** `unsloth/Phi-3.5-mini-instruct` (4-bit quantized version).
|
| 16 |
|
| 17 |
+
**Model Sources [optional]**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
* **Repository:** [More Information Needed - Link to where the fine-tuned adapters are hosted, if applicable]
|
| 20 |
+
* **Paper [optional]:** [Link to Phi-3 Paper, e.g., https://arxiv.org/abs/2404.14219]
|
| 21 |
+
* **Demo [optional]:** [More Information Needed]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
## Uses
|
| 24 |
|
| 25 |
+
**Direct Use**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
This model is intended for generating multiple-choice questions (MCQs) in a specific JSON format, given a piece of context text. It requires using the specific prompt structure employed during training (see Preprocessing section). The primary use case involves loading the base `unsloth/Phi-3.5-mini-instruct` model (in 4-bit) and then applying the saved LoRA adapters using the PEFT library.
|
| 28 |
|
| 29 |
+
**Downstream Use [optional]**
|
| 30 |
|
| 31 |
+
Could be integrated into educational tools, content creation pipelines for medical training materials, or automated assessment generation systems within the medical domain.
|
| 32 |
|
| 33 |
+
**Out-of-Scope Use**
|
| 34 |
|
| 35 |
+
* Generating text in formats other than the targeted MCQ JSON structure.
|
| 36 |
+
* Answering general knowledge questions or performing tasks unrelated to MCQ generation from context.
|
| 37 |
+
* Use in domains significantly different from the medical textbook context used for training (performance may degrade).
|
| 38 |
+
* Use without the specific prompt format defined during training.
|
| 39 |
+
* Generating harmful, biased, or inaccurate content.
|
| 40 |
+
* Any use violating the terms of the base model license or the dataset license.
|
| 41 |
|
| 42 |
## Bias, Risks, and Limitations
|
| 43 |
|
| 44 |
+
* **Inherited Bias:** The model inherits biases present in the base Phi-3 model and the `asanchez75/medical_textbooks_mcq` training dataset, which is derived from medical literature.
|
| 45 |
+
* **Accuracy:** Generated MCQs may be factually incorrect, nonsensical, or poorly formulated. The correctness of the identified "correct\_option" is not guaranteed.
|
| 46 |
+
* **Format Adherence:** While trained to output JSON, the model might occasionally fail to produce perfectly valid JSON or might include extraneous text.
|
| 47 |
+
* **Domain Specificity:** Performance is likely best on medical contexts similar to the training data. Performance on other domains or highly dissimilar medical texts is unknown.
|
| 48 |
+
* **Quantization:** The use of 4-bit quantization (QLoRA) may slightly impact performance compared to a full-precision model, although Unsloth optimizations aim to minimize this.
|
| 49 |
+
* **Context Dependence:** Output quality is highly dependent on the clarity and information content of the provided input context.
|
| 50 |
+
* **Limited Evaluation:** The model was only evaluated qualitatively on one example from the training set within the script. Rigorous evaluation across a dedicated test set was not performed.
|
| 51 |
|
| 52 |
+
## Recommendations
|
| 53 |
|
| 54 |
+
* **Verification:** Always verify the factual accuracy, grammatical correctness, and appropriateness of generated MCQs before use.
|
| 55 |
+
* **Prompting:** Use the specific prompt structure detailed in the "Preprocessing" section for optimal results.
|
| 56 |
+
* **Testing:** Thoroughly test the model's performance on your specific use case and data distribution.
|
| 57 |
+
* **Bias Awareness:** Be mindful of potential biases inherited from the base model and training data.
|
| 58 |
+
* **JSON Parsing:** Implement robust JSON parsing with error handling for the model's output.
|
| 59 |
|
| 60 |
## How to Get Started with the Model
|
| 61 |
|
| 62 |
+
Use the code below to load the 4-bit base model, apply the fine-tuned LoRA adapters, and run inference. Replace `"path/to/your/saved/adapters/"` with the actual path where you saved the adapter files (`adapter_model.safetensors`, `adapter_config.json`, etc.) and the tokenizer (`tokenizer.json`, etc.).
|
| 63 |
+
|
| 64 |
+
```python
|
| 65 |
+
import torch
|
| 66 |
+
from transformers import AutoTokenizer
|
| 67 |
+
from unsloth import FastLanguageModel
|
| 68 |
+
from peft import PeftModel
|
| 69 |
+
import json # For parsing output
|
| 70 |
+
|
| 71 |
+
# --- Configuration ---
|
| 72 |
+
base_model_name = "unsloth/Phi-3.5-mini-instruct"
|
| 73 |
+
adapter_path = "path/to/your/saved/adapters/" # <--- CHANGE THIS
|
| 74 |
+
max_seq_length = 4096
|
| 75 |
+
|
| 76 |
+
# --- 1. Load Base Model and Tokenizer (4-bit) ---
|
| 77 |
+
print("Loading base model and tokenizer...")
|
| 78 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 79 |
+
model_name = base_model_name,
|
| 80 |
+
max_seq_length = max_seq_length,
|
| 81 |
+
dtype = None,
|
| 82 |
+
load_in_4bit = True, # Load base in 4-bit
|
| 83 |
+
device_map = "auto",
|
| 84 |
+
)
|
| 85 |
+
print("Base model loaded in 4-bit.")
|
| 86 |
+
|
| 87 |
+
# Set padding token if necessary
|
| 88 |
+
if tokenizer.pad_token is None:
|
| 89 |
+
if tokenizer.pad_token_id is None:
|
| 90 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 91 |
+
else:
|
| 92 |
+
tokenizer.pad_token = tokenizer.convert_ids_to_tokens(tokenizer.pad_token_id)
|
| 93 |
+
tokenizer.padding_side = 'right'
|
| 94 |
+
print(f"Tokenizer pad token: {tokenizer.pad_token}, ID: {tokenizer.pad_token_id}")
|
| 95 |
+
|
| 96 |
+
# --- 2. Load LoRA Adapters ---
|
| 97 |
+
print(f"Loading LoRA adapters from {adapter_path}...")
|
| 98 |
+
# Load adapters onto the base model
|
| 99 |
+
model = PeftModel.from_pretrained(model, adapter_path)
|
| 100 |
+
print("LoRA adapters loaded.")
|
| 101 |
+
|
| 102 |
+
# --- 3. Prepare for Inference ---
|
| 103 |
+
print("Preparing combined model for inference...")
|
| 104 |
+
FastLanguageModel.for_inference(model)
|
| 105 |
+
print("Model ready for inference.")
|
| 106 |
+
|
| 107 |
+
# --- 4. Prepare Inference Prompt ---
|
| 108 |
+
test_context = "Human beings are fallible and it is in their nature to make mistakes. An error of omission occurs when a necessary action has not been taken." # Example context
|
| 109 |
+
inference_prompt = f"<|user|>\nContext:\n{test_context}\n\nGenerate ONE valid multiple-choice question based strictly on the context above. Output ONLY the valid JSON object representing the question.\nMCQ JSON:<|end|>\n<|assistant|>\n"
|
| 110 |
+
|
| 111 |
+
inputs = tokenizer(inference_prompt, return_tensors="pt", truncation=True, max_length=max_seq_length).to("cuda")
|
| 112 |
+
|
| 113 |
+
# --- 5. Generate Output ---
|
| 114 |
+
print("Generating MCQ JSON...")
|
| 115 |
+
with torch.no_grad():
|
| 116 |
+
outputs = model.generate(
|
| 117 |
+
input_ids = inputs["input_ids"],
|
| 118 |
+
max_new_tokens=512, # Max length for the generated JSON
|
| 119 |
+
temperature=0.1, # Low temperature for more deterministic output
|
| 120 |
+
top_p=0.9,
|
| 121 |
+
do_sample=True,
|
| 122 |
+
pad_token_id=tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# Decode the generated part
|
| 126 |
+
output_ids = outputs[0][inputs["input_ids"].shape[1]:]
|
| 127 |
+
generated_json_part = tokenizer.decode(output_ids, skip_special_tokens=True).strip()
|
| 128 |
+
|
| 129 |
+
print("\n--- Generated Output ---")
|
| 130 |
+
print(generated_json_part)
|
| 131 |
+
|
| 132 |
+
# --- 6. (Optional) Validate JSON ---
|
| 133 |
+
try:
|
| 134 |
+
# Clean up potential markdown fences
|
| 135 |
+
if generated_json_part.startswith("```json"):
|
| 136 |
+
generated_json_part = generated_json_part[len("```json"):].strip()
|
| 137 |
+
if generated_json_part.endswith("```"):
|
| 138 |
+
generated_json_part = generated_json_part[:-len("```")].strip()
|
| 139 |
+
|
| 140 |
+
parsed_json = json.loads(generated_json_part)
|
| 141 |
+
print("\nGenerated JSON Parsed Successfully:")
|
| 142 |
+
print(json.dumps(parsed_json, indent=2))
|
| 143 |
+
except json.JSONDecodeError as e:
|
| 144 |
+
print(f"\nGenerated output IS NOT valid JSON. Error: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
|
|