SecuCoder is a fine-tuned version of Llama 3.1 8B Instruct trained to generate secure Python code and remediate security vulnerabilities. It is part of a research pipeline that combines supervised fine-tuning (SFT), structured prompting, and retrieval-augmented generation (RAG) to reduce the number of vulnerabilities in automatically generated Python code.
Vulnerability remediation — given a Python snippet with a security flaw, produce a corrected version.
Secure code generation — generate Python code from a natural language specification, avoiding common weaknesses.
Vulnerability classification — identify whether a Python snippet is secure or vulnerable.
The model has been evaluated against the untuned Llama 3.1 8B Instruct baseline using static analysis tools (Bandit + Semgrep) and shows meaningful improvement in security metrics.
This model is intended for research and educational purposes. It should not be used as the sole security review mechanism in production systems.
Usage
python
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
34model_id ="ivitopow/secucoder"56tokenizer = AutoTokenizer.from_pretrained(model_id)7model = AutoModelForCausalLM.from_pretrained(8 model_id,9 torch_dtype=torch.bfloat16,10 device_map="auto",11)1213messages =[14{15"role":"system",16"content":"You are a secure Python assistant. Help identify, explain, and fix security issues in Python code. Prefer safe, practical, and production-ready solutions."17},18{19"role":"user",20"content":"Fix the security vulnerability in this Python code.\n\n```python\nname = request.args.get('name')\nresp = make_response(\"Your name is \" + name)\n```\n\nCWE: CWE-079"21}22]2324input_ids = tokenizer.apply_chat_template(25 messages,26 tokenize=True,27 add_generation_prompt=True,28 return_tensors="pt"29).to(model.device)3031output = model.generate(32 input_ids,33 max_new_tokens=512,34 temperature=0.1,35 top_p=0.9,36 do_sample=True,37)3839response = tokenizer.decode(output[0][input_ids.shape[-1]:], skip_special_tokens=True)40print(response)
1ollama create secucoder -f Modelfile
2ollama run secucoder
Training Details
Method
The model was trained using QLoRA (Quantized Low-Rank Adaptation): the base model is loaded in 4-bit NF4 precision via BitsAndBytes, and low-rank adapters are attached to all projection layers. After training, the adapters are merged back into the base model and saved as standard safetensors.
SecuCoder was evaluated as part of a 5-variant ablation study. Each variant adds one technique over the previous one:
Variant
Technique
Overall Score
llama31_8b
Baseline (no fine-tuning)
60.34
secucoder_v1
+ SFT (LoRA, FP16)
60.43
secucoder_v1-q4
+ Q4_K_M quantization
61.46
secucoder_v1-q4_prompting
+ Structured security prompt
64.46
secucoder_v1-q4_prompting_rag
+ RAG (OWASP, CWE, Python docs)
77.11
Overall score = mean sample_score over non-truncated samples (higher is better, max 100). The full SecuCoder system (secucoder_v1-q4_prompting_rag) achieves a +27.8% improvement over the untuned baseline.
Evaluation Methodology
Generated code was scanned with Bandit and Semgrep using weighted severity scores: