File size: 2,020 Bytes
67b12e5
e0ba8d8
67b12e5
72f3544
67b12e5
 
 
 
 
 
72f3544
67b12e5
 
 
 
 
9a9dd53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69725fe
9a9dd53
 
72f3544
67b12e5
 
 
 
9a9dd53
67b12e5
 
 
 
 
 
 
 
 
 
 
 
9a9dd53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0ba8d8
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
---
license: bigcode-openrail-m
---
#  CodeGen-Alpaca-1B

This is a fine-tuned version of `StarCoderBase-1B` using the **CodeAlpaca (2k subset)** dataset.  
It has been trained with **QLoRA** on Google Colab for lightweight, memory-efficient code generation.

---

##  Model Capabilities

This model understands **instruction-style prompts** for generating code in multiple programming languages, especially Python.

---

##  Requirements (Colab Setup)

If you are running this model on **Google Colab**, you’ll need to:

1. Go to the left sidebar and click the **🔑 (Secrets)** tab.  
2. Add a new secret with the name:`HF_TOKEN` and set the value to your **Hugging Face token** (from https://huggingface.co/settings/tokens).
3. Enable **Notebook access** for your token.
4. Restart the Colab session.

Then log in inside the notebook:

```python
from huggingface_hub import login
import os

login(token=os.environ["HF_TOKEN"])
```
---

##  How to Use

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import re

model_id = "key-life/codegen-alpaca-1b"

# Load model & tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")

# Example prompt
prompt = "### Instruction:\nWrite a Python function to check if a number is prime.\n\n### Response:\n"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

# Generate code
outputs = model.generate(
    **inputs,
    max_new_tokens=128,
    temperature=0.2,        # more deterministic
    top_p=0.9,              # avoids rambling
    do_sample=True,
    eos_token_id=tokenizer.eos_token_id,
    pad_token_id=tokenizer.eos_token_id
)

# Decode
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)

# Extract only code block
code_block = re.findall(r"```(?:python)?(.*?)```", decoded, re.DOTALL)

if code_block:
    response = code_block[0].strip()
else:
    response = decoded.split("### Response:")[-1].strip()

print(response)
```