TanujInsane commited on
Commit ·
1b64cdd
1
Parent(s): 847096d
Update inference.py retry loop for server connection
Browse files- __init__.py +32 -0
- agent.py +44 -39
- app.py +225 -90
- client.py +103 -0
- error_log.txt +0 -0
- error_log_utf8.txt +32 -0
- inference.py +93 -16
- models.py +245 -0
- push_err.txt +0 -0
- push_err_utf8.txt +63 -0
- push_error.txt +69 -0
- push_error_develop.txt +69 -0
- server/__init__.py +1 -0
- server/document_classification_environment.py +128 -0
- startup.py +64 -0
- train_rl.py +78 -0
- verify_v1.py +20 -0
__init__.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Document Classification Environment — OpenEnv-compliant package.
|
| 3 |
+
|
| 4 |
+
Install:
|
| 5 |
+
pip install git+https://huggingface.co/spaces/TanujInsane/document-classification-env
|
| 6 |
+
|
| 7 |
+
Usage (sync):
|
| 8 |
+
from document_classification_env import DocEnv, DocAction
|
| 9 |
+
|
| 10 |
+
with DocEnv(base_url="https://tanujinsane-document-classification-env.hf.space").sync() as env:
|
| 11 |
+
result = env.reset()
|
| 12 |
+
result = env.step(DocAction(action_id=0, difficulty="easy"))
|
| 13 |
+
print(result.observation.content[:120])
|
| 14 |
+
print("reward:", result.reward, "done:", result.done)
|
| 15 |
+
|
| 16 |
+
Usage (async):
|
| 17 |
+
import asyncio
|
| 18 |
+
from document_classification_env import DocEnv, DocAction
|
| 19 |
+
|
| 20 |
+
async def main():
|
| 21 |
+
async with DocEnv(base_url="https://tanujinsane-document-classification-env.hf.space") as env:
|
| 22 |
+
result = await env.reset()
|
| 23 |
+
result = await env.step(DocAction(action_id=0, difficulty="easy"))
|
| 24 |
+
print(result.observation.content[:120])
|
| 25 |
+
|
| 26 |
+
asyncio.run(main())
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
from .client import DocEnv
|
| 30 |
+
from .models import DocAction, DocObservation, DocState
|
| 31 |
+
|
| 32 |
+
__all__ = ["DocEnv", "DocAction", "DocObservation", "DocState"]
|
agent.py
CHANGED
|
@@ -1,13 +1,48 @@
|
|
| 1 |
import pickle
|
| 2 |
import os
|
| 3 |
-
import time
|
| 4 |
from datetime import datetime
|
| 5 |
-
from constants import (
|
| 6 |
-
CATEGORIES, CATEGORY_KWORDS, PRIORITY_RULES,
|
| 7 |
-
DEPARTMENT_MAP, get_reply_template
|
| 8 |
-
)
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
REPLY_TEMPLATES = {
|
| 13 |
"Billing": """Dear Customer,
|
|
@@ -161,6 +196,7 @@ Best regards,
|
|
| 161 |
Operations Team""",
|
| 162 |
}
|
| 163 |
|
|
|
|
| 164 |
class TicketAgent:
|
| 165 |
def __init__(self, model_dir="."):
|
| 166 |
self.models = {}
|
|
@@ -184,10 +220,7 @@ class TicketAgent:
|
|
| 184 |
def classify(self, text, difficulty="easy"):
|
| 185 |
text = text.lower()
|
| 186 |
|
| 187 |
-
|
| 188 |
-
text = text.lower()
|
| 189 |
-
|
| 190 |
-
for cat, kw_list in CATEGORY_KWORDS.items():
|
| 191 |
if any(kw in text for kw in kw_list):
|
| 192 |
if difficulty in self.models:
|
| 193 |
model = self.models[difficulty]
|
|
@@ -221,37 +254,9 @@ class TicketAgent:
|
|
| 221 |
|
| 222 |
def generate_reply(self, category, priority, ref_id):
|
| 223 |
dept = self.route(category)
|
| 224 |
-
tmpl =
|
| 225 |
return tmpl.format(ref_id=ref_id, priority=priority.upper(), sla=dept["sla"])
|
| 226 |
|
| 227 |
-
def act(self, obs, difficulty="easy"):
|
| 228 |
-
"""Agentic interface for Gymnasium environments."""
|
| 229 |
-
text = obs.get("content", "")
|
| 230 |
-
cat, scores = self.classify(text, difficulty)
|
| 231 |
-
|
| 232 |
-
# Map category name back to index
|
| 233 |
-
cat_map = CATEGORIES[difficulty]
|
| 234 |
-
inv_map = {v: k for k, v in cat_map.items()}
|
| 235 |
-
|
| 236 |
-
if cat in inv_map:
|
| 237 |
-
return inv_map[cat]
|
| 238 |
-
# Fallback to General (0)
|
| 239 |
-
return 0
|
| 240 |
-
|
| 241 |
-
def run_episode(self, env):
|
| 242 |
-
"""Run a full episode in the provided environment."""
|
| 243 |
-
obs, _ = env.reset()
|
| 244 |
-
done = False
|
| 245 |
-
total_reward = 0
|
| 246 |
-
difficulty = env.task_difficulty
|
| 247 |
-
|
| 248 |
-
while not done:
|
| 249 |
-
action = self.act(obs, difficulty)
|
| 250 |
-
obs, reward, done, _, info = env.step(action)
|
| 251 |
-
total_reward += reward
|
| 252 |
-
|
| 253 |
-
return env.state()
|
| 254 |
-
|
| 255 |
def process_ticket(self, text, difficulty="easy", ticket_id=None):
|
| 256 |
ref_id = ticket_id or f"TKT-{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
| 257 |
cat, scores = self.classify(text, difficulty)
|
|
|
|
| 1 |
import pickle
|
| 2 |
import os
|
|
|
|
| 3 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
CATEGORIES = {
|
| 6 |
+
"easy": {0:"General", 1:"Billing", 2:"Support", 3:"Technical", 4:"HR"},
|
| 7 |
+
"medium": {0:"General", 1:"Billing", 2:"Support", 3:"Technical", 4:"HR",
|
| 8 |
+
5:"Legal", 6:"Sales", 7:"Marketing", 8:"Operations", 9:"Complaints"},
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
CATEGORY_OVERRIDE = {
|
| 12 |
+
"Technical": ["crash", "crashes", "bug", "error", "not working", "broken", "upload fail",
|
| 13 |
+
"application", "software", "login fail", "404", "500", "exception",
|
| 14 |
+
"loading", "slow", "freeze", "hang", "install", "update failed", "glitch"],
|
| 15 |
+
"Billing": ["invoice", "charge", "charged", "payment", "refund", "subscription", "billing",
|
| 16 |
+
"amount", "price", "cost", "fee", "receipt", "transaction", "overcharged"],
|
| 17 |
+
"HR": ["leave", "salary", "policy", "maternity", "paternity", "holiday", "resignation",
|
| 18 |
+
"offer letter", "payslip", "appraisal", "human resources", "benefits"],
|
| 19 |
+
"Complaints": ["complaint", "unacceptable", "disgusted", "terrible", "worst", "rude", "awful",
|
| 20 |
+
"disappointed", "frustrated", "escalate"],
|
| 21 |
+
"Legal": ["legal", "lawsuit", "compliance", "gdpr", "contract", "terms", "court",
|
| 22 |
+
"attorney", "lawyer", "liability", "copyright", "trademark"],
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
PRIORITY_RULES = {
|
| 26 |
+
"urgent": ["urgent", "asap", "immediately", "critical", "emergency", "not working",
|
| 27 |
+
"broken", "down", "error", "failed", "crashes", "crash"],
|
| 28 |
+
"high": ["incorrect", "wrong", "missing", "cannot", "can't", "unable",
|
| 29 |
+
"problem", "issue", "complaint"],
|
| 30 |
+
"medium": ["review", "check", "update", "change", "request", "need", "want", "please"],
|
| 31 |
+
"low": ["inquiry", "question", "info", "information", "general", "curious", "wondering"]
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
DEPARTMENT_MAP = {
|
| 35 |
+
"Billing": {"team": "Billing & Finance", "email": "billing@company.com", "sla": "24h"},
|
| 36 |
+
"Technical": {"team": "Tech Support", "email": "techsupport@company.com","sla": "4h"},
|
| 37 |
+
"HR": {"team": "Human Resources", "email": "hr@company.com", "sla": "48h"},
|
| 38 |
+
"Support": {"team": "Customer Support", "email": "support@company.com", "sla": "12h"},
|
| 39 |
+
"General": {"team": "General Enquiries", "email": "hello@company.com", "sla": "48h"},
|
| 40 |
+
"Legal": {"team": "Legal & Compliance", "email": "legal@company.com", "sla": "72h"},
|
| 41 |
+
"Sales": {"team": "Sales", "email": "sales@company.com", "sla": "6h"},
|
| 42 |
+
"Marketing": {"team": "Marketing", "email": "marketing@company.com", "sla": "48h"},
|
| 43 |
+
"Operations": {"team": "Operations", "email": "ops@company.com", "sla": "24h"},
|
| 44 |
+
"Complaints": {"team": "Customer Relations", "email": "complaints@company.com", "sla": "8h"},
|
| 45 |
+
}
|
| 46 |
|
| 47 |
REPLY_TEMPLATES = {
|
| 48 |
"Billing": """Dear Customer,
|
|
|
|
| 196 |
Operations Team""",
|
| 197 |
}
|
| 198 |
|
| 199 |
+
|
| 200 |
class TicketAgent:
|
| 201 |
def __init__(self, model_dir="."):
|
| 202 |
self.models = {}
|
|
|
|
| 220 |
def classify(self, text, difficulty="easy"):
|
| 221 |
text = text.lower()
|
| 222 |
|
| 223 |
+
for cat, kw_list in CATEGORY_OVERRIDE.items():
|
|
|
|
|
|
|
|
|
|
| 224 |
if any(kw in text for kw in kw_list):
|
| 225 |
if difficulty in self.models:
|
| 226 |
model = self.models[difficulty]
|
|
|
|
| 254 |
|
| 255 |
def generate_reply(self, category, priority, ref_id):
|
| 256 |
dept = self.route(category)
|
| 257 |
+
tmpl = REPLY_TEMPLATES.get(category, REPLY_TEMPLATES["General"])
|
| 258 |
return tmpl.format(ref_id=ref_id, priority=priority.upper(), sla=dept["sla"])
|
| 259 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
def process_ticket(self, text, difficulty="easy", ticket_id=None):
|
| 261 |
ref_id = ticket_id or f"TKT-{datetime.now().strftime('%Y%m%d%H%M%S')}"
|
| 262 |
cat, scores = self.classify(text, difficulty)
|
app.py
CHANGED
|
@@ -1,97 +1,232 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
from
|
| 4 |
-
from fastapi.responses import JSONResponse
|
| 5 |
from environment import DocumentClassificationEnv
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def ensure_models():
|
| 25 |
-
"""Train and persist ML models before starting servers."""
|
| 26 |
-
from baseline_inference import load_or_train
|
| 27 |
-
print("[startup] Pre-training classification models...")
|
| 28 |
-
for difficulty in ["easy", "medium", "hard"]:
|
| 29 |
-
load_or_train(difficulty)
|
| 30 |
-
print(f"[startup] {difficulty} model ready!")
|
| 31 |
-
print("[startup] All models ready!")
|
| 32 |
-
|
| 33 |
-
# Step 1: pre-train models (blocking, needed before server starts)
|
| 34 |
-
ensure_models()
|
| 35 |
-
|
| 36 |
-
app = FastAPI()
|
| 37 |
api_envs = {}
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
data = data or {}
|
| 49 |
-
difficulty = data.get("difficulty", "easy") if data else "easy"
|
| 50 |
-
seed = data.get("seed", 42) if data else 42
|
| 51 |
-
|
| 52 |
-
env = DocumentClassificationEnv(task_difficulty=difficulty, seed=seed)
|
| 53 |
-
obs, _ = env.reset(seed=seed)
|
| 54 |
-
api_envs["current"] = env
|
| 55 |
-
|
| 56 |
-
return JSONResponse(content=sanitize({"observation": dict(obs)}))
|
| 57 |
-
|
| 58 |
-
except Exception as e:
|
| 59 |
-
return JSONResponse({"error": str(e), "traceback": traceback.format_exc()}, status_code=500)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
@app.post("/step")
|
| 63 |
-
async def step_route(request: Request):
|
| 64 |
-
try:
|
| 65 |
-
try:
|
| 66 |
-
data = await request.json()
|
| 67 |
-
except Exception:
|
| 68 |
-
data = {}
|
| 69 |
-
data = data or {}
|
| 70 |
-
action_val = data.get("action", 0)
|
| 71 |
-
|
| 72 |
-
if "current" not in api_envs:
|
| 73 |
-
return JSONResponse(status_code=400, content={"error": "Environment not initialized"})
|
| 74 |
-
|
| 75 |
-
env = api_envs["current"]
|
| 76 |
-
obs, reward, done, _, info = env.step(action_val)
|
| 77 |
-
|
| 78 |
-
return JSONResponse(content=sanitize({"observation": dict(obs), "reward": reward, "done": done, "info": info}))
|
| 79 |
-
|
| 80 |
-
except Exception as e:
|
| 81 |
-
return JSONResponse({"error": str(e), "traceback": traceback.format_exc()}, status_code=500)
|
| 82 |
-
|
| 83 |
-
@app.get("/state")
|
| 84 |
-
async def state_route():
|
| 85 |
-
if "current" not in api_envs:
|
| 86 |
-
return JSONResponse(status_code=400, content={"error": "Environment not initialized"})
|
| 87 |
-
env = api_envs["current"]
|
| 88 |
-
state = env.state()
|
| 89 |
-
return JSONResponse(content=sanitize(state))
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import threading
|
| 3 |
import os
|
| 4 |
+
import pickle
|
| 5 |
+
from flask import Flask, request, jsonify
|
|
|
|
| 6 |
from environment import DocumentClassificationEnv
|
| 7 |
+
from baseline_inference import run_task, load_or_train
|
| 8 |
+
from agent import TicketAgent
|
| 9 |
|
| 10 |
+
print("Pre-training models...")
|
| 11 |
+
models = {}
|
| 12 |
+
for d in ["easy", "medium", "hard"]:
|
| 13 |
+
models[d] = load_or_train(d)
|
| 14 |
+
print(f" {d} model ready!")
|
| 15 |
+
print("All models ready!")
|
| 16 |
+
|
| 17 |
+
agent = TicketAgent(model_dir=".")
|
| 18 |
+
print("Agent ready!")
|
| 19 |
+
|
| 20 |
+
CATEGORIES = {
|
| 21 |
+
"easy": ["General","Billing","Support","Technical","HR"],
|
| 22 |
+
"medium": ["General","Billing","Support","Technical","HR","Legal","Sales","Marketing","Operations","Complaints"],
|
| 23 |
+
"hard": [f"Cat_{i}" for i in range(22)]
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
api = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
api_envs = {}
|
| 28 |
|
| 29 |
+
@api.route("/api/reset", methods=["POST"])
|
| 30 |
+
def api_reset():
|
| 31 |
+
data = request.json or {}
|
| 32 |
+
difficulty = data.get("difficulty", "easy")
|
| 33 |
+
seed = data.get("seed", 42)
|
| 34 |
+
env = DocumentClassificationEnv(task_difficulty=difficulty, seed=seed)
|
| 35 |
+
obs, _ = env.reset(seed=seed)
|
| 36 |
+
api_envs["current"] = env
|
| 37 |
+
return jsonify({"observation": {k: v.tolist() if hasattr(v, 'tolist') else v for k, v in obs.items()}})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
@api.route("/api/step", methods=["POST"])
|
| 40 |
+
def api_step():
|
| 41 |
+
data = request.json or {}
|
| 42 |
+
action = data.get("action", 0)
|
| 43 |
+
env = api_envs.get("current")
|
| 44 |
+
if env is None:
|
| 45 |
+
return jsonify({"error": "Call /api/reset first"}), 400
|
| 46 |
+
obs, reward, done, _, info = env.step(int(action))
|
| 47 |
+
return jsonify({
|
| 48 |
+
"observation": {k: v.tolist() if hasattr(v, 'tolist') else v for k, v in obs.items()},
|
| 49 |
+
"reward": reward, "done": done, "info": info
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
@api.route("/api/run", methods=["POST"])
|
| 53 |
+
def api_run():
|
| 54 |
+
data = request.json or {}
|
| 55 |
+
difficulty = data.get("difficulty", "easy")
|
| 56 |
+
score, t = run_task(difficulty)
|
| 57 |
+
return jsonify({"difficulty": difficulty, "score": score, "time": t})
|
| 58 |
+
|
| 59 |
+
@api.route("/api/agent/process", methods=["POST"])
|
| 60 |
+
def api_agent_process():
|
| 61 |
+
data = request.json or {}
|
| 62 |
+
text = data.get("text", "")
|
| 63 |
+
difficulty = data.get("difficulty", "easy")
|
| 64 |
+
ticket_id = data.get("ticket_id", None)
|
| 65 |
+
if not text:
|
| 66 |
+
return jsonify({"error": "text field required"}), 400
|
| 67 |
+
result = agent.process_ticket(text, difficulty=difficulty, ticket_id=ticket_id)
|
| 68 |
+
return jsonify(result)
|
| 69 |
+
|
| 70 |
+
@api.route("/api/agent/classify", methods=["POST"])
|
| 71 |
+
def api_agent_classify():
|
| 72 |
+
data = request.json or {}
|
| 73 |
+
text = data.get("text", "")
|
| 74 |
+
difficulty = data.get("difficulty", "easy")
|
| 75 |
+
category, scores = agent.classify(text, difficulty)
|
| 76 |
+
priority = agent.get_priority(text)
|
| 77 |
+
return jsonify({"category": category, "priority": priority, "scores": scores})
|
| 78 |
+
|
| 79 |
+
def run_flask():
|
| 80 |
+
api.run(host="0.0.0.0", port=7861, debug=False)
|
| 81 |
+
|
| 82 |
+
env_state = {"env": None, "difficulty": "easy"}
|
| 83 |
+
|
| 84 |
+
def create_env(difficulty):
|
| 85 |
+
e = DocumentClassificationEnv(task_difficulty=difficulty, seed=42)
|
| 86 |
+
obs, _ = e.reset()
|
| 87 |
+
env_state["env"] = e
|
| 88 |
+
env_state["difficulty"] = difficulty
|
| 89 |
+
model = models[difficulty]
|
| 90 |
+
cats = CATEGORIES[difficulty]
|
| 91 |
+
proba = model.predict_proba([obs["content"]])[0]
|
| 92 |
+
top3 = sorted(enumerate(proba), key=lambda x: -x[1])[:3]
|
| 93 |
+
explain = "\n".join([f" {cats[i] if i < len(cats) else f'Cat_{i}'}: {p*100:.1f}%" for i,p in top3])
|
| 94 |
+
pred_idx = int(model.predict([obs["content"]])[0])
|
| 95 |
+
pred_cat = cats[pred_idx] if pred_idx < len(cats) else f"Cat_{pred_idx}"
|
| 96 |
+
return (f"✅ Environment created! Difficulty: {difficulty}",
|
| 97 |
+
obs["content"],
|
| 98 |
+
f"🤖 Model predicts: **{pred_cat}**\n\nTop 3 predictions:\n{explain}",
|
| 99 |
+
"")
|
| 100 |
+
|
| 101 |
+
def classify_doc(category_idx):
|
| 102 |
+
e = env_state["env"]
|
| 103 |
+
if e is None:
|
| 104 |
+
return "Create environment first!", "", "", ""
|
| 105 |
+
obs, reward, done, _, info = e.step(int(category_idx))
|
| 106 |
+
result = f"Reward: {reward:.2f} | Correct: {info.get('is_correct', False)}"
|
| 107 |
+
if done:
|
| 108 |
+
acc = info.get('episode_accuracy', 0)
|
| 109 |
+
result += f"\n🏁 Episode done! Final Accuracy: {acc:.2%}"
|
| 110 |
+
return result, "Episode complete — create new environment", "", str(info)
|
| 111 |
+
difficulty = env_state["difficulty"]
|
| 112 |
+
model = models[difficulty]
|
| 113 |
+
cats = CATEGORIES[difficulty]
|
| 114 |
+
proba = model.predict_proba([obs["content"]])[0]
|
| 115 |
+
top3 = sorted(enumerate(proba), key=lambda x: -x[1])[:3]
|
| 116 |
+
explain = "\n".join([f" {cats[i] if i < len(cats) else f'Cat_{i}'}: {p*100:.1f}%" for i,p in top3])
|
| 117 |
+
pred_idx = int(model.predict([obs["content"]])[0])
|
| 118 |
+
pred_cat = cats[pred_idx] if pred_idx < len(cats) else f"Cat_{pred_idx}"
|
| 119 |
+
return (result, obs["content"],
|
| 120 |
+
f"🤖 Model predicts: **{pred_cat}**\n\nTop 3 predictions:\n{explain}",
|
| 121 |
+
str(info))
|
| 122 |
+
|
| 123 |
+
def run_baseline_all():
|
| 124 |
+
rows = []
|
| 125 |
+
for d in ["easy", "medium", "hard"]:
|
| 126 |
+
score, t = run_task(d)
|
| 127 |
+
rows.append([d.upper(), f"{score:.4f}", f"{score*100:.1f}%", f"{t:.1f}s"])
|
| 128 |
+
return rows
|
| 129 |
+
|
| 130 |
+
def process_ticket_ui(ticket_text, difficulty):
|
| 131 |
+
if not ticket_text.strip():
|
| 132 |
+
return "❌ Please enter ticket text!", "", "", "", "", ""
|
| 133 |
+
result = agent.process_ticket(ticket_text, difficulty=difficulty)
|
| 134 |
+
category_out = f"🏷️ {result['category']} (confidence: {result['confidence']*100:.1f}%)"
|
| 135 |
+
priority_out = f"🚨 {result['priority'].upper()}"
|
| 136 |
+
dept_out = f"🏢 {result['department']}\n📧 {result['email']}\n⏱️ SLA: {result['sla']}"
|
| 137 |
+
top3_out = "\n".join([f" {c}: {p*100:.1f}%" for c,p in result['top3']])
|
| 138 |
+
ref_out = f"🎫 {result['ref_id']} | ⏰ {result['timestamp'][:19]}"
|
| 139 |
+
reply_out = result['reply']
|
| 140 |
+
return category_out, priority_out, dept_out, top3_out, ref_out, reply_out
|
| 141 |
|
| 142 |
+
SAMPLE_TICKETS = {
|
| 143 |
+
"Billing complaint": "My invoice shows an incorrect amount. I was charged $150 but should have been charged $75. Please review and correct this immediately.",
|
| 144 |
+
"Bug report": "The application crashes whenever I try to upload a file larger than 10MB. This is a critical issue affecting my workflow.",
|
| 145 |
+
"HR query": "I have a question about the company's maternity leave policy. How many weeks of paid leave am I entitled to?",
|
| 146 |
+
"Chat message": "Hey, I can't log into my account. The password reset link isn't working either. Please help ASAP!",
|
| 147 |
+
"Email ticket": "Dear Support, I would like to inquire about upgrading my current subscription plan to include additional users.",
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
def load_sample(sample_name):
|
| 151 |
+
return SAMPLE_TICKETS.get(sample_name, "")
|
| 152 |
+
|
| 153 |
+
with gr.Blocks(title="Document Classification OpenEnv") as demo:
|
| 154 |
+
gr.Markdown("# 📄 Document Classification OpenEnv")
|
| 155 |
+
gr.Markdown("Real-world customer support ticket routing environment for RL agent training.")
|
| 156 |
+
|
| 157 |
+
with gr.Tab("🤖 Agent Demo"):
|
| 158 |
+
gr.Markdown("### Full Automation — ML Classification + Rule-based Priority + Auto-Reply")
|
| 159 |
+
with gr.Row():
|
| 160 |
+
with gr.Column(scale=2):
|
| 161 |
+
sample_dd = gr.Dropdown(
|
| 162 |
+
choices=list(SAMPLE_TICKETS.keys()),
|
| 163 |
+
label="📋 Load Sample Ticket",
|
| 164 |
+
value=None
|
| 165 |
+
)
|
| 166 |
+
ticket_input = gr.Textbox(
|
| 167 |
+
label="✍️ Ticket / Message Text",
|
| 168 |
+
lines=6,
|
| 169 |
+
placeholder="Paste email, chat message, bug report, HR query..."
|
| 170 |
+
)
|
| 171 |
+
diff_agent = gr.Radio(["easy","medium","hard"], value="medium", label="Model Difficulty")
|
| 172 |
+
btn_process = gr.Button("🚀 Process Ticket", variant="primary")
|
| 173 |
+
with gr.Column(scale=2):
|
| 174 |
+
ref_out_box = gr.Textbox(label="🎫 Reference ID & Timestamp")
|
| 175 |
+
cat_out_box = gr.Textbox(label="🏷️ Category & Confidence")
|
| 176 |
+
pri_out_box = gr.Textbox(label="🚨 Priority")
|
| 177 |
+
dept_out_box = gr.Textbox(label="🏢 Routed To", lines=3)
|
| 178 |
+
top3_out_box = gr.Textbox(label="📊 Top 3 Predictions", lines=3)
|
| 179 |
+
reply_out_box = gr.Textbox(label="📧 Auto-Generated Reply", lines=12)
|
| 180 |
+
|
| 181 |
+
sample_dd.change(load_sample, inputs=sample_dd, outputs=ticket_input)
|
| 182 |
+
btn_process.click(
|
| 183 |
+
process_ticket_ui,
|
| 184 |
+
inputs=[ticket_input, diff_agent],
|
| 185 |
+
outputs=[cat_out_box, pri_out_box, dept_out_box, top3_out_box, ref_out_box, reply_out_box]
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
with gr.Tab("🎮 Interactive Demo"):
|
| 189 |
+
diff = gr.Radio(["easy","medium","hard"], value="easy", label="Difficulty")
|
| 190 |
+
btn_create = gr.Button("Create Environment", variant="primary")
|
| 191 |
+
status = gr.Textbox(label="Status")
|
| 192 |
+
with gr.Row():
|
| 193 |
+
doc_content = gr.Textbox(label="📝 Document Content", lines=6)
|
| 194 |
+
explain_box = gr.Textbox(label="🧠 Explainability — Model Reasoning", lines=6)
|
| 195 |
+
category = gr.Number(label="Category Index (easy: 0-4, medium: 0-9, hard: 0-21)", value=0)
|
| 196 |
+
btn_classify = gr.Button("Classify Document")
|
| 197 |
+
result = gr.Textbox(label="Result")
|
| 198 |
+
info_box = gr.Textbox(label="Info")
|
| 199 |
+
btn_create.click(create_env, inputs=diff, outputs=[status, doc_content, explain_box, result])
|
| 200 |
+
btn_classify.click(classify_doc, inputs=category, outputs=[result, doc_content, explain_box, info_box])
|
| 201 |
+
|
| 202 |
+
with gr.Tab("📊 Baseline Evaluation"):
|
| 203 |
+
gr.Markdown("TF-IDF + Logistic Regression baseline — models pre-trained at startup for instant results.")
|
| 204 |
+
btn_eval = gr.Button("Run All Tasks", variant="primary")
|
| 205 |
+
score_table = gr.Dataframe(headers=["Task","Score","Accuracy","Time"], label="Results")
|
| 206 |
+
btn_eval.click(run_baseline_all, outputs=score_table)
|
| 207 |
+
|
| 208 |
+
with gr.Tab("📋 Environment Info"):
|
| 209 |
+
gr.Markdown("""
|
| 210 |
+
## Environment Design
|
| 211 |
+
**Task**: Classify customer support documents into correct departments.
|
| 212 |
+
|
| 213 |
+
| Difficulty | Categories | Episodes | Reward |
|
| 214 |
+
|------------|-----------|----------|--------|
|
| 215 |
+
| Easy | 5 | 20 | +1.0 correct, -0.4 wrong |
|
| 216 |
+
| Medium | 10 | 30 | +1.0 correct, -0.4 wrong |
|
| 217 |
+
| Hard | 22 | 50 | +1.0 correct, -0.4 wrong |
|
| 218 |
+
|
| 219 |
+
## Agent Pipeline
|
| 220 |
+
`Ticket Input` → `ML Classify` → `Rule Priority` → `Department Route` → `Auto Reply`
|
| 221 |
+
|
| 222 |
+
## API Endpoints (port 7861)
|
| 223 |
+
- `POST /api/reset` — `{"difficulty": "easy", "seed": 42}`
|
| 224 |
+
- `POST /api/step` — `{"action": 0}`
|
| 225 |
+
- `POST /api/agent/process` — `{"text": "my invoice is wrong", "difficulty": "medium"}`
|
| 226 |
+
- `POST /api/agent/classify` — `{"text": "urgent bug", "difficulty": "easy"}`
|
| 227 |
+
""")
|
| 228 |
+
|
| 229 |
+
if __name__ == "__main__":
|
| 230 |
+
t = threading.Thread(target=run_flask, daemon=True)
|
| 231 |
+
t.start()
|
| 232 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
client.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Document Classification Environment Client.
|
| 3 |
+
|
| 4 |
+
Connects to the running OpenEnv server (FastAPI + WebSocket) and provides
|
| 5 |
+
a typed interface for interacting with the document classification environment.
|
| 6 |
+
|
| 7 |
+
Example (sync):
|
| 8 |
+
from document_classification_env import DocEnv, DocAction
|
| 9 |
+
|
| 10 |
+
with DocEnv(base_url="https://tanujinsane-document-classification-env.hf.space").sync() as env:
|
| 11 |
+
result = env.reset()
|
| 12 |
+
print("Documents to classify:", result.observation.total_documents)
|
| 13 |
+
|
| 14 |
+
for i in range(5):
|
| 15 |
+
action = DocAction(action_id=i % 5, difficulty="easy")
|
| 16 |
+
result = env.step(action)
|
| 17 |
+
print(f"reward={result.reward:.2f} done={result.done}")
|
| 18 |
+
if result.done:
|
| 19 |
+
break
|
| 20 |
+
|
| 21 |
+
Example (async):
|
| 22 |
+
import asyncio
|
| 23 |
+
from document_classification_env import DocEnv, DocAction
|
| 24 |
+
|
| 25 |
+
async def main():
|
| 26 |
+
async with DocEnv(base_url="http://localhost:7860") as env:
|
| 27 |
+
result = await env.reset()
|
| 28 |
+
result = await env.step(DocAction(action_id=0, difficulty="easy"))
|
| 29 |
+
print(result.reward)
|
| 30 |
+
|
| 31 |
+
asyncio.run(main())
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
from __future__ import annotations
|
| 35 |
+
|
| 36 |
+
from typing import Any, Dict
|
| 37 |
+
|
| 38 |
+
from openenv.core.env_client import EnvClient
|
| 39 |
+
from openenv.core.client_types import StepResult
|
| 40 |
+
|
| 41 |
+
from .models import DocAction, DocObservation, DocState
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class DocEnv(EnvClient[DocAction, DocObservation, DocState]):
|
| 45 |
+
"""
|
| 46 |
+
Client for the Document Classification Environment.
|
| 47 |
+
|
| 48 |
+
Maintains a persistent WebSocket connection to the environment server,
|
| 49 |
+
enabling efficient multi-step interactions with lower latency.
|
| 50 |
+
|
| 51 |
+
Supports three difficulty levels:
|
| 52 |
+
- easy : 5 categories, 100 documents, no SLA
|
| 53 |
+
- medium : 10 categories, 500 documents, SLA 120s
|
| 54 |
+
- hard : 22 categories, 1000 documents, SLA 60s
|
| 55 |
+
|
| 56 |
+
Actions:
|
| 57 |
+
action_id 0..N-1 → classify document into category N
|
| 58 |
+
action_id N → request metadata (tool, costs -0.05 reward)
|
| 59 |
+
action_id N+1 → escalate to human (safe fallback, 0.0 reward)
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
def _step_payload(self, action: DocAction) -> Dict[str, Any]:
|
| 63 |
+
"""Convert DocAction to JSON payload for the step request."""
|
| 64 |
+
return {
|
| 65 |
+
"action_id": action.action_id,
|
| 66 |
+
"difficulty": action.difficulty,
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
def _parse_result(self, payload: Dict[str, Any]) -> StepResult[DocObservation]:
|
| 70 |
+
"""Parse server WebSocket response into a typed StepResult."""
|
| 71 |
+
obs_data = payload.get("observation", payload)
|
| 72 |
+
|
| 73 |
+
observation = DocObservation(
|
| 74 |
+
document_id=obs_data.get("document_id", ""),
|
| 75 |
+
content=obs_data.get("content", ""),
|
| 76 |
+
word_count=obs_data.get("word_count", 0),
|
| 77 |
+
has_urgency_markers=obs_data.get("has_urgency_markers", False),
|
| 78 |
+
features=obs_data.get("features", []),
|
| 79 |
+
document_index=obs_data.get("document_index", 0),
|
| 80 |
+
total_documents=obs_data.get("total_documents", 0),
|
| 81 |
+
metadata_response=obs_data.get("metadata_response", "Not requested."),
|
| 82 |
+
sla_remaining=obs_data.get("sla_remaining", 999.0),
|
| 83 |
+
reward=obs_data.get("reward", payload.get("reward", 0.0)),
|
| 84 |
+
done=obs_data.get("done", payload.get("done", False)),
|
| 85 |
+
metadata=obs_data.get("metadata", payload.get("info", {})),
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
return StepResult(
|
| 89 |
+
observation=observation,
|
| 90 |
+
reward=observation.reward,
|
| 91 |
+
done=observation.done,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
def _parse_state(self, payload: Dict[str, Any]) -> DocState:
|
| 95 |
+
"""Parse server /state response into a typed DocState."""
|
| 96 |
+
return DocState(
|
| 97 |
+
episode_id=payload.get("episode_id", ""),
|
| 98 |
+
step_count=payload.get("step_count", 0),
|
| 99 |
+
difficulty=payload.get("difficulty", "easy"),
|
| 100 |
+
accuracy=payload.get("accuracy", 0.0),
|
| 101 |
+
total_reward=payload.get("total_reward", 0.0),
|
| 102 |
+
sla_breaches=payload.get("sla_breaches", 0),
|
| 103 |
+
)
|
error_log.txt
ADDED
|
Binary file (1.34 kB). View file
|
|
|
error_log_utf8.txt
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python : Traceback (most
|
| 2 |
+
recent call last):
|
| 3 |
+
At line:1 char:1
|
| 4 |
+
+ python app.py 2>
|
| 5 |
+
error_log.txt
|
| 6 |
+
+ ~~~~~~~~~~~~~~~~~~~~~~~~
|
| 7 |
+
~~~~~~
|
| 8 |
+
+ CategoryInfo
|
| 9 |
+
: NotSpecified: (T
|
| 10 |
+
raceback (most recent
|
| 11 |
+
call last)::String)
|
| 12 |
+
[], RemoteException
|
| 13 |
+
+ FullyQualifiedError
|
| 14 |
+
Id : NativeCommandErr
|
| 15 |
+
or
|
| 16 |
+
|
| 17 |
+
File "C:\Users\91748\Des
|
| 18 |
+
ktop\metax\app.py", line
|
| 19 |
+
8, in <module>
|
| 20 |
+
from agent import
|
| 21 |
+
TicketAgent
|
| 22 |
+
File "C:\Users\91748\Des
|
| 23 |
+
ktop\metax\agent.py",
|
| 24 |
+
line 5, in <module>
|
| 25 |
+
from constants import
|
| 26 |
+
(
|
| 27 |
+
ImportError: cannot
|
| 28 |
+
import name
|
| 29 |
+
'CATEGORY_KWORDS' from
|
| 30 |
+
'constants' (C:\Users\9174
|
| 31 |
+
8\Desktop\metax\constants.
|
| 32 |
+
py)
|
inference.py
CHANGED
|
@@ -1,42 +1,94 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import requests
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
# Default points to the
|
| 6 |
API_BASE_URL = os.environ.get(
|
| 7 |
"API_BASE_URL",
|
| 8 |
-
"
|
| 9 |
)
|
| 10 |
|
| 11 |
def run_inference(difficulty="easy", seed=42):
|
| 12 |
print(f"Connecting to OpenEnv environment at {API_BASE_URL}...")
|
| 13 |
|
|
|
|
|
|
|
| 14 |
# 1. Reset the environment
|
| 15 |
reset_payload = {"difficulty": difficulty, "seed": seed}
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
done = False
|
| 22 |
total_reward = 0.0
|
| 23 |
-
start_time = time.time()
|
| 24 |
|
| 25 |
# 2. Step through the environment
|
| 26 |
while not done:
|
| 27 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
action = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
step_payload = {"action": action}
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
obs = result.get("observation")
|
| 37 |
reward = result.get("reward", 0.0)
|
| 38 |
done = result.get("done", False)
|
| 39 |
-
info = result.get("info"
|
| 40 |
|
| 41 |
total_reward += reward
|
| 42 |
|
|
@@ -48,6 +100,31 @@ def run_inference(difficulty="easy", seed=42):
|
|
| 48 |
"time": elapsed
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if __name__ == "__main__":
|
| 52 |
-
|
| 53 |
-
print("Inference completed:", result)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import sys
|
| 3 |
import requests
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
# Default points to the local HTTP API or HuggingFace Space URL
|
| 7 |
API_BASE_URL = os.environ.get(
|
| 8 |
"API_BASE_URL",
|
| 9 |
+
"http://127.0.0.1:7860" # Local mode is default for container evaluation
|
| 10 |
)
|
| 11 |
|
| 12 |
def run_inference(difficulty="easy", seed=42):
|
| 13 |
print(f"Connecting to OpenEnv environment at {API_BASE_URL}...")
|
| 14 |
|
| 15 |
+
start_time = time.time()
|
| 16 |
+
|
| 17 |
# 1. Reset the environment
|
| 18 |
reset_payload = {"difficulty": difficulty, "seed": seed}
|
| 19 |
+
try:
|
| 20 |
+
reset_response = requests.post(f"{API_BASE_URL}/reset", json=reset_payload, timeout=60)
|
| 21 |
+
reset_response.raise_for_status()
|
| 22 |
+
obs = reset_response.json().get("observation") or {}
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Error during reset: {e}")
|
| 25 |
+
return {
|
| 26 |
+
"difficulty": difficulty,
|
| 27 |
+
"score": 0.0,
|
| 28 |
+
"time": time.time() - start_time,
|
| 29 |
+
"error": str(e)
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Load model for intelligent classification
|
| 33 |
+
try:
|
| 34 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 35 |
+
from baseline_inference import load_or_train
|
| 36 |
+
model = load_or_train(difficulty)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Failed to load model: {e}")
|
| 39 |
+
model = None
|
| 40 |
|
| 41 |
done = False
|
| 42 |
total_reward = 0.0
|
|
|
|
| 43 |
|
| 44 |
# 2. Step through the environment
|
| 45 |
while not done:
|
| 46 |
+
# Fetch current state as requested before each step
|
| 47 |
+
try:
|
| 48 |
+
state_response = requests.get(f"{API_BASE_URL}/state", timeout=60)
|
| 49 |
+
state_response.raise_for_status()
|
| 50 |
+
state_data = state_response.json() or {}
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Error fetching state: {e}")
|
| 53 |
+
state_data = {}
|
| 54 |
+
|
| 55 |
+
# Use the actual document text; fallback to obs if not strictly inside /state
|
| 56 |
+
content = state_data.get("content")
|
| 57 |
+
|
| 58 |
+
if not content:
|
| 59 |
+
obs_data = state_data.get("observation") or {}
|
| 60 |
+
content = obs_data.get("content")
|
| 61 |
+
|
| 62 |
+
if not content:
|
| 63 |
+
content = obs.get("content", "")
|
| 64 |
+
|
| 65 |
+
# Intelligent classification decision
|
| 66 |
action = 0
|
| 67 |
+
if model is not None and content:
|
| 68 |
+
try:
|
| 69 |
+
action = int(model.predict([str(content)])[0])
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print(f"Error predicting action: {e}")
|
| 72 |
+
action = 0
|
| 73 |
|
| 74 |
step_payload = {"action": action}
|
| 75 |
+
try:
|
| 76 |
+
step_response = requests.post(f"{API_BASE_URL}/step", json=step_payload, timeout=60)
|
| 77 |
+
step_response.raise_for_status()
|
| 78 |
+
result = step_response.json() or {}
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"Error during step: {e}")
|
| 81 |
+
return {
|
| 82 |
+
"difficulty": difficulty,
|
| 83 |
+
"score": total_reward,
|
| 84 |
+
"time": time.time() - start_time,
|
| 85 |
+
"error": str(e)
|
| 86 |
+
}
|
| 87 |
|
| 88 |
+
obs = result.get("observation") or {}
|
| 89 |
reward = result.get("reward", 0.0)
|
| 90 |
done = result.get("done", False)
|
| 91 |
+
info = result.get("info") or {}
|
| 92 |
|
| 93 |
total_reward += reward
|
| 94 |
|
|
|
|
| 100 |
"time": elapsed
|
| 101 |
}
|
| 102 |
|
| 103 |
+
def wait_for_server(url, max_retries=30, delay=2):
|
| 104 |
+
print(f"Waiting for server at {url} to become reachable...")
|
| 105 |
+
for i in range(max_retries):
|
| 106 |
+
try:
|
| 107 |
+
response = requests.get(url, timeout=5)
|
| 108 |
+
print("Server is up and connected!")
|
| 109 |
+
return True
|
| 110 |
+
except Exception as e:
|
| 111 |
+
print(f"Server not ready, retrying in {delay} seconds... ({i+1}/{max_retries})")
|
| 112 |
+
time.sleep(delay)
|
| 113 |
+
|
| 114 |
+
print("Max retries reached. Server did not start in time.")
|
| 115 |
+
return False
|
| 116 |
+
|
| 117 |
+
def main():
|
| 118 |
+
try:
|
| 119 |
+
server_ready = wait_for_server(f"{API_BASE_URL}/reset", max_retries=30, delay=2)
|
| 120 |
+
if not server_ready:
|
| 121 |
+
print("Exiting because server failed to start.")
|
| 122 |
+
sys.exit(1)
|
| 123 |
+
|
| 124 |
+
result = run_inference()
|
| 125 |
+
print("Inference completed:", result)
|
| 126 |
+
except Exception as e:
|
| 127 |
+
print(f"Unhandled exception in main: {e}")
|
| 128 |
+
|
| 129 |
if __name__ == "__main__":
|
| 130 |
+
main()
|
|
|
models.py
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Typed Pydantic models for the Document Classification OpenEnv.
|
| 3 |
+
Implements the OpenEnv spec requirement for typed Observation, Action, and Reward models.
|
| 4 |
+
|
| 5 |
+
Contains two sets of models:
|
| 6 |
+
1. DocAction / DocObservation / DocState — extend openenv-core base types for
|
| 7 |
+
WebSocket-based EnvClient ↔ server communication (judges use these).
|
| 8 |
+
2. Action / Observation / Reward / EnvironmentState — legacy Pydantic models
|
| 9 |
+
kept for the REST API and Gradio UI.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from pydantic import BaseModel, Field, field_validator
|
| 13 |
+
from typing import List, Optional, Literal
|
| 14 |
+
from enum import Enum
|
| 15 |
+
|
| 16 |
+
# ── OpenEnv framework-native models (extend openenv-core base types) ─────────
|
| 17 |
+
try:
|
| 18 |
+
from openenv.core.env_server.types import (
|
| 19 |
+
Action as _OEAction,
|
| 20 |
+
Observation as _OEObservation,
|
| 21 |
+
)
|
| 22 |
+
_OPENENV_AVAILABLE = True
|
| 23 |
+
except ImportError: # graceful fallback so existing code still works without openenv
|
| 24 |
+
_OEAction = BaseModel
|
| 25 |
+
_OEObservation = BaseModel
|
| 26 |
+
_OPENENV_AVAILABLE = False
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class DocAction(_OEAction):
|
| 30 |
+
"""
|
| 31 |
+
Typed action for the Document Classification environment.
|
| 32 |
+
|
| 33 |
+
action_id meanings (for difficulty='easy', 5 categories):
|
| 34 |
+
0..4 → classify document into category 0..4
|
| 35 |
+
5 → request metadata (tool, costs -0.05 reward)
|
| 36 |
+
6 → escalate to human (safe fallback, 0.0 reward)
|
| 37 |
+
|
| 38 |
+
Use num_categories from the observation to determine valid range:
|
| 39 |
+
classify: 0 .. total_documents-related N
|
| 40 |
+
metadata: N
|
| 41 |
+
escalate: N+1
|
| 42 |
+
"""
|
| 43 |
+
action_id: int = Field(..., ge=0, description="Integer action index")
|
| 44 |
+
difficulty: str = Field("easy", description="Task difficulty: easy | medium | hard")
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class DocObservation(_OEObservation):
|
| 48 |
+
"""
|
| 49 |
+
Typed observation returned by reset() and step().
|
| 50 |
+
|
| 51 |
+
Inherits from openenv Observation which provides: reward, done, metadata.
|
| 52 |
+
"""
|
| 53 |
+
document_id: str = Field("", description="Unique ID for the current document")
|
| 54 |
+
content: str = Field("", description="Raw text of the support ticket")
|
| 55 |
+
word_count: int = Field(0, ge=0, description="Number of words")
|
| 56 |
+
has_urgency_markers: bool = Field(False, description="True if document is VIP / urgent")
|
| 57 |
+
features: List[float] = Field(default_factory=list, description="TF-IDF feature vector (dim=100)")
|
| 58 |
+
document_index: int = Field(0, ge=0, description="Current document index in episode")
|
| 59 |
+
total_documents: int = Field(0, ge=0, description="Total documents in this episode")
|
| 60 |
+
metadata_response: str = Field(
|
| 61 |
+
"Not requested. Use action to request customer history.",
|
| 62 |
+
description="Response from Request Metadata tool",
|
| 63 |
+
)
|
| 64 |
+
sla_remaining: float = Field(999.0, ge=0.0, description="Seconds before SLA breach")
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class DocState(BaseModel):
|
| 68 |
+
"""
|
| 69 |
+
Episode-level state returned by state().
|
| 70 |
+
|
| 71 |
+
Exposes ground-truth metrics for graders and evaluation harnesses.
|
| 72 |
+
NOT visible to the agent during training.
|
| 73 |
+
"""
|
| 74 |
+
episode_id: str = Field(..., description="Unique episode identifier")
|
| 75 |
+
step_count: int = Field(..., ge=0, description="Number of steps taken so far")
|
| 76 |
+
difficulty: str = Field(..., description="Task difficulty: easy | medium | hard")
|
| 77 |
+
accuracy: float = Field(..., ge=0.0, le=1.0, description="Classification accuracy so far")
|
| 78 |
+
total_reward: float = Field(..., description="Cumulative reward for this episode")
|
| 79 |
+
sla_breaches: int = Field(0, ge=0, description="Number of SLA breaches so far")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# ─── Action Models ───────────────────────────────────────────────────────────
|
| 83 |
+
|
| 84 |
+
class ActionType(str, Enum):
|
| 85 |
+
"""Types of actions an agent can take."""
|
| 86 |
+
CLASSIFY = "classify"
|
| 87 |
+
REQUEST_METADATA = "request_metadata"
|
| 88 |
+
ESCALATE = "escalate"
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class Action(BaseModel):
|
| 92 |
+
"""
|
| 93 |
+
Typed action model for the Document Classification environment.
|
| 94 |
+
|
| 95 |
+
Actions 0..N-1 classify the document into a category.
|
| 96 |
+
Action N requests customer metadata (tool usage, costs -0.05 reward).
|
| 97 |
+
Action N+1 escalates to a human manager (safe fallback, 0.0 reward).
|
| 98 |
+
"""
|
| 99 |
+
action_id: int = Field(..., ge=0, description="Integer action index")
|
| 100 |
+
action_type: ActionType = Field(..., description="Type of action taken")
|
| 101 |
+
category_name: Optional[str] = Field(None, description="Category name if classify action")
|
| 102 |
+
|
| 103 |
+
@classmethod
|
| 104 |
+
def from_raw(cls, action_id: int, num_categories: int, category_map: dict) -> "Action":
|
| 105 |
+
"""Create a typed Action from a raw integer action."""
|
| 106 |
+
if action_id < num_categories:
|
| 107 |
+
return cls(
|
| 108 |
+
action_id=action_id,
|
| 109 |
+
action_type=ActionType.CLASSIFY,
|
| 110 |
+
category_name=category_map.get(action_id, f"Unknown_{action_id}")
|
| 111 |
+
)
|
| 112 |
+
elif action_id == num_categories:
|
| 113 |
+
return cls(
|
| 114 |
+
action_id=action_id,
|
| 115 |
+
action_type=ActionType.REQUEST_METADATA,
|
| 116 |
+
category_name=None
|
| 117 |
+
)
|
| 118 |
+
elif action_id == num_categories + 1:
|
| 119 |
+
return cls(
|
| 120 |
+
action_id=action_id,
|
| 121 |
+
action_type=ActionType.ESCALATE,
|
| 122 |
+
category_name=None
|
| 123 |
+
)
|
| 124 |
+
else:
|
| 125 |
+
raise ValueError(f"Invalid action_id: {action_id}. Must be 0..{num_categories + 1}")
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
# ─── Observation Models ──────────────────────────────────────────────────────
|
| 129 |
+
|
| 130 |
+
class Observation(BaseModel):
|
| 131 |
+
"""
|
| 132 |
+
Typed observation model returned by reset() and step().
|
| 133 |
+
|
| 134 |
+
Contains the document content, metadata features, and progress tracking.
|
| 135 |
+
"""
|
| 136 |
+
document_id: str = Field(..., description="Unique identifier for the current document")
|
| 137 |
+
content: str = Field(..., description="Raw text content of the support ticket")
|
| 138 |
+
word_count: int = Field(..., ge=0, description="Number of words in the document")
|
| 139 |
+
has_urgency_markers: bool = Field(..., description="Whether document contains urgency indicators")
|
| 140 |
+
features: List[float] = Field(..., description="Pre-extracted TF-IDF feature vector (normalized)")
|
| 141 |
+
document_index: int = Field(..., ge=0, description="Current document index in the episode")
|
| 142 |
+
total_documents: int = Field(..., ge=0, description="Total documents in the episode")
|
| 143 |
+
metadata_response: str = Field(
|
| 144 |
+
default="Not requested. Use action to request customer history.",
|
| 145 |
+
description="Response from the metadata tool, if previously requested"
|
| 146 |
+
)
|
| 147 |
+
sla_remaining: float = Field(
|
| 148 |
+
default=999.0,
|
| 149 |
+
ge=0.0,
|
| 150 |
+
description="Seconds remaining before SLA breach for current document"
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
@classmethod
|
| 154 |
+
def from_gym_obs(cls, obs: dict) -> "Observation":
|
| 155 |
+
"""Create a typed Observation from a raw Gymnasium observation dict."""
|
| 156 |
+
return cls(
|
| 157 |
+
document_id=obs.get("document_id", ""),
|
| 158 |
+
content=obs.get("content", ""),
|
| 159 |
+
word_count=int(obs["word_count"][0]) if hasattr(obs.get("word_count"), '__len__') else int(obs.get("word_count", 0)),
|
| 160 |
+
has_urgency_markers=bool(obs["has_urgency_markers"][0]) if hasattr(obs.get("has_urgency_markers"), '__len__') else bool(obs.get("has_urgency_markers", False)),
|
| 161 |
+
features=[float(f) for f in obs.get("features", [])],
|
| 162 |
+
document_index=int(obs["document_index"][0]) if hasattr(obs.get("document_index"), '__len__') else int(obs.get("document_index", 0)),
|
| 163 |
+
total_documents=int(obs["total_documents"][0]) if hasattr(obs.get("total_documents"), '__len__') else int(obs.get("total_documents", 0)),
|
| 164 |
+
metadata_response=obs.get("metadata_response", "Not requested."),
|
| 165 |
+
sla_remaining=float(obs["sla_remaining"][0]) if hasattr(obs.get("sla_remaining"), '__len__') else float(obs.get("sla_remaining", 999.0)),
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
# ─── Reward Models ───────────────────────────────────────────────────────────
|
| 170 |
+
|
| 171 |
+
class Reward(BaseModel):
|
| 172 |
+
"""
|
| 173 |
+
Typed reward model returned by step().
|
| 174 |
+
|
| 175 |
+
Decomposes the scalar reward into interpretable components.
|
| 176 |
+
"""
|
| 177 |
+
total: float = Field(..., description="Total scalar reward for this step")
|
| 178 |
+
accuracy_component: float = Field(0.0, description="+1.0 for correct, -0.4 for wrong, 0.0 for escalate")
|
| 179 |
+
tool_cost: float = Field(0.0, description="-0.05 if metadata was requested, 0.0 otherwise")
|
| 180 |
+
is_correct: bool = Field(False, description="Whether the classification was correct")
|
| 181 |
+
|
| 182 |
+
@classmethod
|
| 183 |
+
def from_step(cls, reward: float, info: dict) -> "Reward":
|
| 184 |
+
"""Create a typed Reward from raw step() outputs."""
|
| 185 |
+
pred = info.get("predicted_category", "")
|
| 186 |
+
if pred == "TOOL_METADATA":
|
| 187 |
+
return cls(total=reward, accuracy_component=0.0, tool_cost=-0.05, is_correct=False)
|
| 188 |
+
elif pred == "ESCALATED":
|
| 189 |
+
return cls(total=reward, accuracy_component=0.0, tool_cost=0.0, is_correct=False)
|
| 190 |
+
else:
|
| 191 |
+
is_correct = info.get("is_correct", False)
|
| 192 |
+
acc = 1.0 if is_correct else -0.4
|
| 193 |
+
return cls(total=reward, accuracy_component=acc, tool_cost=0.0, is_correct=is_correct)
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
# ─── State Model ─────────────────────────────────────────────────────────────
|
| 197 |
+
|
| 198 |
+
class EnvironmentState(BaseModel):
|
| 199 |
+
"""
|
| 200 |
+
Typed state model returned by state().
|
| 201 |
+
|
| 202 |
+
Provides a complete snapshot of the environment's internal state.
|
| 203 |
+
"""
|
| 204 |
+
document_index: int = Field(..., ge=0)
|
| 205 |
+
total_documents: int = Field(..., ge=0)
|
| 206 |
+
current_true_label: Optional[int] = None
|
| 207 |
+
current_true_category: Optional[str] = None
|
| 208 |
+
total_reward: float
|
| 209 |
+
accuracy: float = Field(..., ge=0.0, le=1.0)
|
| 210 |
+
avg_time_ms: float = Field(..., ge=0.0)
|
| 211 |
+
difficulty: Literal["easy", "medium", "hard"]
|
| 212 |
+
sla_breaches: int = Field(default=0, ge=0, description="Number of SLA breaches")
|
| 213 |
+
action_history: List[int] = Field(default_factory=list)
|
| 214 |
+
reward_history: List[float] = Field(default_factory=list)
|
| 215 |
+
processing_times_ms: List[float] = Field(default_factory=list)
|
| 216 |
+
|
| 217 |
+
@classmethod
|
| 218 |
+
def from_state_dict(cls, state: dict) -> "EnvironmentState":
|
| 219 |
+
"""Create a typed EnvironmentState from raw state() output."""
|
| 220 |
+
return cls(
|
| 221 |
+
document_index=state["document_index"],
|
| 222 |
+
total_documents=state["total_documents"],
|
| 223 |
+
current_true_label=state.get("current_true_label"),
|
| 224 |
+
current_true_category=state.get("current_true_category"),
|
| 225 |
+
total_reward=state["total_reward"],
|
| 226 |
+
accuracy=state["accuracy"],
|
| 227 |
+
avg_time_ms=state["avg_time_ms"],
|
| 228 |
+
difficulty=state["difficulty"],
|
| 229 |
+
sla_breaches=state.get("sla_breaches", 0),
|
| 230 |
+
action_history=state.get("action_history", []),
|
| 231 |
+
reward_history=state.get("reward_history", []),
|
| 232 |
+
processing_times_ms=state.get("processing_times_ms", []),
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
# ─── Episode Summary ─────────────────────────────────────────────────────────
|
| 237 |
+
|
| 238 |
+
class EpisodeSummary(BaseModel):
|
| 239 |
+
"""Summary metrics for a completed episode."""
|
| 240 |
+
total_reward: float
|
| 241 |
+
avg_reward: float
|
| 242 |
+
accuracy: float = Field(..., ge=0.0, le=1.0)
|
| 243 |
+
total_classified: int = Field(..., ge=0)
|
| 244 |
+
avg_time_ms: float = Field(..., ge=0.0)
|
| 245 |
+
total_time_s: float = Field(..., ge=0.0)
|
push_err.txt
ADDED
|
Binary file (2.81 kB). View file
|
|
|
push_err_utf8.txt
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git : remote: [31m-------
|
| 2 |
+
--------------------------
|
| 3 |
+
--------------------------
|
| 4 |
+
--------------[0m
|
| 5 |
+
At line:1 char:1
|
| 6 |
+
+ git push origin
|
| 7 |
+
master:main 2>
|
| 8 |
+
push_err.txt; cat
|
| 9 |
+
push_err.txt
|
| 10 |
+
+ ~~~~~~~~~~~~~~~~~~~~~~~~
|
| 11 |
+
~~~~~~~~~~~~~~~~~~~
|
| 12 |
+
+ CategoryInfo
|
| 13 |
+
: NotSpecified: (r
|
| 14 |
+
emote: [31m--...---
|
| 15 |
+
[0m :String) [
|
| 16 |
+
], RemoteException
|
| 17 |
+
+ FullyQualifiedError
|
| 18 |
+
Id : NativeCommandErr
|
| 19 |
+
or
|
| 20 |
+
|
| 21 |
+
remote: [31mYour push
|
| 22 |
+
was rejected because it
|
| 23 |
+
contains binary
|
| 24 |
+
files.[0m
|
| 25 |
+
remote: [31mPlease use ht
|
| 26 |
+
tps://huggingface.co/docs/
|
| 27 |
+
hub/xet to store binary
|
| 28 |
+
files.[0m
|
| 29 |
+
remote: [31mSee also: htt
|
| 30 |
+
ps://huggingface.co/docs/h
|
| 31 |
+
ub/xet/using-xet-storage#g
|
| 32 |
+
it[0m
|
| 33 |
+
remote: [31m[0m
|
| 34 |
+
remote: [31mOffending
|
| 35 |
+
files:[0m
|
| 36 |
+
remote: [31m -
|
| 37 |
+
model_easy.pkl (ref:
|
| 38 |
+
refs/heads/main)[0m
|
| 39 |
+
|
| 40 |
+
remote: [31m -
|
| 41 |
+
model_hard.pkl (ref:
|
| 42 |
+
refs/heads/main)[0m
|
| 43 |
+
|
| 44 |
+
remote: [31m -
|
| 45 |
+
model_medium.pkl (ref:
|
| 46 |
+
refs/heads/main)[0m
|
| 47 |
+
|
| 48 |
+
remote: [31m-------------
|
| 49 |
+
--------------------------
|
| 50 |
+
--------------------------
|
| 51 |
+
--------[0m
|
| 52 |
+
To https://huggingface.co/
|
| 53 |
+
spaces/TanujInsane/documen
|
| 54 |
+
t-classification-env
|
| 55 |
+
! [remote rejected]
|
| 56 |
+
master -> main
|
| 57 |
+
(pre-receive hook
|
| 58 |
+
declined)
|
| 59 |
+
error: failed to push
|
| 60 |
+
some refs to 'https://hugg
|
| 61 |
+
ingface.co/spaces/TanujIns
|
| 62 |
+
ane/document-classificatio
|
| 63 |
+
n-env'
|
push_error.txt
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git : Pushing to https://h
|
| 2 |
+
uggingface.co/spaces/Tanuj
|
| 3 |
+
Insane/document-classifica
|
| 4 |
+
tion-env
|
| 5 |
+
At line:1 char:1
|
| 6 |
+
+ git push origin
|
| 7 |
+
master:main -f --verbose
|
| 8 |
+
2>&1 | Out-File -FilePath
|
| 9 |
+
pu ...
|
| 10 |
+
+ ~~~~~~~~~~~~~~~~~~~~~~~~
|
| 11 |
+
~~~~~~~~~~~~~~~~~~~~~
|
| 12 |
+
+ CategoryInfo
|
| 13 |
+
: NotSpecified: (P
|
| 14 |
+
ushing to http...ssif
|
| 15 |
+
ication-env:String) [
|
| 16 |
+
], RemoteException
|
| 17 |
+
+ FullyQualifiedError
|
| 18 |
+
Id : NativeCommandErr
|
| 19 |
+
or
|
| 20 |
+
|
| 21 |
+
POST git-receive-pack
|
| 22 |
+
(chunked)
|
| 23 |
+
remote: [31m-------------
|
| 24 |
+
--------------------------
|
| 25 |
+
--------------------------
|
| 26 |
+
--------[0m
|
| 27 |
+
remote: [31mYour push
|
| 28 |
+
was rejected because it
|
| 29 |
+
contains binary
|
| 30 |
+
files.[0m
|
| 31 |
+
remote: [31mPlease use ht
|
| 32 |
+
tps://huggingface.co/docs/
|
| 33 |
+
hub/xet to store binary
|
| 34 |
+
files.[0m
|
| 35 |
+
remote: [31mSee also: htt
|
| 36 |
+
ps://huggingface.co/docs/h
|
| 37 |
+
ub/xet/using-xet-storage#g
|
| 38 |
+
it[0m
|
| 39 |
+
remote: [31m[0m
|
| 40 |
+
remote: [31mOffending
|
| 41 |
+
files:[0m
|
| 42 |
+
remote: [31m -
|
| 43 |
+
model_easy.pkl (ref:
|
| 44 |
+
refs/heads/main)[0m
|
| 45 |
+
|
| 46 |
+
remote: [31m -
|
| 47 |
+
model_hard.pkl (ref:
|
| 48 |
+
refs/heads/main)[0m
|
| 49 |
+
|
| 50 |
+
remote: [31m -
|
| 51 |
+
model_medium.pkl (ref:
|
| 52 |
+
refs/heads/main)[0m
|
| 53 |
+
|
| 54 |
+
remote: [31m-------------
|
| 55 |
+
--------------------------
|
| 56 |
+
--------------------------
|
| 57 |
+
--------[0m
|
| 58 |
+
To https://huggingface.co/
|
| 59 |
+
spaces/TanujInsane/documen
|
| 60 |
+
t-classification-env
|
| 61 |
+
! [remote rejected]
|
| 62 |
+
master -> main
|
| 63 |
+
(pre-receive hook
|
| 64 |
+
declined)
|
| 65 |
+
error: failed to push
|
| 66 |
+
some refs to 'https://hugg
|
| 67 |
+
ingface.co/spaces/TanujIns
|
| 68 |
+
ane/document-classificatio
|
| 69 |
+
n-env'
|
push_error_develop.txt
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git : Pushing to https://h
|
| 2 |
+
uggingface.co/spaces/Tanuj
|
| 3 |
+
Insane/document-classifica
|
| 4 |
+
tion-env
|
| 5 |
+
At line:1 char:1
|
| 6 |
+
+ git push origin
|
| 7 |
+
master:develop -f
|
| 8 |
+
--verbose 2>&1 | Out-File
|
| 9 |
+
-FilePath ...
|
| 10 |
+
+ ~~~~~~~~~~~~~~~~~~~~~~~~
|
| 11 |
+
~~~~~~~~~~~~~~~~~~~~~~~~
|
| 12 |
+
+ CategoryInfo
|
| 13 |
+
: NotSpecified: (P
|
| 14 |
+
ushing to http...ssif
|
| 15 |
+
ication-env:String) [
|
| 16 |
+
], RemoteException
|
| 17 |
+
+ FullyQualifiedError
|
| 18 |
+
Id : NativeCommandErr
|
| 19 |
+
or
|
| 20 |
+
|
| 21 |
+
POST git-receive-pack
|
| 22 |
+
(chunked)
|
| 23 |
+
remote: [31m-------------
|
| 24 |
+
--------------------------
|
| 25 |
+
--------------------------
|
| 26 |
+
--------[0m
|
| 27 |
+
remote: [31mYour push
|
| 28 |
+
was rejected because it
|
| 29 |
+
contains binary
|
| 30 |
+
files.[0m
|
| 31 |
+
remote: [31mPlease use ht
|
| 32 |
+
tps://huggingface.co/docs/
|
| 33 |
+
hub/xet to store binary
|
| 34 |
+
files.[0m
|
| 35 |
+
remote: [31mSee also: htt
|
| 36 |
+
ps://huggingface.co/docs/h
|
| 37 |
+
ub/xet/using-xet-storage#g
|
| 38 |
+
it[0m
|
| 39 |
+
remote: [31m[0m
|
| 40 |
+
remote: [31mOffending
|
| 41 |
+
files:[0m
|
| 42 |
+
remote: [31m -
|
| 43 |
+
model_easy.pkl (ref:
|
| 44 |
+
refs/heads/develop)[0m
|
| 45 |
+
|
| 46 |
+
remote: [31m -
|
| 47 |
+
model_hard.pkl (ref:
|
| 48 |
+
refs/heads/develop)[0m
|
| 49 |
+
|
| 50 |
+
remote: [31m -
|
| 51 |
+
model_medium.pkl (ref:
|
| 52 |
+
refs/heads/develop)[0m
|
| 53 |
+
|
| 54 |
+
remote: [31m-------------
|
| 55 |
+
--------------------------
|
| 56 |
+
--------------------------
|
| 57 |
+
--------[0m
|
| 58 |
+
To https://huggingface.co/
|
| 59 |
+
spaces/TanujInsane/documen
|
| 60 |
+
t-classification-env
|
| 61 |
+
! [remote rejected]
|
| 62 |
+
master -> develop
|
| 63 |
+
(pre-receive hook
|
| 64 |
+
declined)
|
| 65 |
+
error: failed to push
|
| 66 |
+
some refs to 'https://hugg
|
| 67 |
+
ingface.co/spaces/TanujIns
|
| 68 |
+
ane/document-classificatio
|
| 69 |
+
n-env'
|
server/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# server package — OpenEnv FastAPI server for Document Classification Environment
|
server/document_classification_environment.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Document Classification Environment — OpenEnv server-side implementation.
|
| 3 |
+
|
| 4 |
+
Bridges the existing DocumentClassificationEnv (gym-style) to the OpenEnv
|
| 5 |
+
Environment base class. The gym env contains all real RL logic; this adapter
|
| 6 |
+
only converts types and wires up reset/step/state for the OpenEnv HTTP server.
|
| 7 |
+
|
| 8 |
+
Architecture:
|
| 9 |
+
OpenEnv HTTP/WebSocket server (server/app.py)
|
| 10 |
+
└── DocumentClassificationEnvironment ← this file
|
| 11 |
+
└── DocumentClassificationEnv ← environment.py (gym env, unchanged)
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
from __future__ import annotations
|
| 15 |
+
|
| 16 |
+
import sys
|
| 17 |
+
import os
|
| 18 |
+
import uuid
|
| 19 |
+
from typing import Any, Optional
|
| 20 |
+
|
| 21 |
+
# Allow importing from parent directory (environment.py, models.py, tasks.py)
|
| 22 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 23 |
+
|
| 24 |
+
from openenv.core.env_server import Environment
|
| 25 |
+
|
| 26 |
+
from environment import DocumentClassificationEnv
|
| 27 |
+
from models import DocAction, DocObservation, DocState
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class DocumentClassificationEnvironment(Environment):
|
| 31 |
+
"""
|
| 32 |
+
OpenEnv-compliant wrapper around DocumentClassificationEnv.
|
| 33 |
+
|
| 34 |
+
Satisfies the OpenEnv Environment interface:
|
| 35 |
+
- reset(seed, **kwargs) → DocObservation
|
| 36 |
+
- step(DocAction) → DocObservation (obs.done, obs.reward set)
|
| 37 |
+
- state (property) → DocState
|
| 38 |
+
|
| 39 |
+
One instance = one difficulty level. The create_app() factory in app.py
|
| 40 |
+
instantiates a fresh instance per WebSocket session (for isolation).
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
def __init__(self, difficulty: str = "easy", seed: int = 42):
|
| 44 |
+
self._difficulty = difficulty
|
| 45 |
+
self._seed = seed
|
| 46 |
+
self._gym_env = DocumentClassificationEnv(
|
| 47 |
+
task_difficulty=difficulty, seed=seed
|
| 48 |
+
)
|
| 49 |
+
self._episode_id: str = str(uuid.uuid4())
|
| 50 |
+
self._step_count: int = 0
|
| 51 |
+
|
| 52 |
+
# ── OpenEnv interface ─────────────────────────────────────────────────────
|
| 53 |
+
|
| 54 |
+
def reset(
|
| 55 |
+
self,
|
| 56 |
+
seed: Optional[int] = None,
|
| 57 |
+
episode_id: Optional[str] = None,
|
| 58 |
+
**kwargs: Any,
|
| 59 |
+
) -> DocObservation:
|
| 60 |
+
"""Start a new episode and return the first observation."""
|
| 61 |
+
obs, _ = self._gym_env.reset(seed=seed)
|
| 62 |
+
self._episode_id = episode_id or str(uuid.uuid4())
|
| 63 |
+
self._step_count = 0
|
| 64 |
+
return self._make_obs(obs, reward=0.0, done=False)
|
| 65 |
+
|
| 66 |
+
def step(self, action: DocAction, **kwargs: Any) -> DocObservation:
|
| 67 |
+
"""Execute one classification action and return the resulting observation."""
|
| 68 |
+
obs, reward, done, _, info = self._gym_env.step(int(action.action_id))
|
| 69 |
+
self._step_count += 1
|
| 70 |
+
return self._make_obs(obs, reward=float(reward), done=bool(done), metadata=info)
|
| 71 |
+
|
| 72 |
+
@property
|
| 73 |
+
def state(self) -> DocState:
|
| 74 |
+
"""Return episode-level state metadata (not exposed to agent during training)."""
|
| 75 |
+
s = self._gym_env.state()
|
| 76 |
+
return DocState(
|
| 77 |
+
episode_id=self._episode_id,
|
| 78 |
+
step_count=self._step_count,
|
| 79 |
+
difficulty=self._difficulty,
|
| 80 |
+
accuracy=float(s.get("accuracy", 0.0)),
|
| 81 |
+
total_reward=float(s.get("total_reward", 0.0)),
|
| 82 |
+
sla_breaches=int(s.get("sla_breaches", 0)),
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# ── Internal helpers ──────────────────────────────────────────────────────
|
| 86 |
+
|
| 87 |
+
def _make_obs(
|
| 88 |
+
self,
|
| 89 |
+
gym_obs: dict,
|
| 90 |
+
reward: float,
|
| 91 |
+
done: bool,
|
| 92 |
+
metadata: Optional[dict] = None,
|
| 93 |
+
) -> DocObservation:
|
| 94 |
+
"""Convert a raw gymnasium observation dict to a typed DocObservation."""
|
| 95 |
+
|
| 96 |
+
def _scalar(value, default=0):
|
| 97 |
+
"""Unwrap numpy scalars / 1-element arrays to Python primitives."""
|
| 98 |
+
if value is None:
|
| 99 |
+
return default
|
| 100 |
+
if hasattr(value, "__len__") and len(value) == 1:
|
| 101 |
+
return type(default)(value[0])
|
| 102 |
+
if hasattr(value, "item"):
|
| 103 |
+
return value.item()
|
| 104 |
+
return value
|
| 105 |
+
|
| 106 |
+
features_raw = gym_obs.get("features", [])
|
| 107 |
+
features = (
|
| 108 |
+
features_raw.tolist()
|
| 109 |
+
if hasattr(features_raw, "tolist")
|
| 110 |
+
else list(features_raw)
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
return DocObservation(
|
| 114 |
+
document_id=gym_obs.get("document_id", ""),
|
| 115 |
+
content=gym_obs.get("content", ""),
|
| 116 |
+
word_count=int(_scalar(gym_obs.get("word_count"), 0)),
|
| 117 |
+
has_urgency_markers=bool(_scalar(gym_obs.get("has_urgency_markers"), 0)),
|
| 118 |
+
features=[float(f) for f in features],
|
| 119 |
+
document_index=int(_scalar(gym_obs.get("document_index"), 0)),
|
| 120 |
+
total_documents=int(_scalar(gym_obs.get("total_documents"), 0)),
|
| 121 |
+
metadata_response=gym_obs.get(
|
| 122 |
+
"metadata_response", "Not requested. Use action to request customer history."
|
| 123 |
+
),
|
| 124 |
+
sla_remaining=float(_scalar(gym_obs.get("sla_remaining"), 999.0)),
|
| 125 |
+
reward=reward,
|
| 126 |
+
done=done,
|
| 127 |
+
metadata=metadata or {},
|
| 128 |
+
)
|
startup.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Startup entrypoint for HuggingFace Spaces (Option A port layout).
|
| 3 |
+
|
| 4 |
+
Launches two servers in parallel:
|
| 5 |
+
- FastAPI (OpenEnv API): port 7860 ← HF Spaces primary, grader connects here
|
| 6 |
+
- Gradio (Demo UI): port 7861 ← human-readable demo
|
| 7 |
+
|
| 8 |
+
This ensures the OpenEnv grader can reach the API at the Space root URL
|
| 9 |
+
while still serving the demo interface on port 7861.
|
| 10 |
+
|
| 11 |
+
Usage (Docker CMD):
|
| 12 |
+
python startup.py
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
import sys
|
| 17 |
+
import threading
|
| 18 |
+
import subprocess
|
| 19 |
+
|
| 20 |
+
# Pre-train models if .pkl files are missing
|
| 21 |
+
def ensure_models():
|
| 22 |
+
"""Train and persist ML models before starting servers."""
|
| 23 |
+
from baseline_inference import load_or_train
|
| 24 |
+
print("[startup] Pre-training classification models...")
|
| 25 |
+
for difficulty in ["easy", "medium", "hard"]:
|
| 26 |
+
model = load_or_train(difficulty)
|
| 27 |
+
print(f"[startup] {difficulty} model ready \u2713")
|
| 28 |
+
print("[startup] All models ready!")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def run_fastapi():
|
| 32 |
+
"""Launch the OpenEnv FastAPI server on port 7860."""
|
| 33 |
+
import uvicorn
|
| 34 |
+
# Import here so sys.path is already set correctly
|
| 35 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 36 |
+
from server.app import app
|
| 37 |
+
print("[startup] Starting FastAPI (OpenEnv API) on port 7860...")
|
| 38 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def run_gradio():
|
| 42 |
+
"""Launch the Gradio demo UI on port 7861."""
|
| 43 |
+
print("[startup] Starting Gradio UI on port 7861...")
|
| 44 |
+
# Patch the Gradio launch port before importing app.py
|
| 45 |
+
# app.py calls demo.launch(server_port=7860) \u2014 we override it via env var
|
| 46 |
+
env = os.environ.copy()
|
| 47 |
+
env["GRADIO_SERVER_PORT"] = "7861"
|
| 48 |
+
subprocess.Popen(
|
| 49 |
+
[sys.executable, "app.py"],
|
| 50 |
+
env=env,
|
| 51 |
+
stdout=sys.stdout,
|
| 52 |
+
stderr=sys.stderr,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
# Step 1: pre-train models (blocking, needed before either server starts)
|
| 58 |
+
ensure_models()
|
| 59 |
+
|
| 60 |
+
# Step 2: start Gradio in a subprocess (it has its own event loop)
|
| 61 |
+
run_gradio()
|
| 62 |
+
|
| 63 |
+
# Step 3: start FastAPI in the main thread (blocks until stopped)
|
| 64 |
+
run_fastapi()
|
train_rl.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
import torch.nn as nn
|
| 6 |
+
import torch.optim as optim
|
| 7 |
+
from environment import DocumentClassificationEnv
|
| 8 |
+
|
| 9 |
+
class SimplePolicy(nn.Module):
|
| 10 |
+
def __init__(self, input_dim, output_dim):
|
| 11 |
+
super(SimplePolicy, self).__init__()
|
| 12 |
+
self.fc = nn.Sequential(
|
| 13 |
+
nn.Linear(input_dim, 256),
|
| 14 |
+
nn.ReLU(),
|
| 15 |
+
nn.Linear(256, output_dim),
|
| 16 |
+
nn.Softmax(dim=-1)
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def forward(self, x):
|
| 20 |
+
return self.fc(x)
|
| 21 |
+
|
| 22 |
+
def train(difficulty="easy", num_episodes=50):
|
| 23 |
+
print(f"--- Starting RL Training on {difficulty} ---")
|
| 24 |
+
env = DocumentClassificationEnv(task_difficulty=difficulty)
|
| 25 |
+
input_dim = env.feature_dim
|
| 26 |
+
output_dim = env.num_categories # Simplify: ignore tools for now
|
| 27 |
+
|
| 28 |
+
policy = SimplePolicy(input_dim, output_dim)
|
| 29 |
+
optimizer = optim.Adam(policy.parameters(), lr=0.01)
|
| 30 |
+
|
| 31 |
+
history = []
|
| 32 |
+
|
| 33 |
+
for ep in range(num_episodes):
|
| 34 |
+
obs, _ = env.reset()
|
| 35 |
+
done = False
|
| 36 |
+
ep_reward = 0
|
| 37 |
+
|
| 38 |
+
while not done:
|
| 39 |
+
# Convert features to tensor
|
| 40 |
+
x = torch.FloatTensor(obs["features"]).unsqueeze(0)
|
| 41 |
+
|
| 42 |
+
# Action probability distribution
|
| 43 |
+
probs = policy(x)
|
| 44 |
+
m = torch.distributions.Categorical(probs)
|
| 45 |
+
action_tensor = m.sample()
|
| 46 |
+
action = action_tensor.item()
|
| 47 |
+
|
| 48 |
+
# Step in environment
|
| 49 |
+
new_obs, reward, done, _, info = env.step(action)
|
| 50 |
+
|
| 51 |
+
# Simple REINFORCE-like update (simplified for demo)
|
| 52 |
+
# Log prob of action * reward
|
| 53 |
+
loss = -m.log_prob(action_tensor) * reward
|
| 54 |
+
|
| 55 |
+
optimizer.zero_grad()
|
| 56 |
+
loss.backward()
|
| 57 |
+
optimizer.step()
|
| 58 |
+
|
| 59 |
+
obs = new_obs
|
| 60 |
+
ep_reward += reward
|
| 61 |
+
|
| 62 |
+
acc = env.correct / env.total if env.total > 0 else 0
|
| 63 |
+
history.append({"episode": ep+1, "reward": ep_reward, "accuracy": acc})
|
| 64 |
+
|
| 65 |
+
if (ep + 1) % 5 == 0:
|
| 66 |
+
print(f"Episode {ep+1}/{num_episodes} | Reward: {ep_reward:.2f} | Accuracy: {acc:.2%}")
|
| 67 |
+
|
| 68 |
+
print("--- Training Complete ---")
|
| 69 |
+
return history
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
import argparse
|
| 73 |
+
parser = argparse.ArgumentParser()
|
| 74 |
+
parser.add_argument("--difficulty", type=str, default="easy")
|
| 75 |
+
parser.add_argument("--episodes", type=int, default=20)
|
| 76 |
+
args = parser.parse_args()
|
| 77 |
+
|
| 78 |
+
train(difficulty=args.difficulty, num_episodes=args.episodes)
|
verify_v1.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from environment import DocumentClassificationEnv
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
env = DocumentClassificationEnv(task_difficulty='hard')
|
| 5 |
+
obs, _ = env.reset()
|
| 6 |
+
|
| 7 |
+
print("Testing Hard Step...")
|
| 8 |
+
# Step quickly
|
| 9 |
+
obs, reward, done, _, info = env.step(0)
|
| 10 |
+
print(f"Reward: {reward:.4f}")
|
| 11 |
+
print(f"Is Correct: {info.get('is_correct')}")
|
| 12 |
+
print(f"Speed Bonus: {info.get('speed_bonus'):.4f}")
|
| 13 |
+
print(f"Proc Cost: {info.get('proc_cost'):.4f}")
|
| 14 |
+
print(f"True Category: {info.get('true_category')}")
|
| 15 |
+
|
| 16 |
+
# Test state API
|
| 17 |
+
telemetry = env.state()
|
| 18 |
+
print(f"Internal State Action History Length: {len(telemetry['action_history'])}")
|
| 19 |
+
print(f"Internal State Reward History: {telemetry['reward_history']}")
|
| 20 |
+
print(f"Total reward from state: {telemetry['total_reward']:.4f}")
|