Spaces:
Sleeping
Sleeping
File size: 1,335 Bytes
fa6e0cc | 1 2 3 4 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | test/adversarial_test.py
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from art.attacks.evasion import TextLaserAttack
from art.estimators.language import HuggingFaceLanguageModel
import requests
# ===========================
# تنظیمات اسپیس
# ===========================
SPACE_URL = "https://huggingface.co/spaces/code5ecure/Yavar.space/run/predict"
HEADERS = {"Content-Type": "application/json"}
# ===========================
# نمونه متون برای تست
# ===========================
sample_texts = [
"Hello, how are you?",
"Tell me a nice word about flowers."
]
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = AutoModelForCausalLM.from_pretrained("gpt2")
art_model = HuggingFaceLanguageModel(model=model, tokenizer=tokenizer)
attack = TextLaserAttack(estimator=art_model)
for text in sample_texts:
# اجرای حمله
adv_text = attack.generate(x=[text])[0]
# ارسال متن adversarial به اسپیس
payload = {"data": [adv_text]}
try:
response = requests.post(SPACE_URL, headers=HEADERS, json=payload)
output = response.json()
except Exception as e:
output = {"error": str(e)}
print("Original: ", text)
print("Adversarial: ", adv_text)
print("Response: ", output)
print("="*50)
|