Spaces:
Sleeping
Sleeping
Update inference.py
Browse files- inference.py +70 -72
inference.py
CHANGED
|
@@ -1,73 +1,71 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
-
import os
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
# Environment Configuration
|
| 9 |
-
API_BASE_URL = os.getenv("API_BASE_URL") or "https://router.huggingface.co/v1"
|
| 10 |
-
API_KEY = os.getenv("HF_TOKEN") or
|
| 11 |
-
MODEL_NAME = os.getenv("MODEL_NAME") or "
|
| 12 |
-
TASK_NAME = "
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def
|
| 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 |
-
if __name__ == "__main__":
|
| 73 |
asyncio.run(main())
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import os
|
| 3 |
+
from typing import List
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
from env import MyEnvV4Env
|
| 6 |
+
from models import MyEnvV4Action
|
| 7 |
+
|
| 8 |
+
# Environment Configuration
|
| 9 |
+
API_BASE_URL = os.getenv("API_BASE_URL") or "https://router.huggingface.co/v1"
|
| 10 |
+
API_KEY = os.getenv("HF_TOKEN") or ""
|
| 11 |
+
MODEL_NAME = os.getenv("MODEL_NAME") or "gemini-2.5-flash-preview-09-2025"
|
| 12 |
+
TASK_NAME = "security-mail-triage"
|
| 13 |
+
|
| 14 |
+
SYSTEM_PROMPT = """
|
| 15 |
+
You are an Advanced Email Security Agent. Analyze the metadata (headers, SPF/DKIM), URLs, and content.
|
| 16 |
+
Categories:
|
| 17 |
+
- INBOX: Trusted academic/official domains, passed auth, clean history.
|
| 18 |
+
- SPAM: Mass marketing, generic lottery/sales, usually safe but unwanted.
|
| 19 |
+
- QUARANTINE: Phishing, spear-phishing, credential theft, high-urgency threats, typo-squatted domains.
|
| 20 |
+
|
| 21 |
+
Rules:
|
| 22 |
+
1. Examine 'raw_headers' and 'auth_results'.
|
| 23 |
+
2. Inspect 'urls' for low reputation or high age.
|
| 24 |
+
3. Provide reasoning first, then your decision.
|
| 25 |
+
|
| 26 |
+
Respond in JSON format:
|
| 27 |
+
{
|
| 28 |
+
"reasoning": "Explain your logic here...",
|
| 29 |
+
"message": "INBOX|SPAM|QUARANTINE"
|
| 30 |
+
}
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
async def main():
|
| 34 |
+
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 35 |
+
env = MyEnvV4Env()
|
| 36 |
+
|
| 37 |
+
rewards = []
|
| 38 |
+
print(f"[START] Testing Security Triage Environment...")
|
| 39 |
+
|
| 40 |
+
result = await env.reset()
|
| 41 |
+
step_idx = 1
|
| 42 |
+
|
| 43 |
+
while not result.done:
|
| 44 |
+
obs = result.observation
|
| 45 |
+
prompt = f"Sender: {obs.sender}\nSubject: {obs.subject}\nBody: {obs.body}\nHeaders: {obs.raw_headers}\nURLs: {obs.urls}"
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
response = client.chat.completions.create(
|
| 49 |
+
model=MODEL_NAME,
|
| 50 |
+
messages=[{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": prompt}],
|
| 51 |
+
response_format={"type": "json_object"},
|
| 52 |
+
temperature=0.0
|
| 53 |
+
)
|
| 54 |
+
import json
|
| 55 |
+
data = json.loads(response.choices[0].message.content)
|
| 56 |
+
|
| 57 |
+
action = MyEnvV4Action(message=data["message"], reasoning=data["reasoning"])
|
| 58 |
+
result = await env.step(action)
|
| 59 |
+
rewards.append(result.reward)
|
| 60 |
+
|
| 61 |
+
print(f"[STEP {step_idx}] Action: {action.message} | Reward: {result.reward:.2f}")
|
| 62 |
+
step_idx += 1
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"[ERROR] Step {step_idx}: {e}")
|
| 65 |
+
break
|
| 66 |
+
|
| 67 |
+
score = sum(rewards) / len(rewards) if rewards else 0
|
| 68 |
+
print(f"[END] Final Score: {score:.3f}")
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
| 71 |
asyncio.run(main())
|