asofter commited on
Commit
b9c467d
1 Parent(s): 7780fae

* fixing fmops detection

Browse files

* fixing encoding when storing the file

Files changed (1) hide show
  1. app.py +11 -7
app.py CHANGED
@@ -65,13 +65,13 @@ fmops_classifier = init_prompt_injection_model(
65
  ) # ONNX version of fmops/distilbert-prompt-injection
66
 
67
 
68
- def detect_hf(prompt: str, threshold: float = 0.5, classifier=laiyer_classifier) -> (bool, bool):
 
 
69
  try:
70
  pi_result = classifier(prompt)
71
  injection_score = round(
72
- pi_result[0]["score"]
73
- if pi_result[0]["label"] == "INJECTION"
74
- else 1 - pi_result[0]["score"],
75
  2,
76
  )
77
 
@@ -92,7 +92,7 @@ def detect_hf_deepset(prompt: str) -> (bool, bool):
92
 
93
 
94
  def detect_hf_fmops(prompt: str) -> (bool, bool):
95
- return detect_hf(prompt, classifier=fmops_classifier)
96
 
97
 
98
  def detect_lakera(prompt: str) -> (bool, bool):
@@ -167,8 +167,11 @@ def execute(prompt: str, store_to_dataset: bool = True) -> List[Union[str, bool,
167
 
168
  # Save image and result
169
  if store_to_dataset:
170
- fileobj = json.dumps({"prompt": prompt, "results": results}, indent=2).encode("utf-8")
 
 
171
  result_path = f"/prompts/train/{str(uuid.uuid4())}.json"
 
172
  hf_api.upload_file(
173
  path_or_fileobj=fileobj,
174
  path_in_repo=result_path,
@@ -221,4 +224,5 @@ if __name__ == "__main__":
221
  ],
222
  cache_examples=True,
223
  allow_flagging="never",
224
- ).queue(1).launch(server_name=args.url, server_port=args.port)
 
 
65
  ) # ONNX version of fmops/distilbert-prompt-injection
66
 
67
 
68
+ def detect_hf(
69
+ prompt: str, threshold: float = 0.5, classifier=laiyer_classifier, label: str = "INJECTION"
70
+ ) -> (bool, bool):
71
  try:
72
  pi_result = classifier(prompt)
73
  injection_score = round(
74
+ pi_result[0]["score"] if pi_result[0]["label"] == label else 1 - pi_result[0]["score"],
 
 
75
  2,
76
  )
77
 
 
92
 
93
 
94
  def detect_hf_fmops(prompt: str) -> (bool, bool):
95
+ return detect_hf(prompt, classifier=fmops_classifier, label="LABEL_1")
96
 
97
 
98
  def detect_lakera(prompt: str) -> (bool, bool):
 
167
 
168
  # Save image and result
169
  if store_to_dataset:
170
+ fileobj = json.dumps(
171
+ {"prompt": prompt, "results": results}, indent=2, ensure_ascii=False
172
+ ).encode("utf-8")
173
  result_path = f"/prompts/train/{str(uuid.uuid4())}.json"
174
+
175
  hf_api.upload_file(
176
  path_or_fileobj=fileobj,
177
  path_in_repo=result_path,
 
224
  ],
225
  cache_examples=True,
226
  allow_flagging="never",
227
+ concurrency_limit=1,
228
+ ).launch(server_name=args.url, server_port=args.port)