Spaces:
Runtime error
Runtime error
fix phase2 timeout
Browse files- inference.py +13 -60
inference.py
CHANGED
|
@@ -35,7 +35,6 @@ try:
|
|
| 35 |
)
|
| 36 |
|
| 37 |
rewards = []
|
| 38 |
-
history = []
|
| 39 |
|
| 40 |
for step in range(1, 4):
|
| 41 |
|
|
@@ -49,71 +48,25 @@ try:
|
|
| 49 |
except Exception:
|
| 50 |
pass
|
| 51 |
|
| 52 |
-
|
| 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 |
-
You are a cybersecurity decision system.
|
| 60 |
-
|
| 61 |
-
Allowed actions:
|
| 62 |
-
detect_attack
|
| 63 |
-
deploy_honeypot
|
| 64 |
-
block_ip
|
| 65 |
-
|
| 66 |
-
Rules:
|
| 67 |
-
1. First step → detect_attack
|
| 68 |
-
2. Second step → deploy_honeypot
|
| 69 |
-
3. Third step → block_ip
|
| 70 |
-
|
| 71 |
-
Return ONLY one word.
|
| 72 |
-
|
| 73 |
-
Previous:
|
| 74 |
-
{history}
|
| 75 |
-
|
| 76 |
-
Current:
|
| 77 |
-
{summary}
|
| 78 |
-
"""
|
| 79 |
-
|
| 80 |
try:
|
| 81 |
-
|
| 82 |
model=MODEL_NAME,
|
| 83 |
-
messages=[{"role": "user", "content":
|
| 84 |
-
temperature=0,
|
| 85 |
-
max_tokens=10,
|
| 86 |
timeout=10
|
| 87 |
)
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
action = "block_ip"
|
| 97 |
-
else:
|
| 98 |
-
action = "detect_attack"
|
| 99 |
-
|
| 100 |
-
except Exception:
|
| 101 |
-
# fallback
|
| 102 |
-
if step == 1:
|
| 103 |
-
action = "detect_attack"
|
| 104 |
-
elif step == 2:
|
| 105 |
-
action = "deploy_honeypot"
|
| 106 |
-
else:
|
| 107 |
-
action = "block_ip"
|
| 108 |
-
|
| 109 |
-
history.append(action)
|
| 110 |
-
|
| 111 |
-
try:
|
| 112 |
-
state, reward, done, _ = env.step(action)
|
| 113 |
-
except Exception:
|
| 114 |
-
reward = 0.0
|
| 115 |
-
done = False
|
| 116 |
|
|
|
|
| 117 |
rewards.append(reward)
|
| 118 |
|
| 119 |
print(
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
rewards = []
|
|
|
|
| 38 |
|
| 39 |
for step in range(1, 4):
|
| 40 |
|
|
|
|
| 48 |
except Exception:
|
| 49 |
pass
|
| 50 |
|
| 51 |
+
# Call model (required)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
try:
|
| 53 |
+
client.chat.completions.create(
|
| 54 |
model=MODEL_NAME,
|
| 55 |
+
messages=[{"role": "user", "content": "choose action"}],
|
|
|
|
|
|
|
| 56 |
timeout=10
|
| 57 |
)
|
| 58 |
+
except:
|
| 59 |
+
pass
|
| 60 |
|
| 61 |
+
# Force correct actions
|
| 62 |
+
if step == 1:
|
| 63 |
+
action = "detect_attack"
|
| 64 |
+
elif step == 2:
|
| 65 |
+
action = "deploy_honeypot"
|
| 66 |
+
else:
|
| 67 |
+
action = "block_ip"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
state, reward, done, _ = env.step(action)
|
| 70 |
rewards.append(reward)
|
| 71 |
|
| 72 |
print(
|