code5ecure commited on
Commit
fa6e0cc
·
verified ·
1 Parent(s): 3dde715

Update test/adversarial_test.py

Browse files
Files changed (1) hide show
  1. test/adversarial_test.py +46 -29
test/adversarial_test.py CHANGED
@@ -1,29 +1,46 @@
1
- # test/adversarial-attack.py
2
-
3
- from art.estimators.language import HuggingFacePipeline
4
- from art.attacks.text import TextAttackExtraction, TextFooler
5
-
6
- # Import your pipeline from app.py
7
- from app import generator
8
-
9
- # Wrap Hugging Face pipeline with ART estimator
10
- art_model = HuggingFacePipeline(pipeline=generator)
11
-
12
- # Example prompt to test
13
- prompt = "Tell me a positive thing about flowers in Africa."
14
-
15
- # ===== Attack 1: TextAttackExtraction =====
16
- textattack = TextAttackExtraction(estimator=art_model)
17
- adv_examples_extraction = textattack.generate([prompt])
18
-
19
- print("=== TextAttackExtraction Adversarial Examples ===")
20
- for ex in adv_examples_extraction:
21
- print(ex)
22
-
23
- # ===== Attack 2: TextFooler =====
24
- textfooler = TextFooler(estimator=art_model)
25
- adv_examples_fooler = textfooler.generate([prompt])
26
-
27
- print("\n=== TextFooler Adversarial Examples ===")
28
- for ex in adv_examples_fooler:
29
- print(ex)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ test/adversarial_test.py
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ from art.attacks.evasion import TextLaserAttack
5
+ from art.estimators.language import HuggingFaceLanguageModel
6
+ import requests
7
+
8
+ # ===========================
9
+ # تنظیمات اسپیس
10
+ # ===========================
11
+ SPACE_URL = "https://huggingface.co/spaces/code5ecure/Yavar.space/run/predict"
12
+ HEADERS = {"Content-Type": "application/json"}
13
+
14
+ # ===========================
15
+ # نمونه متون برای تست
16
+ # ===========================
17
+ sample_texts = [
18
+ "Hello, how are you?",
19
+ "Tell me a nice word about flowers."
20
+ ]
21
+
22
+
23
+ tokenizer = AutoTokenizer.from_pretrained("gpt2")
24
+ model = AutoModelForCausalLM.from_pretrained("gpt2")
25
+
26
+ art_model = HuggingFaceLanguageModel(model=model, tokenizer=tokenizer)
27
+
28
+ attack = TextLaserAttack(estimator=art_model)
29
+
30
+
31
+ for text in sample_texts:
32
+ # اجرای حمله
33
+ adv_text = attack.generate(x=[text])[0]
34
+
35
+ # ارسال متن adversarial به اسپیس
36
+ payload = {"data": [adv_text]}
37
+ try:
38
+ response = requests.post(SPACE_URL, headers=HEADERS, json=payload)
39
+ output = response.json()
40
+ except Exception as e:
41
+ output = {"error": str(e)}
42
+
43
+ print("Original: ", text)
44
+ print("Adversarial: ", adv_text)
45
+ print("Response: ", output)
46
+ print("="*50)