spidey121 commited on
Commit
12f3820
·
1 Parent(s): 03a69bc

fix phase2 timeout

Browse files
Files changed (1) hide show
  1. inference.py +72 -82
inference.py CHANGED
@@ -1,6 +1,6 @@
1
- import os
2
  import threading
3
  import time
 
4
  import random
5
  import traceback
6
  from openai import OpenAI
@@ -11,63 +11,53 @@ from env.fake_server import run_server
11
  from env.env import DeceptionEnv
12
  from env.attacker import simulate_attack
13
 
 
 
 
14
 
15
- # If running inside HuggingFace Space → start server only
16
- if os.getenv("SPACE_ID"):
17
-
18
- run_server()
19
 
 
20
 
21
- else:
 
 
 
22
 
23
- API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
24
- MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
25
- HF_TOKEN = os.getenv("HF_TOKEN")
26
 
27
- try:
28
-
29
- threading.Thread(target=run_server, daemon=True).start()
30
- time.sleep(3)
31
-
32
- client = OpenAI(
33
- base_url=API_BASE_URL,
34
- api_key=HF_TOKEN
35
- )
36
-
37
- env = DeceptionEnv()
38
- state = env.reset()
39
-
40
- print(
41
- "[START] task=ai-deception env=cyber-security model=AI-agent",
42
- flush=True
43
- )
44
 
45
- rewards = []
46
- history = []
47
 
48
- for step in range(1, 4):
49
 
50
- try:
51
- simulate_attack()
52
- except:
53
- pass
54
 
55
- try:
56
- state = env.state()
57
- except:
58
- pass
59
 
60
- summary = {
61
- "failed_logins": state.get("failed_logins", 0),
62
- "port_scans": state.get("port_scans", 0),
63
- "suspicious_ips": len(state.get("suspicious_ips", []))
64
- }
65
 
66
- prompt = f"""
67
- You are cybersecurity system.
68
-
69
- Previous: {history}
70
- Current: {summary}
71
 
72
  Return one:
73
  detect_attack
@@ -75,50 +65,50 @@ deploy_honeypot
75
  block_ip
76
  """
77
 
78
- try:
79
- response = client.chat.completions.create(
80
- model=MODEL_NAME,
81
- messages=[{"role": "user", "content": prompt}],
82
- temperature=0.2,
83
- max_tokens=20,
84
- timeout=15
85
- )
86
-
87
- action = response.choices[0].message.content.strip()
88
-
89
- except:
90
 
91
- if not history:
92
- action = "detect_attack"
93
- elif history[-1] == "detect_attack":
94
- action = "deploy_honeypot"
95
- else:
96
- action = "block_ip"
97
 
98
- history.append(action)
99
 
100
- state, reward, done, _ = env.step(action)
101
- rewards.append(reward)
 
 
 
 
102
 
103
- print(
104
- f"[STEP] step={step} action={action} reward={reward:.2f} "
105
- f"done={str(done).lower()} error=null",
106
- flush=True
107
- )
108
 
109
- score = min(sum(rewards), 1.0)
 
110
 
111
  print(
112
- f"[END] success=true steps={len(rewards)} "
113
- f"score={score:.2f} rewards={','.join(f'{r:.2f}' for r in rewards)}",
114
  flush=True
115
  )
116
 
117
- except Exception:
118
 
119
- traceback.print_exc()
 
 
 
 
120
 
121
- print(
122
- "[END] success=false steps=0 score=0.00 rewards=",
123
- flush=True
124
- )
 
 
 
 
 
 
1
  import threading
2
  import time
3
+ import os
4
  import random
5
  import traceback
6
  from openai import OpenAI
 
11
  from env.env import DeceptionEnv
12
  from env.attacker import simulate_attack
13
 
14
+ # Start server in background
15
+ threading.Thread(target=run_server, daemon=True).start()
16
+ time.sleep(3)
17
 
18
+ API_BASE_URL = os.getenv("API_BASE_URL", "https://router.huggingface.co/v1")
19
+ MODEL_NAME = os.getenv("MODEL_NAME", "Qwen/Qwen2.5-7B-Instruct")
20
+ HF_TOKEN = os.getenv("HF_TOKEN")
 
21
 
22
+ try:
23
 
24
+ client = OpenAI(
25
+ base_url=API_BASE_URL,
26
+ api_key=HF_TOKEN
27
+ )
28
 
29
+ env = DeceptionEnv()
30
+ state = env.reset()
 
31
 
32
+ print(
33
+ "[START] task=ai-deception env=cyber-security model=AI-agent",
34
+ flush=True
35
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ rewards = []
38
+ history = []
39
 
40
+ for step in range(1, 4):
41
 
42
+ try:
43
+ simulate_attack()
44
+ except:
45
+ pass
46
 
47
+ try:
48
+ state = env.state()
49
+ except:
50
+ pass
51
 
52
+ summary = {
53
+ "failed_logins": state.get("failed_logins", 0),
54
+ "port_scans": state.get("port_scans", 0),
55
+ "suspicious_ips": len(state.get("suspicious_ips", []))
56
+ }
57
 
58
+ prompt = f"""
59
+ Previous actions: {history}
60
+ Current summary: {summary}
 
 
61
 
62
  Return one:
63
  detect_attack
 
65
  block_ip
66
  """
67
 
68
+ try:
69
+ response = client.chat.completions.create(
70
+ model=MODEL_NAME,
71
+ messages=[{"role": "user", "content": prompt}],
72
+ temperature=0.2,
73
+ max_tokens=20,
74
+ timeout=15
75
+ )
 
 
 
 
76
 
77
+ action = response.choices[0].message.content.strip()
 
 
 
 
 
78
 
79
+ except:
80
 
81
+ if not history:
82
+ action = "detect_attack"
83
+ elif history[-1] == "detect_attack":
84
+ action = "deploy_honeypot"
85
+ else:
86
+ action = "block_ip"
87
 
88
+ history.append(action)
 
 
 
 
89
 
90
+ state, reward, done, _ = env.step(action)
91
+ rewards.append(reward)
92
 
93
  print(
94
+ f"[STEP] step={step} action={action} reward={reward:.2f} "
95
+ f"done={str(done).lower()} error=null",
96
  flush=True
97
  )
98
 
99
+ score = min(sum(rewards), 1.0)
100
 
101
+ print(
102
+ f"[END] success=true steps={len(rewards)} "
103
+ f"score={score:.2f} rewards={','.join(f'{r:.2f}' for r in rewards)}",
104
+ flush=True
105
+ )
106
 
107
+ except Exception:
108
+
109
+ traceback.print_exc()
110
+
111
+ print(
112
+ "[END] success=false steps=0 score=0.00 rewards=",
113
+ flush=True
114
+ )