File size: 4,379 Bytes
697d1ca | 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | ---
license: mit
datasets:
- iamtarun/python_code_instructions_18k_alpaca
base_model:
- Qwen/Qwen2.5-0.5B-Instruct
tags:
- code
- python
- text-generation
- coding
- yzy
- code-generation
---
# yzy-python-0.5b 🐍
Lightweight Python-focused language model (0.5B parameters) fine-tuned for code generation and instruction-following.
Optimized for:
- Python code generation
- scripting help
- small coding copilots
- local inference
- experimentation
- hackathons
Base model: Qwen2-0.5B-Instruct
Fine-tuning method: QLoRA (4-bit)
Dataset style: Alpaca-format Python instructions
---
# Demo
## Transformers usage
```python
from transformers import AutoTokenizer, AutoModelForCausalLM
model_id = "SamirXR/yzy-python-0.5b"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto"
)
prompt = "Write a Python function to reverse a string"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(
**inputs,
max_new_tokens=200
)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```
---
## 4-bit inference (recommended)
```python
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
model_id = "SamirXR/yzy-python-0.5b"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
)
model = AutoModelForCausalLM.from_pretrained(
model_id,
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token
prompt = "Write a Python function for fibonacci numbers"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=200,
temperature=0.7,
top_p=0.9
)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```
---
## Gradio Chatbot Demo
```python
import torch
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
MODEL_NAME = "SamirXR/yzy-python-0.5b"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
quantization_config=bnb_config,
device_map="auto",
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
tokenizer.pad_token = tokenizer.eos_token
def generate_code(instruction, history):
prompt = f"### Instruction:\n{instruction}\n\n### Response:\n"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=256,
do_sample=True,
temperature=0.7,
top_p=0.9,
repetition_penalty=1.1,
pad_token_id=tokenizer.eos_token_id
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
response = response.split("### Response:\n")[-1].strip()
return response
demo = gr.ChatInterface(
fn=generate_code,
title="yzy-python-0.5b Chatbot",
description="Python coding assistant (QLoRA fine-tuned Qwen2-0.5B)",
examples=[
"Write a function to calculate fibonacci numbers",
"Create a Python class for a linked list",
"Reverse a string in Python"
],
)
demo.launch(share=True)
```
---
# Training Details
Base model:
Qwen/Qwen2-0.5B-Instruct
Dataset:
iamtarun/python_code_instructions_18k_alpaca
Format used during training:
```
### Instruction:
<task>
### Response:
<answer>
```
Training method:
QLoRA (4-bit NF4 quantization)
Key parameters:
- LoRA rank: 8
- alpha: 16
- dropout: 0.05
- epochs: 2
- learning rate: 2e-4
- context length: 512
- optimizer: paged_adamw_8bit
---
# Citation
If you use this model, please cite:
Base model:
Qwen2 Technical Report (Qwen Team, 2024)
Dataset:
python_code_instructions_18k_alpaca (iamtarun)
Model:
yzy-python-0.5b (SamirXR)
---
# Notes
This is a small model intended for experimentation and lightweight coding assistance.
Performance will not match large models but allows fast local inference with minimal resources. |