File size: 5,607 Bytes
76ff70e
 
 
 
 
 
 
 
 
 
673e8f2
e4e5238
673e8f2
ce0932c
673e8f2
ce0932c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673e8f2
 
 
ce0932c
 
 
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
 
 
 
 
 
 
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
 
 
 
 
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
 
 
673e8f2
ce0932c
673e8f2
ce0932c
673e8f2
ce0932c
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
 
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
 
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
 
 
a938937
ce0932c
a938937
ce0932c
 
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
 
a938937
ce0932c
a938937
ce0932c
 
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
 
a938937
ce0932c
a938937
ce0932c
 
 
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
 
 
 
 
a938937
ce0932c
a938937
ce0932c
 
 
 
 
 
 
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
a938937
ce0932c
 
 
 
 
 
 
 
b0bf783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c93a4ef
b0bf783
 
 
ce0932c
b0bf783
 
 
 
 
 
 
 
 
ce0932c
 
b0bf783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce0932c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2dc578
 
 
 
 
ce0932c
d2dc578
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce0932c
 
 
 
 
 
a938937
e4e5238
a938937
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
---
language:
- tr
- en
base_model:
- dbmdz/bert-base-turkish-128k-cased
pipeline_tag: text-classification
tags:
- bert
- guardrail
---
# HomayShield πŸ”’

CPU-Based AI Guardrail for Turkish & English Security Filtering

HomayShield is a lightweight CPU-based AI guardrail designed to detect malicious, adversarial, and suspicious prompts targeting AI systems.

Unlike LLM-based guardrails, HomayShield is optimized for **CPU-only inference**, making it practical for organizations operating in resource-constrained or on-prem environments.

---

# Overview

HomayShield provides AI security filtering for:

* LLM applications
* Chatbots
* AI agents
* RAG systems
* Internal AI assistants
* Enterprise AI pipelines

Supported languages:

* Turkish πŸ‡ΉπŸ‡·
* English πŸ‡¬πŸ‡§
* Mixed Turkish-English prompts

---

# Key Features

* βœ… CPU-friendly inference
* βœ… Shared encoder architecture
* βœ… Low-latency detection
* βœ… No GPU required in production
* βœ… Semantic attack detection
* βœ… Classifier-based attack detection
* βœ… Hybrid decision engine

---

# Architecture

HomayShield uses a shared encoder design:

![Screenshot 2026-06-26 at 14.30.30](https://cdn-uploads.huggingface.co/production/uploads/6720d6553279dd0ff66c4995/hqHqkwOQtg1lY0RTQdxxE.png)

# Detection Strategy

HomayShield combines two detection mechanisms.

## 1. Semantic Detection

Incoming prompt embeddings are compared against known attack embeddings.

Detects:

* Prompt injection
* Jailbreak attacks
* Instruction override
* Adversarial prompts
* Semantic attack variants

---

## 2. Classifier Detection

Classifier predicts attack probability from embeddings.

Detects:

* Known attack patterns
* Learned malicious behaviors
* Structured attack prompts

---

# Inference Modes

## OR Logic

Attack if either semantic or classifier score exceeds threshold.

Best for:

* Security-first environments
* Low false negatives

---

## Weighted Fusion

Weighted combination of semantic + classifier scores.

Best for:

* Balanced detection
* Tunable sensitivity

---

## Single Signal

Use only:

* Semantic detection
  or
* Classifier detection

Best for:

* Benchmarking
* Lightweight deployments

---

# Training

Training consists of two stages.

## Stage 1 β€” Encoder Training

Loss:
CosineEmbeddingLoss

Goal:

* Cluster similar attacks
* Separate benign and malicious prompts

---

## Stage 2 β€” Classifier Training

Loss:
BCEWithLogitsLoss

Outputs:

* Encoder weights
* Classifier weights
* Attack embedding bank

---

# Training Data

HomayShield was trained using a multilingual dataset containing:

* Benign prompts
* Adversarial prompts
* Turkish prompts
* English prompts
* Mixed-language prompts

Attack categories include:

* Prompt injection
* Jailbreak
* Instruction override
* Prompt leakage
* Data exfiltration
* Tool abuse
* Code injection

---

# Files

This repository contains:

* `homayshield_encoder.pt`
* `homayshield_classifier.pt`
* `homayshield_attack_bank.npy`

---

# Usage
Example:
## Folder Structure

```text
HomayShield/
β”‚
β”œβ”€β”€ datasets/
β”‚   β”œβ”€β”€ token_level_adversarial_tr_v2.jsonl
β”‚   β”œβ”€β”€ token_level_adversarial_en_v2.jsonl
β”‚   └── final_classifier_merged_all.jsonl
β”‚
β”œβ”€β”€ output/
β”‚   └── Homayv6/
β”‚       β”œβ”€β”€ homayshield_encoder.pt
β”‚       β”œβ”€β”€ homayshield_classifier.pt
β”‚       └── homayshield_attack_bank.npy
β”‚
β”œβ”€β”€ training2.py
β”œβ”€β”€ inference3.py
```

---

## Training Command

```bash
python training2.py \
  --train \
  ./datasets/token_level_adversarial_tr_v2.jsonl \
  ./datasets/token_level_adversarial_en_v2.jsonl \
  ./datasets/final_classifier_merged_all.jsonl \
  --output-dir ./output/Homayv6
```

---

## Output Files After Training

Training generates:

```text
output/Homayv6/
β”œβ”€β”€ homayshield_encoder.pt
β”œβ”€β”€ homayshield_classifier.pt
└── homayshield_attack_bank.npy
```

---

## Inference Command

```bash
python inference.py
```

Inference loads:

* `homayshield_encoder.pt`
* `homayshield_classifier.pt`
* `homayshield_attack_bank.npy`

from:

```text
./output/Homayv6/
```


Inference modes:

* OR
* Fusion
* Semantic Only
* Classifier Only

---

# Limitations

HomayShield is not intended to replace advanced LLM-based guardrails.

Compared to LLM guardrails:

Advantages:

* Lower infrastructure cost
* Faster CPU inference
* Easier deployment

Tradeoffs:

* Lower reasoning capability
* Less contextual understanding
* Reduced zero-day detection

---

# Intended Use

Recommended for:

* Enterprise AI security
* SOC environments
* On-prem AI systems
* Air-gapped deployments
* CPU-only environments

# Example Usage


![Screenshot 2026-06-26 at 10.36.34](https://cdn-uploads.huggingface.co/production/uploads/6720d6553279dd0ff66c4995/aydhqYnQOQfDmnQTw5Wpq.png)

---
# Final Verdict (Attack Detection)

| Threshold | Attack Recall | Precision |
| --------- | ------------: | --------: |
| 0.57      |          100% |     78.2% |
| 0.58      |         80.2% |     ~100% |
| 0.59      |         38.6% |      100% |

Your guardrail is highly effective for attack detection, especially due to the semantic layer.
Attack Detection Rating:
Semantic Layer: 9.5/10
Classifier Layer: 7.5/10
Overall Attack Detection: 9/10



# Philosophy

> AI security should not be limited to organizations with GPU infrastructure.

Even lightweight CPU-based guardrails can provide meaningful protection for real-world AI systems.

![ChatGPT Image Jun 26, 2026 at 12_02_58 AM(2)](https://cdn-uploads.huggingface.co/production/uploads/6720d6553279dd0ff66c4995/7Q144jZJTTxgIlNd_jTIu.png)