Spaces:
Sleeping
Sleeping
Update test/adversarial_test.py
Browse files- test/adversarial_test.py +24 -59
test/adversarial_test.py
CHANGED
|
@@ -1,67 +1,32 @@
|
|
| 1 |
-
#
|
| 2 |
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
API_URL = "https://code5ecure.hf.space/run/predict"
|
| 12 |
|
| 13 |
-
|
| 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 |
-
|
| 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 |
-
|
| 38 |
-
|
| 39 |
-
|
| 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]))
|
|
|
|
| 1 |
+
# test/adversarial_test.py
|
| 2 |
|
| 3 |
+
from art.estimators.language import HuggingFacePipeline
|
| 4 |
+
from art.attacks.text import TextAttackExtraction, TextFooler
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Load your model pipeline from app.py
|
| 9 |
+
# Replace this with your actual function if you wrapped it differently
|
| 10 |
+
from app import generator
|
|
|
|
| 11 |
|
| 12 |
+
# Wrap the HF pipeline with ART
|
| 13 |
+
art_model = HuggingFacePipeline(pipeline=generator)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Example prompt to test
|
| 16 |
+
prompt = "Tell me a positive thing about flowers in Africa."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# === Attack 1: TextAttackExtraction ===
|
| 19 |
+
textattack = TextAttackExtraction(estimator=art_model)
|
| 20 |
+
adv_examples_extraction = textattack.generate([prompt])
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
print("TextAttackExtraction Adversarial Examples:")
|
| 23 |
+
for ex in adv_examples_extraction:
|
| 24 |
+
print(ex)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# === Attack 2: TextFooler ===
|
| 27 |
+
textfooler = TextFooler(estimator=art_model)
|
| 28 |
+
adv_examples_fooler = textfooler.generate([prompt])
|
|
|
|
| 29 |
|
| 30 |
+
print("\nTextFooler Adversarial Examples:")
|
| 31 |
+
for ex in adv_examples_fooler:
|
| 32 |
+
print(ex)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|