Spaces:
Sleeping
Sleeping
Upload inference.py
Browse files- inference.py +17 -9
inference.py
CHANGED
|
@@ -6,9 +6,11 @@ from env import MyEnvV4Env
|
|
| 6 |
from models import MyEnvV4Action
|
| 7 |
|
| 8 |
# Environment Configuration
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
TASK_NAME = "security-mail-triage"
|
| 13 |
|
| 14 |
SYSTEM_PROMPT = """
|
|
@@ -30,20 +32,21 @@ Respond in JSON format:
|
|
| 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,
|
|
@@ -53,13 +56,17 @@ async def main():
|
|
| 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
|
|
@@ -67,5 +74,6 @@ async def main():
|
|
| 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())
|
|
|
|
| 6 |
from models import MyEnvV4Action
|
| 7 |
|
| 8 |
# Environment Configuration
|
| 9 |
+
# FIX: Point directly to Google's OpenAI-compatible endpoint for Gemini models
|
| 10 |
+
API_BASE_URL = os.getenv("API_BASE_URL") or "https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 11 |
+
# FIX: Use Gemini API Key instead of Hugging Face token
|
| 12 |
+
API_KEY = os.getenv("GEMINI_API_KEY") or ""
|
| 13 |
+
MODEL_NAME = "gemini-2.0-flash"
|
| 14 |
TASK_NAME = "security-mail-triage"
|
| 15 |
|
| 16 |
SYSTEM_PROMPT = """
|
|
|
|
| 32 |
}
|
| 33 |
"""
|
| 34 |
|
| 35 |
+
|
| 36 |
async def main():
|
| 37 |
client = OpenAI(base_url=API_BASE_URL, api_key=API_KEY)
|
| 38 |
env = MyEnvV4Env()
|
| 39 |
+
|
| 40 |
rewards = []
|
| 41 |
print(f"[START] Testing Security Triage Environment...")
|
| 42 |
+
|
| 43 |
result = await env.reset()
|
| 44 |
step_idx = 1
|
| 45 |
+
|
| 46 |
while not result.done:
|
| 47 |
obs = result.observation
|
| 48 |
prompt = f"Sender: {obs.sender}\nSubject: {obs.subject}\nBody: {obs.body}\nHeaders: {obs.raw_headers}\nURLs: {obs.urls}"
|
| 49 |
+
|
| 50 |
try:
|
| 51 |
response = client.chat.completions.create(
|
| 52 |
model=MODEL_NAME,
|
|
|
|
| 56 |
)
|
| 57 |
import json
|
| 58 |
data = json.loads(response.choices[0].message.content)
|
| 59 |
+
|
| 60 |
action = MyEnvV4Action(message=data["message"], reasoning=data["reasoning"])
|
| 61 |
result = await env.step(action)
|
| 62 |
rewards.append(result.reward)
|
| 63 |
+
|
| 64 |
print(f"[STEP {step_idx}] Action: {action.message} | Reward: {result.reward:.2f}")
|
| 65 |
step_idx += 1
|
| 66 |
+
|
| 67 |
+
# Prevent hitting Gemini Free Tier rate limits (15 requests per minute & token limits)
|
| 68 |
+
# Increased to 10 seconds to ensure we do not hit the burst quotas.
|
| 69 |
+
await asyncio.sleep(10)
|
| 70 |
except Exception as e:
|
| 71 |
print(f"[ERROR] Step {step_idx}: {e}")
|
| 72 |
break
|
|
|
|
| 74 |
score = sum(rewards) / len(rewards) if rewards else 0
|
| 75 |
print(f"[END] Final Score: {score:.3f}")
|
| 76 |
|
| 77 |
+
|
| 78 |
if __name__ == "__main__":
|
| 79 |
asyncio.run(main())
|