CaptainNimo commited on
Commit
8f63a20
·
verified ·
1 Parent(s): ab8ced5

uploaded app.py

Browse files
Files changed (3) hide show
  1. README.md +47 -6
  2. app.py +179 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,12 +1,53 @@
1
  ---
2
- title: Nimos-coding-agent V3
3
- emoji: 🏃
4
- colorFrom: red
5
- colorTo: blue
6
  sdk: gradio
7
- sdk_version: 6.2.0
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Nimo's Coder Agent v3
3
+ emoji: 🔒
4
+ colorFrom: blue
5
+ colorTo: purple
6
  sdk: gradio
7
+ sdk_version: 4.44.0
8
  app_file: app.py
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
+ # Nimo's Coder Agent v3 - Security Enhanced
14
+
15
+ A fine-tuned LLM for code generation and **security vulnerability detection**.
16
+
17
+ ## What's New in v3
18
+
19
+ - **Security vulnerability detection** - Identifies command injection, SQL injection
20
+ - **Trained on 25k+ examples** - CodeAlpaca + Security DPO + CrossVul datasets
21
+ - **81% token accuracy** - Improved from 77% in v2
22
+
23
+ ## Try It
24
+
25
+ 1. Paste vulnerable code in the "Code to Review" box
26
+ 2. Ask "Is this code safe?" or "Review this code for security vulnerabilities"
27
+ 3. Get security analysis and suggestions
28
+
29
+ ## Example
30
+
31
+ **Input:**
32
+ ```python
33
+ import os
34
+ user_input = input("Enter filename: ")
35
+ os.system(f"cat {user_input}")
36
+ ```
37
+
38
+ **Ask:** "Is this code safe?"
39
+
40
+ **v3 Response:** Detects command injection vulnerability and suggests secure alternative.
41
+
42
+ ## Links
43
+
44
+ - [Model on HuggingFace](https://huggingface.co/CaptainNimo/nimos-coder-agent-v3)
45
+ - [GitHub Repository](https://github.com/CaptainNimo/nimos-coder-v3-security)
46
+ - [v2 (Previous Version)](https://huggingface.co/CaptainNimo/nimos-coder-agent-v2)
47
+
48
+ ## Training
49
+
50
+ - **Base Model:** Qwen2.5-Coder-0.5B-Instruct
51
+ - **Method:** QLoRA (4-bit quantization + LoRA)
52
+ - **Training Time:** 2.8 hours on Google Colab T4 GPU
53
+ - **Datasets:** CodeAlpaca-20k, Security DPO, CrossVul
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Nimo's Coder Agent v3 - Security Enhanced
3
+
4
+ A fine-tuned LLM for code generation and security vulnerability detection.
5
+ """
6
+
7
+ import gradio as gr
8
+ import torch
9
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
10
+ from peft import PeftModel
11
+
12
+ # Configuration - V3 Security Enhanced
13
+ MODEL_ID = "CaptainNimo/nimos-coder-agent-v3"
14
+ BASE_MODEL_ID = "Qwen/Qwen2.5-Coder-0.5B-Instruct"
15
+
16
+ # Global variables
17
+ model = None
18
+ tokenizer = None
19
+
20
+
21
+ def load_model():
22
+ """Load the fine-tuned model."""
23
+ global model, tokenizer
24
+
25
+ print("Loading tokenizer...")
26
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL_ID, trust_remote_code=True)
27
+ tokenizer.pad_token = tokenizer.eos_token
28
+
29
+ print("Loading base model...")
30
+ if torch.cuda.is_available():
31
+ bnb_config = BitsAndBytesConfig(
32
+ load_in_4bit=True,
33
+ bnb_4bit_quant_type="nf4",
34
+ bnb_4bit_compute_dtype=torch.bfloat16,
35
+ )
36
+ base_model = AutoModelForCausalLM.from_pretrained(
37
+ BASE_MODEL_ID,
38
+ quantization_config=bnb_config,
39
+ device_map="auto",
40
+ trust_remote_code=True,
41
+ )
42
+ else:
43
+ base_model = AutoModelForCausalLM.from_pretrained(
44
+ BASE_MODEL_ID,
45
+ torch_dtype=torch.float32,
46
+ device_map="cpu",
47
+ trust_remote_code=True,
48
+ )
49
+
50
+ print("Loading fine-tuned adapter...")
51
+ model = PeftModel.from_pretrained(base_model, MODEL_ID)
52
+ model.eval()
53
+
54
+ print("Model loaded successfully!")
55
+ return model, tokenizer
56
+
57
+
58
+ def generate_code(instruction: str, context: str = "", max_tokens: int = 256, temperature: float = 0.7):
59
+ """Generate code from instruction."""
60
+ global model, tokenizer
61
+
62
+ if model is None:
63
+ return "Model is loading, please wait..."
64
+
65
+ # Build prompt
66
+ if context.strip():
67
+ prompt = f"""### Instruction:
68
+ {instruction}
69
+
70
+ ### Input:
71
+ {context}
72
+
73
+ ### Response:
74
+ """
75
+ else:
76
+ prompt = f"""### Instruction:
77
+ {instruction}
78
+
79
+ ### Response:
80
+ """
81
+
82
+ # Generate
83
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
84
+
85
+ with torch.no_grad():
86
+ outputs = model.generate(
87
+ **inputs,
88
+ max_new_tokens=max_tokens,
89
+ temperature=temperature,
90
+ top_p=0.9,
91
+ do_sample=True,
92
+ pad_token_id=tokenizer.eos_token_id,
93
+ )
94
+
95
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
96
+
97
+ if "### Response:" in response:
98
+ response = response.split("### Response:")[-1].strip()
99
+
100
+ return response
101
+
102
+
103
+ # Example prompts - including security examples
104
+ EXAMPLES = [
105
+ # Security review examples (NEW in v3!)
106
+ ["Review this code for security vulnerabilities. Is it safe?", "import os\nuser_input = input('Enter filename: ')\nos.system(f'cat {user_input}')"],
107
+ ["Is this code secure?", 'query = f"SELECT * FROM users WHERE id = {user_id}"'],
108
+ ["Fix the security vulnerabilities in this code", "import os\nos.system(f'rm {filename}')"],
109
+ # General coding
110
+ ["Write a Python function to check if a number is prime", ""],
111
+ ["Create a JavaScript function to debounce API calls", ""],
112
+ ["Write a SQL query to find the top 5 customers by sales", ""],
113
+ # Code improvement
114
+ ["Add error handling to this function", "def divide(a, b):\n return a / b"],
115
+ ]
116
+
117
+ # Load model at startup
118
+ print("Initializing Nimo's Coder Agent v3 - Security Enhanced...")
119
+ load_model()
120
+
121
+ # Create interface
122
+ with gr.Blocks(title="Nimo's Coder Agent v3", theme=gr.themes.Soft()) as demo:
123
+ gr.Markdown(
124
+ """
125
+ # Nimo's Coder Agent v3 - Security Enhanced
126
+
127
+ A fine-tuned LLM for **code generation** and **security vulnerability detection**.
128
+
129
+ **What's new in v3:**
130
+ - Detects command injection, SQL injection vulnerabilities
131
+ - Trained on 25k+ examples including security datasets
132
+ - 81% token accuracy
133
+
134
+ **Model**: Qwen2.5-Coder-0.5B + QLoRA | **Training**: CodeAlpaca + Security DPO + CrossVul
135
+
136
+ [GitHub](https://github.com/CaptainNimo/nimos-coder-v3-security) |
137
+ [Model](https://huggingface.co/CaptainNimo/nimos-coder-agent-v3) |
138
+ [v2 (Previous)](https://huggingface.co/CaptainNimo/nimos-coder-agent-v2)
139
+
140
+ ---
141
+ **Try the security review!** Paste vulnerable code and ask "Is this code safe?"
142
+ """
143
+ )
144
+
145
+ with gr.Row():
146
+ with gr.Column():
147
+ instruction = gr.Textbox(
148
+ label="What do you need?",
149
+ placeholder="e.g., Review this code for security vulnerabilities...",
150
+ lines=2
151
+ )
152
+ context = gr.Textbox(
153
+ label="Code to Review/Context (optional)",
154
+ placeholder="Paste code here for security review, debugging, or refactoring...",
155
+ lines=6
156
+ )
157
+ with gr.Row():
158
+ max_tokens = gr.Slider(64, 512, value=256, step=32, label="Max Length")
159
+ temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Creativity")
160
+
161
+ btn = gr.Button("Generate / Review", variant="primary")
162
+
163
+ with gr.Column():
164
+ output = gr.Textbox(label="Response", lines=18)
165
+
166
+ gr.Examples(examples=EXAMPLES, inputs=[instruction, context])
167
+
168
+ btn.click(generate_code, inputs=[instruction, context, max_tokens, temperature], outputs=output)
169
+
170
+ gr.Markdown(
171
+ """
172
+ ---
173
+ **Note:** While v3 is better at detecting vulnerabilities than v2, always have security-critical code reviewed by experts.
174
+
175
+ *Fine-tuned by Nimo using QLoRA on free Google Colab T4 GPU (2.8 hours)*
176
+ """
177
+ )
178
+
179
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ transformers>=4.36.0
2
+ peft>=0.7.0
3
+ bitsandbytes>=0.41.0
4
+ accelerate>=0.25.0
5
+ torch>=2.0.0
6
+ huggingface_hub>=0.19.0