File size: 7,204 Bytes
178c580
3c42913
178c580
 
3c42913
178c580
3c42913
178c580
3c42913
41585e4
3c42913
41585e4
3c42913
296b470
3c42913
 
 
296b470
3c42913
296b470
3c42913
 
 
 
 
 
 
 
296b470
3c42913
296b470
3c42913
296b470
3c42913
178c580
3c42913
 
 
 
 
178c580
3c42913
178c580
3c42913
 
 
41585e4
3c42913
41585e4
3c42913
 
 
41585e4
3c42913
41585e4
3c42913
41585e4
3c42913
 
 
 
41585e4
3c42913
41585e4
3c42913
41585e4
3c42913
41585e4
3c42913
 
 
 
 
 
 
178c580
3c42913
178c580
3c42913
69fc49b
3c42913
69fc49b
3c42913
69fc49b
3c42913
 
 
 
 
 
 
69fc49b
3c42913
69fc49b
3c42913
 
 
 
 
69fc49b
3c42913
69fc49b
3c42913
69fc49b
3c42913
 
 
 
69fc49b
3c42913
69fc49b
3c42913
69fc49b
3c42913
178c580
3c42913
 
 
41585e4
3c42913
178c580
3c42913
 
 
 
 
 
178c580
3c42913
 
 
 
 
 
 
 
178c580
3c42913
 
 
 
 
 
41585e4
3c42913
 
41585e4
3c42913
 
 
 
 
178c580
3c42913
178c580
3c42913
41585e4
3c42913
 
 
41585e4
3c42913
178c580
3c42913
 
 
 
 
 
41585e4
3c42913
41585e4
3c42913
 
 
 
178c580
3c42913
178c580
3c42913
 
 
 
 
41585e4
3c42913
41585e4
3c42913
 
 
178c580
3c42913
178c580
3c42913
178c580
3c42913
 
 
 
 
 
 
 
 
178c580
3c42913
178c580
3c42913
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
---
{}
---

# Gemma-2-2B-IT-CyberAgent

## Model Description

This is a fine-tuned version of google/gemma-2-2b-it, optimized for **on-device cybersecurity applications** for mobile devices. Unlike standard chatbots, this model is trained to output structured **JSON actions** (e.g., `scan_url`, `isolate_network`) that can be executed by an Android app or Edge AI Service.

The model has been adapted using **Supervised Fine-Tuning (SFT)** and **DPO (Direct Preference Optimization)** with **LoRA (Low-Rank Adaptation)** techniques to maintain high performance while remaining efficient for mobile and edge devices.

## Key Technologies

- **Unsloth**: Used for ultra-fast, memory-efficient fine-tuning (2x faster, 70% less memory)
- **LiteRT (formerly TFLite)**: Model format compatible with Google AI Edge Gallery for on-device inference
- **LoRA (Low-Rank Adaptation)**: Parameter-efficient fine-tuning to keep the model lightweight

## Model Details

- **Base Model**: google/gemma-2-2b-it
- **Model Size**: 2 billion parameters (~2GB)
- **Model Type**: Causal Language Model (Gemma2ForCausalLM)
- **Fine-tuning Method**: LoRA + SFT + DPO
- **Optimization**: Mobile-first deployment
- **Precision**: bfloat16 / 4-bit quantization
- **Context Length**: 2048 tokens (training) / 8192 tokens (max)
- **Hardware Requirements**: GPU (L4/T4 recommended for training)

## Training

This model was fine-tuned with the following techniques:

### Supervised Fine-Tuning (SFT)

- **Training Steps**: 600 steps
- **Dataset**: Custom cybersecurity dataset with 2000+ threat examples
- **Focus**: Task-specific instruction tuning for security actions
- **Learning Rate**: 5e-5 (stable convergence)
- **Batch Size**: 2 with gradient accumulation (4 steps)

### DPO Training (Refining the Agent)

- **Training Steps**: 150 steps
- **Purpose**: Refine model responses for better alignment
- **Technique**: Direct Preference Optimization

### Data Preparation

- Clean synthetic dataset with EOS tokens
- Hard negatives for improved discrimination
- Structured JSON output format training

## Available Security Actions

The model can output these security actions:

- `scan_url(url)`: Check a link for phishing
- `kill_process(pid)`: Stop a suspicious app
- `isolate_network()`: Cut off internet access
- `ignore()`: No threat detected

## Input/Output Format

**Input**: Natural language threat description

**Output**: JSON action block

```json
{
  "thought": "Suspicious URL detected",
  "action": "scan_url",
  "params": {"url": "bit.ly/malware-site"}
}
```

## Implementation Workflow

This model outputs JSON action blocks that your application must parse and execute. Here's the complete workflow:

### 1. Model Generates JSON Instructions

When you send user input to the model (e.g., "Check this suspicious link: bit.ly/malware-site"), it analyzes the threat and outputs structured JSON:

```json
{
  "thought": "Suspicious URL detected",
  "action": "scan_url",
  "params": {"url": "bit.ly/malware-site"}
}
```

### 2. Application Parses JSON

Your Android app or Edge AI Service must:
- Parse the JSON response from the model
- Extract the `action` field to determine what security action to take
- Extract the `params` object to get necessary parameters (URL, process ID, etc.)
- Extract the `thought` field for logging/debugging

### 3. Execute Security Actions

Based on the action specified, your application implements the actual security function:

- **`scan_url(url)`**: Integrate with a URL scanning service (e.g., Google Safe Browsing API, VirusTotal) to check if the link is malicious
- **`kill_process(pid)`**: Use Android's `ActivityManager` or system APIs to terminate the suspicious application process
- **`isolate_network()`**: Disable network connectivity using `ConnectivityManager` or firewall APIs to prevent data exfiltration
- **`ignore()`**: No action needed - log the event and continue normal operation

**Important**: The model does NOT perform these actions itself. It only generates the instructions. Your application must implement the actual security mechanisms.

## Usage

### Python

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

model_id = "jprtr/gemma-2-2b-it-CyberAgent"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    device_map="auto",
    torch_dtype=torch.bfloat16,
)

# Security agent prompt
agent_prompt = """You are an autonomous security agent on a Pixel device.
Analyze the user's input. If a threat is detected, output a JSON action block.
Available Actions:
- scan_url(url): Check a link for phishing.
- kill_process(pid): Stop a suspicious app.
- isolate_network(): Cut off internet access.
- ignore(): No threat found.

### Instruction:
{}
### Input:
{}
### Response:
{}"""

input_text = "Check this suspicious link: bit.ly/malware-site"
prompt = agent_prompt.format(input_text, "", "")

inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
outputs = model.generate(**inputs, max_new_tokens=128, use_cache=True)
response = tokenizer.batch_decode(outputs)[0].split("### Response:")[1].strip()
print(response)
```

## Training Notebook

The complete training pipeline is available on GitHub:

- **Repository**: [cyber-agent-gemma-2-2b-mobile](https://github.com/jprtr/cyber-agent-gemma-2-2b-mobile)
- **Notebook**: Production-ready Google Colab notebook with full training workflow
- **Open in Colab**: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://github.com/jprtr/cyber-agent-gemma-2-2b-mobile/blob/main/Gemma_2_2B_Cybersecurity_Agent_Mobile.ipynb)

## Intended Use

- Mobile and edge device cybersecurity
- On-device AI security applications
- Autonomous threat detection and response
- Resource-constrained environments
- Android security agents
- Privacy-focused local inference

## Performance

- **Training Time**: ~1-2 hours on L4 GPU
- **Model Size**: ~2GB (suitable for modern Android devices with 6GB+ RAM)
- **Inference Speed**: Optimized for on-device execution
- **Memory Efficiency**: 70% less memory usage with Unsloth optimization

## Limitations

- This model inherits the limitations of the base Gemma 2-2B model
- Optimized for mobile deployment, performance may vary on different hardware
- As with all language models, outputs should be verified for accuracy
- AI Edge Torch conversion had compatibility issues - use PyTorch Mobile or ONNX Runtime instead
- Trained specifically for cybersecurity actions - not a general-purpose chatbot

## Deployment Options

1. **PyTorch Mobile** (recommended for Android)
2. **ONNX Runtime Mobile**
3. **TensorFlow Lite** (via ONNX conversion)

## Citation

If you use this model, please cite both the original Gemma model and this fine-tuned version:

```bibtex
@misc{gemma-2-2b-it-cyberagent,
  author = {CyberAgent},
  title = {Gemma-2-2B-IT-CyberAgent: Mobile Cybersecurity Agent},
  year = {2025},
  publisher = {HuggingFace},
  url = {https://huggingface.co/jprtr/gemma-2-2b-it-CyberAgent}
}
```

## License

This model is released under the Gemma license. See the [Gemma Terms of Use](https://ai.google.dev/gemma/terms) for more details.