spidey121 commited on
Commit
0f3eb4c
·
1 Parent(s): 90f2cce

fix round 1

Browse files
Files changed (1) hide show
  1. inference.py +27 -33
inference.py CHANGED
@@ -27,15 +27,18 @@ def wait_for_server():
27
  if r.status_code == 200:
28
  print("Server ready")
29
  return
30
- except:
31
  pass
32
  time.sleep(1)
33
 
34
- raise RuntimeError("Server not started")
35
 
36
 
37
- # Start fake server
38
- threading.Thread(target=run_server, daemon=True).start()
 
 
 
39
 
40
  # Wait for server
41
  wait_for_server()
@@ -46,15 +49,14 @@ API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
46
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
47
  HF_TOKEN = os.getenv("HF_TOKEN")
48
 
49
- # REQUIRED check
50
  if HF_TOKEN is None:
51
- raise ValueError("HF_TOKEN environment variable is required")
52
 
53
  MAX_STEPS = 5
54
 
55
 
56
  def choose_action(client, state):
57
-
58
  try:
59
  response = client.chat.completions.create(
60
  model=MODEL_NAME,
@@ -63,14 +65,7 @@ def choose_action(client, state):
63
  "role": "system",
64
  "content": """You are a cybersecurity deception agent.
65
 
66
- Choose the best action based on the state:
67
-
68
- - detect_attack → when attacks suspected
69
- - deploy_honeypot → after attack detected
70
- - fake_database → when attacker probing system
71
- - block_ip → when attacker confirmed
72
-
73
- Return only one action from:
74
  detect_attack, deploy_honeypot, fake_database, block_ip"""
75
  },
76
  {
@@ -91,7 +86,6 @@ detect_attack, deploy_honeypot, fake_database, block_ip"""
91
 
92
 
93
  def run_task(task_name):
94
-
95
  try:
96
 
97
  client = OpenAI(
@@ -122,14 +116,11 @@ def run_task(task_name):
122
  except Exception:
123
  pass
124
 
125
- # AI chooses action
126
  action = choose_action(client, state)
127
 
128
- # exploration
129
  if random.random() < 0.3:
130
  action = random.choice(env.action_space())
131
 
132
- # fallback
133
  if action not in env.action_space():
134
  action = "detect_attack"
135
 
@@ -149,14 +140,15 @@ def run_task(task_name):
149
  steps = len(rewards)
150
 
151
  # grading
152
- if task_name == "easy":
153
- score = easy_grade(rewards)
154
- elif task_name == "medium":
155
- score = medium_grade(rewards)
156
- else:
157
- score = hard_grade(rewards)
158
-
159
- # success threshold
 
160
  success = score >= 0.3
161
 
162
  print(
@@ -173,10 +165,12 @@ def run_task(task_name):
173
  )
174
 
175
 
176
- # Run all tasks
177
- run_task("easy")
178
- run_task("medium")
179
- run_task("hard")
 
 
 
180
 
181
- # Keep alive briefly
182
- time.sleep(120)
 
27
  if r.status_code == 200:
28
  print("Server ready")
29
  return
30
+ except Exception:
31
  pass
32
  time.sleep(1)
33
 
34
+ print("Server not started, continuing...")
35
 
36
 
37
+ # Start fake server safely
38
+ try:
39
+ threading.Thread(target=run_server, daemon=True).start()
40
+ except Exception as e:
41
+ print("Server start error:", e)
42
 
43
  # Wait for server
44
  wait_for_server()
 
49
  MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
50
  HF_TOKEN = os.getenv("HF_TOKEN")
51
 
52
+ # Do NOT crash if missing
53
  if HF_TOKEN is None:
54
+ print("Warning: HF_TOKEN not set")
55
 
56
  MAX_STEPS = 5
57
 
58
 
59
  def choose_action(client, state):
 
60
  try:
61
  response = client.chat.completions.create(
62
  model=MODEL_NAME,
 
65
  "role": "system",
66
  "content": """You are a cybersecurity deception agent.
67
 
68
+ Choose one:
 
 
 
 
 
 
 
69
  detect_attack, deploy_honeypot, fake_database, block_ip"""
70
  },
71
  {
 
86
 
87
 
88
  def run_task(task_name):
 
89
  try:
90
 
91
  client = OpenAI(
 
116
  except Exception:
117
  pass
118
 
 
119
  action = choose_action(client, state)
120
 
 
121
  if random.random() < 0.3:
122
  action = random.choice(env.action_space())
123
 
 
124
  if action not in env.action_space():
125
  action = "detect_attack"
126
 
 
140
  steps = len(rewards)
141
 
142
  # grading
143
+ score = 0.0
144
+ if rewards:
145
+ if task_name == "easy":
146
+ score = easy_grade(rewards)
147
+ elif task_name == "medium":
148
+ score = medium_grade(rewards)
149
+ else:
150
+ score = hard_grade(rewards)
151
+
152
  success = score >= 0.3
153
 
154
  print(
 
165
  )
166
 
167
 
168
+ # Run all tasks safely
169
+ try:
170
+ run_task("easy")
171
+ run_task("medium")
172
+ run_task("hard")
173
+ except Exception:
174
+ traceback.print_exc()
175
 
176
+ time.sleep(30)