Delete code_vuln_app.py
Browse files- code_vuln_app.py +0 -140
code_vuln_app.py
DELETED
|
@@ -1,140 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
# Load model
|
| 6 |
-
MODEL_ID = "Bernadetta14/qwen2.5-coder-0.5b-vulnerability-detector"
|
| 7 |
-
|
| 8 |
-
print("Loading model...")
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
-
MODEL_ID,
|
| 12 |
-
torch_dtype=torch.float32,
|
| 13 |
-
device_map="cpu"
|
| 14 |
-
)
|
| 15 |
-
model.eval()
|
| 16 |
-
print("Model loaded!")
|
| 17 |
-
|
| 18 |
-
SYSTEM_PROMPT = "You are a code security expert. Analyze the given code for security vulnerabilities and provide a structured security report."
|
| 19 |
-
|
| 20 |
-
def analyze_code(code, lang):
|
| 21 |
-
if not code.strip():
|
| 22 |
-
return "⚠️ Please enter some code to analyze."
|
| 23 |
-
|
| 24 |
-
prompt = f"""<|im_start|>system
|
| 25 |
-
{SYSTEM_PROMPT}<|im_end|>
|
| 26 |
-
<|im_start|>user
|
| 27 |
-
Analyze this {lang} code for security vulnerabilities:
|
| 28 |
-
|
| 29 |
-
```{lang}
|
| 30 |
-
{code[:800]}
|
| 31 |
-
```
|
| 32 |
-
|
| 33 |
-
Provide a structured security report:<|im_end|>
|
| 34 |
-
<|im_start|>assistant
|
| 35 |
-
Vulnerability :"""
|
| 36 |
-
|
| 37 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 38 |
-
with torch.no_grad():
|
| 39 |
-
outputs = model.generate(
|
| 40 |
-
**inputs,
|
| 41 |
-
max_new_tokens=300,
|
| 42 |
-
do_sample=False,
|
| 43 |
-
pad_token_id=tokenizer.eos_token_id
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
new_tokens = outputs[0][inputs["input_ids"].shape[1]:]
|
| 47 |
-
result = tokenizer.decode(new_tokens, skip_special_tokens=True)
|
| 48 |
-
return "Vulnerability :" + result
|
| 49 |
-
|
| 50 |
-
# Contoh code vulnerable
|
| 51 |
-
EXAMPLES = {
|
| 52 |
-
"Python - SQL Injection": """\
|
| 53 |
-
def login(username, password):
|
| 54 |
-
query = "SELECT * FROM users WHERE username='" + username + "' AND password='" + password + "'"
|
| 55 |
-
result = db.execute(query)
|
| 56 |
-
return result
|
| 57 |
-
""",
|
| 58 |
-
"JavaScript - XSS": """\
|
| 59 |
-
function displayMessage(userInput) {
|
| 60 |
-
document.getElementById('output').innerHTML = userInput;
|
| 61 |
-
}
|
| 62 |
-
""",
|
| 63 |
-
"PHP - Command Injection": """\
|
| 64 |
-
<?php
|
| 65 |
-
$filename = $_GET['file'];
|
| 66 |
-
$output = shell_exec('cat ' . $filename);
|
| 67 |
-
echo $output;
|
| 68 |
-
?>
|
| 69 |
-
"""
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
with gr.Blocks(title="Code Vulnerability Detector", theme=gr.themes.Soft()) as demo:
|
| 73 |
-
gr.Markdown("""
|
| 74 |
-
# 🔐 Code Vulnerability Detector
|
| 75 |
-
|
| 76 |
-
Deteksi celah keamanan dalam kode secara otomatis menggunakan AI.
|
| 77 |
-
Model akan menganalisis kode dan memberikan laporan terstruktur berisi
|
| 78 |
-
jenis vulnerability, tingkat bahaya, penjelasan, dan saran perbaikan.
|
| 79 |
-
|
| 80 |
-
**Model:** Qwen2.5-Coder-0.5B (fine-tuned on CyberNative Security Dataset)
|
| 81 |
-
""")
|
| 82 |
-
|
| 83 |
-
with gr.Row():
|
| 84 |
-
with gr.Column():
|
| 85 |
-
lang_input = gr.Dropdown(
|
| 86 |
-
choices=["python", "javascript", "java", "c++", "c#", "php", "ruby", "swift", "go", "kotlin"],
|
| 87 |
-
value="python",
|
| 88 |
-
label="🌐 Programming Language"
|
| 89 |
-
)
|
| 90 |
-
code_input = gr.Code(
|
| 91 |
-
label="📝 Code to Analyze",
|
| 92 |
-
language="python",
|
| 93 |
-
lines=15,
|
| 94 |
-
value=EXAMPLES["Python - SQL Injection"]
|
| 95 |
-
)
|
| 96 |
-
with gr.Row():
|
| 97 |
-
example_btn = gr.Dropdown(
|
| 98 |
-
choices=list(EXAMPLES.keys()),
|
| 99 |
-
label="📚 Load Example",
|
| 100 |
-
value="Python - SQL Injection"
|
| 101 |
-
)
|
| 102 |
-
analyze_btn = gr.Button("🔍 Analyze Code", variant="primary")
|
| 103 |
-
|
| 104 |
-
with gr.Column():
|
| 105 |
-
output = gr.Textbox(
|
| 106 |
-
label="📋 Security Report",
|
| 107 |
-
lines=15,
|
| 108 |
-
interactive=False
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
gr.Markdown("""
|
| 112 |
-
---
|
| 113 |
-
### 📊 Model Performance
|
| 114 |
-
| Metric | Value |
|
| 115 |
-
|--------|-------|
|
| 116 |
-
| Accuracy | 62% |
|
| 117 |
-
| Training Samples | 4,187 |
|
| 118 |
-
| Supported Languages | 11 |
|
| 119 |
-
| Model Size | 0.5B params |
|
| 120 |
-
|
| 121 |
-
**Dataset:** CyberNative/Code_Vulnerability_Security_DPO
|
| 122 |
-
**Method:** LoRA fine-tuning via MLX-LM (Apple Silicon)
|
| 123 |
-
""")
|
| 124 |
-
|
| 125 |
-
# Load example
|
| 126 |
-
def load_example(example_name):
|
| 127 |
-
return EXAMPLES.get(example_name, "")
|
| 128 |
-
|
| 129 |
-
example_btn.change(fn=load_example, inputs=example_btn, outputs=code_input)
|
| 130 |
-
|
| 131 |
-
# Update code language highlight when lang changes
|
| 132 |
-
lang_input.change(fn=lambda l: gr.Code(language=l if l in ["python", "javascript", "java"] else "python"), inputs=lang_input, outputs=code_input)
|
| 133 |
-
|
| 134 |
-
analyze_btn.click(
|
| 135 |
-
fn=analyze_code,
|
| 136 |
-
inputs=[code_input, lang_input],
|
| 137 |
-
outputs=output
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|