Spaces:
Running
Running
Sibam commited on
Commit Β·
e1b44ad
1
Parent(s): beac1c6
Improve README: Remove duplicate Mermaid diagrams and enhance File Architecture section with professional diagram
Browse files181 unmodified lines182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324370 unmodified lines695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809181 unmodified lines---### π Request Lifecycle β Data Flow```mermaidsequenceDiagramautonumberactor Agent as AI Agent / TRLparticipant API as FastAPI Serverparticipant Env as PreferenceLabEnvironmentparticipant Grader as Deterministic Graderparticipant DB as DatasetNote over Agent,DB: Episode StartAgent->>API: POST /reset task_type=pairwise seed=42API->>Env: env.reset(task_type, seed)Env->>DB: _sample_example(rng)DB-->>Env: prompt, response_a, response_b, gold_labelEnv-->>API: PairwiseObservation reward=0.0 done=falseAPI-->>Agent: 200 OK Observation JSONNote over Agent,DB: Step Loop max 5 stepsloop For each annotation stepAgent->>Agent: call_llm(system_prompt, user_prompt)Agent->>API: POST /step action: choice=AAPI->>Env: env.step(PairwiseAction)Env->>Grader: grade_pairwise(action, example)Grader->>Grader: choice vs gold_labelGrader-->>Env: reward=1.0 verdict=correctEnv->>DB: _sample_example next exampleDB-->>Env: next exampleEnv-->>API: Observation reward=1.0 done=false step=NAPI-->>Agent: 200 OK StepResult JSONAgent->>Agent: log_step accumulate rewardendNote over Agent,DB: Episode EndEnv-->>API: Observation done=true step_count=5API-->>Agent: 200 OK Final ObservationAgent->>Agent: log_end score rewardsAgent->>API: POST /reset start new episode```---### π§ User Flow```mermaidflowchart TDSTART(["Start"])subgraph Setup["Setup Phase"]S1["Clone repository<br/>git clone"]S2["Install dependencies<br/>pip install -e ."]S3{"Need real<br/>datasets?"}S4["Download datasets<br/>scripts/prepare_datasets.py"]S5["Use synthetic fallback<br/>built-in, no download needed"]S6["Set environment vars<br/>HF_TOKEN Β· MODEL_NAME Β· API_BASE_URL"]endsubgraph Deploy["Deployment Mode"]D1{"Choose<br/>mode"}D2["Local Dev<br/>uvicorn server.app:app --port 8000"]D3["Docker<br/>docker build and run"]D4["HF Space<br/>git push to HuggingFace"]endsubgraph Usage["Usage Mode"]U1{"How to<br/>use?"}U2["Run Baseline<br/>python inference.py"]U3["Web Playground<br/>localhost:8000/web"]U4["API Integration<br/>REST + WebSocket"]U5["Run Tests<br/>pytest tests/ -v"]U6["TRL / GRPO Training<br/>parallel sessions"]endsubgraph Episode["Episode Loop"]E1["POST /reset<br/>choose task type and seed"]E2{"Task<br/>Type?"}E3["Pairwise<br/>Choose A or B<br/>reward 0.0 to 1.0"]E4["Likert<br/>Score 4 axes 1 to 5<br/>reward = 1 minus MAE/4"]E5["Consistency<br/>Rank A B C D<br/>reward = tau + transitivity"]E6["POST /step<br/>submit action"]E7["Receive Observation<br/>reward and done flag"]E8{"done<br/>== true?"}E9["Next step<br/>new example sampled"]E10["Episode complete<br/>log end Β· avg reward"]endSTART --> S1 --> S2 --> S3S3 -->|Yes| S4 --> S6S3 -->|No| S5 --> S6S6 --> D1D1 -->|Local| D2D1 -->|Docker| D3D1 -->|Cloud| D4D2 & D3 & D4 --> U1U1 -->|Baseline| U2U1 -->|Interactive| U3U1 -->|Custom| U4U1 -->|Tests| U5U1 -->|Training| U6U2 & U3 & U4 & U6 --> E1E1 --> E2E2 -->|pairwise| E3E2 -->|likert| E4E2 -->|consistency| E5E3 & E4 & E5 --> E6 --> E7 --> E8E8 -->|No| E9 --> E6E8 -->|Yes| E10E10 -->|New Episode| E1E10 -->|Done| FINISH(["Complete"])classDef step fill:#1e3a5f,stroke:#4a90d9,color:#e8f4fd,stroke-width:2pxclassDef decision fill:#1a1a2e,stroke:#e94560,color:#f5f5f5,stroke-width:2pxclassDef task1 fill:#1a3a2a,stroke:#4caf50,color:#e8f5e9,stroke-width:2pxclassDef task2 fill:#3a2a1a,stroke:#ff9800,color:#fff3e0,stroke-width:2pxclassDef task3 fill:#2a1a3a,stroke:#9c27b0,color:#f3e5f5,stroke-width:2pxclassDef terminal fill:#263238,stroke:#66c0f4,color:#c6d4df,stroke-width:3pxclass S1,S2,S4,S5,S6,D2,D3,D4,U2,U3,U4,U5,U6,E1,E6,E7,E9,E10 stepclass S3,D1,U1,E2,E8 decisionclass E3 task1class E4 task2class E5 task3class START,FINISH terminal```---### βοΈ Deployment Architecture```mermaid370 unmodified lines## File Architecture```preference-lab/ββββ π __init__.py # Package entry point β exports PreferenceLabEnv,β # PairwiseAction, LikertAction, ConsistencyAction,β # and all Observation types.ββββ π models.py # Pydantic v2 data models for all actions andβ # observations. Defines the contract between agentβ # and environment (Actions β Observations).β # β’ PairwiseAction / PairwiseObservationβ # β’ LikertAction / LikertObservationβ # β’ ConsistencyAction/ ConsistencyObservationββββ π client.py # PreferenceLabEnv β thin wrapper aroundβ # openenv.core.MCPToolClient. Provides sync/asyncβ # tool-calling interface to the server.ββββ π inference.py # Baseline LLM inference script.β # Runs all three tasks end-to-end using theβ # OpenAI client against any compatible API.β # Produces structured START / STEP / END logs.ββββ π openenv.yaml # OpenEnv manifest (spec v1).β # Declares runtime=fastapi, app=server.app:app,β # port=8000, type=space.ββββ π pyproject.toml # Build config (setuptools) + project metadata.β # Core deps: openenv-core, fastapi, uvicorn,β # pydantic, openai, datasets, httpx, websockets.β # Dev extras: pytest, pytest-asyncio, httpx.ββββ π requirements.txt # Flat pip requirements (same as pyproject deps).ββββ π Dockerfile # Multi-stage Docker build.β # Builder stage: installs uv + deps via uv sync.β # Runtime stage: copies .venv + code, runs uvicorn.β # HEALTHCHECK: polls /health every 30s.ββββ π .dockerignore # Excludes .git, __pycache__, *.pyc, data/*.jsonβ # from Docker build context.ββββ π .gitignore # Standard Python .gitignore + data/*.json.βββββ π server/ # FastAPI server packageβ βββ π __init__.pyβ ββ βββ π app.py # App factory β calls openenv.core.create_app()β β # with PreferenceLabEnvironment as the factory,β β # PairwiseAction/Observation as the primary schema,β β # and MAX_CONCURRENT_ENVS (default: 64) for sessionβ β # concurrency. This produces all REST + WebSocketβ β # endpoints automatically.β ββ βββ π environment.py # Core environment logic (514 lines).β #β # ββ PreferenceLabEnvironment (Environment) βββββββ # β reset() β initial Observation ββ # β step() β (obs, reward, done, info) ββ # β state() β episode metadata dict ββ # βββββββββββββββββββββββββββββββββββββββββββββββββ #β # Graders:β # grade_pairwise() β {0.0, 0.1, 0.3, 1.0}β # grade_likert() β 1.0 - (MAE / 4.0)β # grade_consistency() β tau_score + transit_scoreβ #β # Synthetic fallback data embedded as functions:β # _synthetic_pairwise() β 8 examplesβ # _synthetic_likert() β 6 examplesβ # _synthetic_consistency() β 5 examplesβββββ π data/ # Dataset storage directoryβ βββ π .gitkeep # Ensures directory is tracked in git (data excluded)β βββ π README.md # Documents each JSON schemaβ ββ βββ π pairwise_data.json # [generated] HH-RLHF pairwise examplesβ β # Fields: prompt, response_a, response_b,β β # gold_label ("A"|"B"), sourceβ ββ βββ π likert_data.json # [generated] UltraFeedback multi-axis examplesβ β # Fields: prompt, response, rubric,β β # gold_scores {helpfulness, honesty,β β # harmlessness, instruction_following}β ββ βββ π consistency_data.json # [generated] Stanford SHP 4-way ranking examplesβ # Fields: prompt, response_a/b/c/d,β # gold_ranking (list[str])βββββ π scripts/ # Utility scriptsβ βββ π __init__.pyβ βββ π prepare_datasets.py # Downloads HH-RLHF, UltraFeedback, Stanford SHPβ # from Hugging Face and converts to PreferenceLabβ # JSON format. Writes to data/. Falls back gracefullyβ # if HF download fails.β # CLI: python scripts/prepare_datasets.py --samples Nβββββ π tests/ # Test suiteβββ π __init__.pyβββ π test_environment.py # Full test suite (248 lines, pytest).# TestPairwiseGrader (5 tests)# TestLikertGrader (4 tests)# TestConsistencyGrader (5 tests)# TestPreferenceLabEnvironment (11 tests)# Run: pytest tests/ -v```---181 unmodified lines182183184185186187370 unmodified lines558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636181 unmodified lines---### βοΈ Deployment Architecture```mermaid370 unmodified lines## File Architecture```mermaidgraph TDsubgraph Root["preference-lab/"]direction TBsubgraph Config["Configuration Files"]direction LRPY[pyproject.toml<br/>Build config & metadata]REQ[requirements.txt<br/>Python dependencies]OPEN[openenv.yaml<br/>OpenEnv manifest]DOCK[Dockerfile<br/>Multi-stage build]DOCKI[.dockerignore<br/>Docker exclusions]GITI[.gitignore<br/>Git exclusions]endsubgraph Core["Core Modules"]direction LRINIT[__init__.py<br/>Package exports]MOD[models.py<br/>Pydantic schemas]CLI[client.py<br/>OpenEnv client wrapper]INF[inference.py<br/>Baseline inference]endsubgraph Server["server/ - FastAPI Backend"]direction TBSINIT[__init__.py]SAPP[app.py<br/>App factory & endpoints]SENV[environment.py<br/>Core environment logic<br/>Graders & data loading]endsubgraph Data["data/ - Datasets"]direction TBDINIT[.gitkeep<br/>Directory placeholder]DREAD[README.md<br/>Schema documentation]PAIR[pairwise_data.json<br/>HH-RLHF examples]LIK[likert_data.json<br/>UltraFeedback examples]CONS[consistency_data.json<br/>Stanford SHP examples]endsubgraph Scripts["scripts/ - Utilities"]direction LRSCINIT[__init__.py]PREP[prepare_datasets.py<br/>Dataset preparation]endsubgraph Tests["tests/ - Test Suite"]direction LRTINIT[__init__.py]TENV[test_environment.py<br/>Environment tests]endsubgraph Standalone["Standalone Files"]direction LRTAPI[test_api.py<br/>API integration tests]endendclassDef config fill:#e3f2fd,stroke:#2196f3,color:#0d47a1classDef core fill:#f3e5f5,stroke:#9c27b0,color:#4a148cclassDef server fill:#e8f5e9,stroke:#4caf50,color:#1b5e20classDef data fill:#fff3e0,stroke:#ff9800,color:#e65100classDef scripts fill:#fce4ec,stroke:#e91e63,color:#880e4fclassDef tests fill:#f1f8e9,stroke:#8bc34a,color:#2e7d32classDef standalone fill:#e0f2f1,stroke:#009688,color:#004d40class PY,REQ,OPEN,DOCK,DOCKI,GITI configclass INIT,MOD,CLI,INF coreclass SINIT,SAPP,SENV serverclass DINIT,DREAD,PAIR,LIK,CONS dataclass SCINIT,PREP scriptsclass TINIT,TENV testsclass TAPI standalone``````---
README.md+72-245