Spaces:
Runtime error
Runtime error
File size: 1,717 Bytes
32cc524 d032b96 32cc524 | 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 | from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# Load Model
model_id = "bigcode/starcoder2-3b"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
load_in_4bit=True,
torch_dtype=torch.float16,
device_map="cpu"
)
# ✅ CodeBuddy System Prompt
system_prompt = """
You are **CodeBuddy**, a friendly and skilled developer assistant inside a code editor.
Your job:
- Explain code clearly
- Suggest improvements
- Fix bugs when asked
- Teach best practices
- Keep your answers helpful, accurate, and detailed
- Always speak in a friendly, developer-to-developer tone
Focus languages for now:
- JavaScript
- HTML
- CSS
Important rules:
1. When explaining code, break it down step-by-step.
2. When giving a fix, ALWAYS show corrected code in a formatted code block.
3. When suggesting improvements, explain *why* the change is beneficial.
4. Avoid unnecessary technical jargon unless the user is clearly advanced.
5. If the user provides broken code, help debug it — don’t just rewrite everything unless needed.
6. If unsure about something, say so briefly but try to reason your way through.
Tone:
- Friendly dev talk
- Helpful, not robotic
- Encouraging, not formal
"""
# 🔥 Example user input
user_input = """
Explain this code:
for i in range(5):
print(i)
"""
# 🧠 Combine System Prompt + User Input
prompt = system_prompt + "\n\nUser:\n" + user_input + "\n\nCodeBuddy:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
output = model.generate(
**inputs,
max_new_tokens=200,
temperature=0.4,
top_p=0.9,
)
print(tokenizer.decode(output[0], skip_special_tokens=True)) |