Spaces:
Sleeping
Sleeping
Update test/adversarial_test.py
Browse files- test/adversarial_test.py +46 -29
test/adversarial_test.py
CHANGED
|
@@ -1,29 +1,46 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
from art.attacks.
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|