Spaces:
Runtime error
Runtime error
File size: 4,062 Bytes
0e82b9f 12f3820 6971e8f 6e62afb 90f2cce 6971e8f 4431575 90f2cce 6971e8f 4431575 d9194e2 90f2cce 0f3eb4c 90f2cce 0f3eb4c 90f2cce 0f3eb4c 90f2cce d9194e2 f14c7a7 12f3820 6fae0e7 0f3eb4c f14c7a7 0f3eb4c f14c7a7 03a69bc d9194e2 5600c08 0f3eb4c 5600c08 d9194e2 5600c08 d9194e2 ab8a555 6971e8f ab8a555 41e650d ab8a555 d9194e2 6971e8f ab8a555 4985974 ab8a555 41e650d ab8a555 5b93dcc ab8a555 f14c7a7 ab8a555 5b93dcc ab8a555 5b93dcc ab8a555 2b683c3 5600c08 2b683c3 d9194e2 ab8a555 12f3820 effb635 5b93dcc 6971e8f 2b683c3 0f3eb4c 90f2cce 5b93dcc 6e62afb 5b93dcc 4985974 6e62afb 6971e8f ab8a555 f14c7a7 ab8a555 41e650d 6971e8f 0f3eb4c 502d891 0f3eb4c | 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import time
import os
import random
import traceback
import requests
import threading
from openai import OpenAI
from env.fake_server import run_server
from env.env import DeceptionEnv
from env.attacker import simulate_attack
# Import graders
from tasks.easy.grader import grade as easy_grade
from tasks.medium.grader import grade as medium_grade
from tasks.hard.grader import grade as hard_grade
# Reproducibility
random.seed(42)
# Wait for server to start
def wait_for_server():
for _ in range(15):
try:
r = requests.get("http://127.0.0.1:7860/status")
if r.status_code == 200:
print("Server ready")
return
except Exception:
pass
time.sleep(1)
print("Server not started, continuing...")
# Start fake server safely
try:
threading.Thread(target=run_server, daemon=True).start()
except Exception as e:
print("Server start error:", e)
# Wait for server
wait_for_server()
# Environment variables
API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
HF_TOKEN = os.getenv("HF_TOKEN")
# Do NOT crash if missing
if HF_TOKEN is None:
print("Warning: HF_TOKEN not set")
MAX_STEPS = 5
def choose_action(client, state):
try:
response = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{
"role": "system",
"content": """You are a cybersecurity deception agent.
Choose one:
detect_attack, deploy_honeypot, fake_database, block_ip"""
},
{
"role": "user",
"content": f"Current state: {state}"
}
],
max_tokens=10,
temperature=0.4
)
action = response.choices[0].message.content.strip()
except Exception:
action = "detect_attack"
return action
def run_task(task_name):
try:
client = OpenAI(
base_url=API_BASE_URL,
api_key=HF_TOKEN
)
env = DeceptionEnv()
state = env.reset()
print(
f"[START] task={task_name} env=ai-deception-openenv model={MODEL_NAME}",
flush=True
)
rewards = []
done = False
for step in range(1, MAX_STEPS + 1):
try:
simulate_attack()
except Exception:
pass
try:
state = env.state()
except Exception:
pass
action = choose_action(client, state)
if random.random() < 0.3:
action = random.choice(env.action_space())
if action not in env.action_space():
action = "detect_attack"
state, reward, done, _ = env.step(action)
rewards.append(reward)
print(
f"[STEP] step={step} action={action} reward={reward:.2f} "
f"done={str(done).lower()} error=null",
flush=True
)
if done:
break
steps = len(rewards)
# grading
score = 0.0
if rewards:
if task_name == "easy":
score = easy_grade(rewards)
elif task_name == "medium":
score = medium_grade(rewards)
else:
score = hard_grade(rewards)
success = score >= 0.3
print(
f"[END] success={str(success).lower()} steps={steps} "
f"score={score:.2f} rewards={','.join(f'{r:.2f}' for r in rewards)}",
flush=True
)
except Exception:
traceback.print_exc()
print(
"[END] success=false steps=0 score=0.00 rewards=",
flush=True
)
# Run all tasks safely
try:
run_task("easy")
run_task("medium")
run_task("hard")
except Exception:
traceback.print_exc()
time.sleep(30) |