File size: 2,289 Bytes
7181aa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
73
try:
    from openenv.core.env_server.http_server import create_app
except Exception as e:
    raise ImportError(
        "openenv is required. Install with: pip install openenv-core"
    ) from e

from fastapi import Request
from fastapi.responses import JSONResponse
from uuid import uuid4
import json

from models import ComplianceAction, ComplianceObservation
from compliance_env_environment import ComplianceEnvironment, _GLOBAL_STATE, generate_scenario

app = create_app(
    ComplianceEnvironment,
    ComplianceAction,
    ComplianceObservation,
    env_name="compliance_env",
    max_concurrent_envs=1,
)

@app.get("/health")
async def health():
    return {"status": "healthy"}

@app.post("/reset", include_in_schema=False)
async def reset_with_difficulty(request: Request):
    try:
        body = await request.json()
        difficulty = body.get("difficulty", None)
    except Exception:
        difficulty = None

    scenario = generate_scenario(difficulty)
    _GLOBAL_STATE["episode_id"] = str(uuid4())
    _GLOBAL_STATE["step_count"] = 0
    _GLOBAL_STATE["scenario"] = scenario
    _GLOBAL_STATE["extracted_clause_ids"] = []
    _GLOBAL_STATE["gap_items"] = []
    _GLOBAL_STATE["checklist"] = []
    _GLOBAL_STATE["max_steps"] = scenario["max_steps"]

    obs = ComplianceObservation(
        step=0,
        max_steps=scenario["max_steps"],
        message=f"[{scenario['difficulty'].upper()} TASK] New episode started. Read the regulation and company profile carefully. Start by extracting applicable clause IDs. {scenario['company_for_episode']['difficulty_hint']}",
        regulation_document=json.dumps(scenario["regulation"], indent=2),
        company_profile=scenario["company_for_episode"],
        episode_id=_GLOBAL_STATE["episode_id"],
        difficulty=scenario["difficulty"],
        extracted_clauses_so_far=[],
        gaps_found_so_far=[],
        partial_score=0.0,
        steps_remaining=scenario["max_steps"],
        done=False,
        reward=0.0,
    )
    return JSONResponse(content={
        "observation": obs.model_dump(),
        "reward": 0.0,
        "done": False
    })


def main(host: str = "0.0.0.0", port: int = 8000) -> None:
    import uvicorn
    uvicorn.run(app, host=host, port=port)


if __name__ == "__main__":
    main()