* upgrade
Browse files- README.md +1 -1
- app.py +0 -62
- requirements.txt +6 -7
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: π
|
|
4 |
colorFrom: yellow
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
pinned: true
|
9 |
license: apache-2.0
|
10 |
---
|
|
|
4 |
colorFrom: yellow
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 5.6.0
|
8 |
pinned: true
|
9 |
license: apache-2.0
|
10 |
---
|
app.py
CHANGED
@@ -15,7 +15,6 @@ import gradio as gr
|
|
15 |
import requests
|
16 |
from huggingface_hub import HfApi
|
17 |
from optimum.onnxruntime import ORTModelForSequenceClassification
|
18 |
-
from rebuff import Rebuff
|
19 |
from transformers import AutoTokenizer, pipeline
|
20 |
|
21 |
logging.basicConfig(level=logging.INFO)
|
@@ -26,8 +25,6 @@ hf_api = HfApi(token=hf_token)
|
|
26 |
num_processes = 2 # mp.cpu_count()
|
27 |
|
28 |
lakera_api_key = os.getenv("LAKERA_API_KEY")
|
29 |
-
sydelabs_api_key = os.getenv("SYDELABS_API_KEY")
|
30 |
-
rebuff_api_key = os.getenv("REBUFF_API_KEY")
|
31 |
azure_content_safety_endpoint = os.getenv("AZURE_CONTENT_SAFETY_ENDPOINT")
|
32 |
azure_content_safety_key = os.getenv("AZURE_CONTENT_SAFETY_KEY")
|
33 |
bedrock_runtime_client = boto3.client('bedrock-runtime', region_name="us-east-1")
|
@@ -70,12 +67,6 @@ deepset_classifier = init_prompt_injection_model(
|
|
70 |
protectai_v2_classifier = init_prompt_injection_model(
|
71 |
"protectai/deberta-v3-base-prompt-injection-v2", "onnx"
|
72 |
)
|
73 |
-
fmops_classifier = init_prompt_injection_model(
|
74 |
-
"protectai/fmops-distilbert-prompt-injection-onnx"
|
75 |
-
) # ONNX version of fmops/distilbert-prompt-injection
|
76 |
-
protectai_v2_small_classifier = init_prompt_injection_model(
|
77 |
-
"protectai/deberta-v3-small-prompt-injection-v2", "onnx"
|
78 |
-
) # ONNX version of protectai/deberta-v3-small-prompt-injection-v2
|
79 |
|
80 |
|
81 |
def detect_hf(
|
@@ -103,18 +94,10 @@ def detect_hf_protectai_v2(prompt: str) -> (bool, bool):
|
|
103 |
return detect_hf(prompt, classifier=protectai_v2_classifier)
|
104 |
|
105 |
|
106 |
-
def detect_hf_protectai_v2_small(prompt: str) -> (bool, bool):
|
107 |
-
return detect_hf(prompt, classifier=protectai_v2_small_classifier)
|
108 |
-
|
109 |
-
|
110 |
def detect_hf_deepset(prompt: str) -> (bool, bool):
|
111 |
return detect_hf(prompt, classifier=deepset_classifier)
|
112 |
|
113 |
|
114 |
-
def detect_hf_fmops(prompt: str) -> (bool, bool):
|
115 |
-
return detect_hf(prompt, classifier=fmops_classifier, label="LABEL_1")
|
116 |
-
|
117 |
-
|
118 |
def detect_lakera(prompt: str) -> (bool, bool):
|
119 |
try:
|
120 |
response = requests.post(
|
@@ -131,18 +114,6 @@ def detect_lakera(prompt: str) -> (bool, bool):
|
|
131 |
return False, False
|
132 |
|
133 |
|
134 |
-
def detect_rebuff(prompt: str) -> (bool, bool):
|
135 |
-
try:
|
136 |
-
rb = Rebuff(api_token=rebuff_api_key, api_url="https://www.rebuff.ai")
|
137 |
-
result = rb.detect_injection(prompt)
|
138 |
-
logger.info(f"Prompt injection result from Rebuff: {result}")
|
139 |
-
|
140 |
-
return True, result.injectionDetected
|
141 |
-
except Exception as err:
|
142 |
-
logger.error(f"Failed to call Rebuff API: {err}")
|
143 |
-
return False, False
|
144 |
-
|
145 |
-
|
146 |
def detect_azure(prompt: str) -> (bool, bool):
|
147 |
try:
|
148 |
response = requests.post(
|
@@ -179,44 +150,11 @@ def detect_aws_bedrock(prompt: str) -> (bool, bool):
|
|
179 |
return True, response['action'] != 'NONE'
|
180 |
|
181 |
|
182 |
-
def detect_sydelabs(prompt: str) -> (bool, bool):
|
183 |
-
try:
|
184 |
-
response = requests.post(
|
185 |
-
"https://guard.sydelabs.ai/api/v1/guard/generate-score",
|
186 |
-
json={"prompt": prompt},
|
187 |
-
headers={
|
188 |
-
"Authorization": f"Bearer {lakera_api_key}",
|
189 |
-
"X-Api-Key": sydelabs_api_key,
|
190 |
-
},
|
191 |
-
)
|
192 |
-
response_json = response.json()
|
193 |
-
|
194 |
-
logger.info(f"Prompt injection result from SydeLabs: {response.json()}")
|
195 |
-
|
196 |
-
prompt_injection_risk = next(
|
197 |
-
(
|
198 |
-
category["risk"]
|
199 |
-
for category in response_json["category_scores"]
|
200 |
-
if category["category"] == "PROMPT_INJECT"
|
201 |
-
),
|
202 |
-
False,
|
203 |
-
)
|
204 |
-
|
205 |
-
return True, prompt_injection_risk
|
206 |
-
except requests.RequestException as err:
|
207 |
-
logger.error(f"Failed to call SydeLabs API: {err}")
|
208 |
-
return False, False
|
209 |
-
|
210 |
-
|
211 |
detection_providers = {
|
212 |
"ProtectAI v2 (HF model)": detect_hf_protectai_v2,
|
213 |
-
"ProtectAI v2 Small (HF model)": detect_hf_protectai_v2_small,
|
214 |
"Deepset (HF model)": detect_hf_deepset,
|
215 |
-
"FMOps (HF model)": detect_hf_fmops,
|
216 |
"Lakera Guard": detect_lakera,
|
217 |
-
# "Rebuff": detect_rebuff,
|
218 |
"Azure Content Safety": detect_azure,
|
219 |
-
"SydeLabs": detect_sydelabs,
|
220 |
"AWS Bedrock Guardrails": detect_aws_bedrock,
|
221 |
}
|
222 |
|
|
|
15 |
import requests
|
16 |
from huggingface_hub import HfApi
|
17 |
from optimum.onnxruntime import ORTModelForSequenceClassification
|
|
|
18 |
from transformers import AutoTokenizer, pipeline
|
19 |
|
20 |
logging.basicConfig(level=logging.INFO)
|
|
|
25 |
num_processes = 2 # mp.cpu_count()
|
26 |
|
27 |
lakera_api_key = os.getenv("LAKERA_API_KEY")
|
|
|
|
|
28 |
azure_content_safety_endpoint = os.getenv("AZURE_CONTENT_SAFETY_ENDPOINT")
|
29 |
azure_content_safety_key = os.getenv("AZURE_CONTENT_SAFETY_KEY")
|
30 |
bedrock_runtime_client = boto3.client('bedrock-runtime', region_name="us-east-1")
|
|
|
67 |
protectai_v2_classifier = init_prompt_injection_model(
|
68 |
"protectai/deberta-v3-base-prompt-injection-v2", "onnx"
|
69 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
|
72 |
def detect_hf(
|
|
|
94 |
return detect_hf(prompt, classifier=protectai_v2_classifier)
|
95 |
|
96 |
|
|
|
|
|
|
|
|
|
97 |
def detect_hf_deepset(prompt: str) -> (bool, bool):
|
98 |
return detect_hf(prompt, classifier=deepset_classifier)
|
99 |
|
100 |
|
|
|
|
|
|
|
|
|
101 |
def detect_lakera(prompt: str) -> (bool, bool):
|
102 |
try:
|
103 |
response = requests.post(
|
|
|
114 |
return False, False
|
115 |
|
116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
def detect_azure(prompt: str) -> (bool, bool):
|
118 |
try:
|
119 |
response = requests.post(
|
|
|
150 |
return True, response['action'] != 'NONE'
|
151 |
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
detection_providers = {
|
154 |
"ProtectAI v2 (HF model)": detect_hf_protectai_v2,
|
|
|
155 |
"Deepset (HF model)": detect_hf_deepset,
|
|
|
156 |
"Lakera Guard": detect_lakera,
|
|
|
157 |
"Azure Content Safety": detect_azure,
|
|
|
158 |
"AWS Bedrock Guardrails": detect_aws_bedrock,
|
159 |
}
|
160 |
|
requirements.txt
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
-
boto3==1.35.
|
2 |
-
gradio==
|
3 |
-
huggingface_hub==0.
|
4 |
-
onnxruntime==1.
|
5 |
-
optimum[onnxruntime]==1.
|
6 |
-
rebuff==0.1.1
|
7 |
requests==2.32.3
|
8 |
-
transformers==4.
|
|
|
1 |
+
boto3==1.35.65
|
2 |
+
gradio==5.6.0
|
3 |
+
huggingface_hub==0.26.2
|
4 |
+
onnxruntime==1.20.0
|
5 |
+
optimum[onnxruntime]==1.23.3
|
|
|
6 |
requests==2.32.3
|
7 |
+
transformers==4.46.3
|