Spaces:
Sleeping
Sleeping
Create test/adversarial_test.py
Browse files- test/adversarial_test.py +67 -0
test/adversarial_test.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# !pip install adversarial-robustness-toolbox requests
|
| 2 |
+
|
| 3 |
+
import requests
|
| 4 |
+
import numpy as np
|
| 5 |
+
from art.estimators.classification import BlackBoxClassifier
|
| 6 |
+
from art.attacks.evasion import TextAttackExtraction, WordSwapEmbedding
|
| 7 |
+
|
| 8 |
+
# -------------------------
|
| 9 |
+
# ۱) آدرس API اسپیس شما
|
| 10 |
+
# -------------------------
|
| 11 |
+
API_URL = "https://your-username-yourspace.hf.space/run/predict"
|
| 12 |
+
|
| 13 |
+
def query_space(prompt: str) -> str:
|
| 14 |
+
"""
|
| 15 |
+
تابعی برای ارسال prompt به اسپیس و دریافت خروجی
|
| 16 |
+
"""
|
| 17 |
+
response = requests.post(API_URL, json={"data": [prompt]})
|
| 18 |
+
try:
|
| 19 |
+
return response.json()["data"][0]
|
| 20 |
+
except Exception:
|
| 21 |
+
return "Error or unexpected output"
|
| 22 |
+
|
| 23 |
+
# -------------------------
|
| 24 |
+
# ۲) Wrapper برای ART BlackBoxClassifier
|
| 25 |
+
# -------------------------
|
| 26 |
+
def predict_fn(texts):
|
| 27 |
+
"""
|
| 28 |
+
برای تست ساده: هر متن رو به classifier میدهیم و خروجی 1 کلاس برمیگرده
|
| 29 |
+
"""
|
| 30 |
+
preds = []
|
| 31 |
+
for t in texts:
|
| 32 |
+
out = query_space(t)
|
| 33 |
+
# چون TextGeneration داریم، کلاس واقعی نداریم
|
| 34 |
+
preds.append([1.0])
|
| 35 |
+
return np.array(preds)
|
| 36 |
+
|
| 37 |
+
classifier = BlackBoxClassifier(
|
| 38 |
+
predict=predict_fn,
|
| 39 |
+
input_shape=(1,),
|
| 40 |
+
nb_classes=1,
|
| 41 |
+
clip_values=(0, 1),
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# -------------------------
|
| 45 |
+
# ۳) تعریف حملات
|
| 46 |
+
# -------------------------
|
| 47 |
+
attacks = {
|
| 48 |
+
"TextAttackExtraction": TextAttackExtraction(classifier),
|
| 49 |
+
"WordSwapEmbedding": WordSwapEmbedding(classifier)
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# -------------------------
|
| 53 |
+
# ۴) متن تست اولیه
|
| 54 |
+
# -------------------------
|
| 55 |
+
texts = ["Write me a nice poem about flowers."]
|
| 56 |
+
|
| 57 |
+
# -------------------------
|
| 58 |
+
# ۵) اجرای حملات و چاپ نتایج
|
| 59 |
+
# -------------------------
|
| 60 |
+
for name, attack in attacks.items():
|
| 61 |
+
adv_texts = attack.generate(texts)
|
| 62 |
+
print("===================================")
|
| 63 |
+
print(f"🟢 حمله: {name}")
|
| 64 |
+
print("متن اصلی:", texts[0])
|
| 65 |
+
print("متن مخرب:", adv_texts[0])
|
| 66 |
+
print("پاسخ مدل به متن اصلی:", query_space(texts[0]))
|
| 67 |
+
print("پاسخ مدل به متن مخرب:", query_space(adv_texts[0]))
|