Views
No views yet
Qwen/Qwen2.5-Coder-7B-Instruct
to explain Python code in plain, beginner-friendly English — it gives the
overall purpose, then walks through the code part by part, explaining each
programming term as it goes (for someone with zero Python knowledge).1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from peft import PeftModel
4
5BASE = "Qwen/Qwen2.5-Coder-7B-Instruct"
6ADAPTER = "AyushPatel28/PyExplain-qwen-coder-7b"
7
8bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4",
9 bnb_4bit_compute_dtype=torch.float16)
10tok = AutoTokenizer.from_pretrained(BASE)
11model = AutoModelForCausalLM.from_pretrained(BASE, quantization_config=bnb, device_map="auto")
12model = PeftModel.from_pretrained(model, ADAPTER)
13
14code = "def reverse(s):\n return s[::-1]"
15msgs = [{"role": "system", "content": "Explain Python code simply and accurately."},
16 {"role": "user", "content": f"Explain this code:\n```python\n{code}\n```"}]
17prompt = tok.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
18inputs = tok(prompt, return_tensors="pt", add_special_tokens=False).to(model.device)
19out = model.generate(**inputs, max_new_tokens=300, do_sample=False)
20print(tok.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))