File size: 18,237 Bytes
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea9eade
 
3818a51
 
ea9eade
 
 
 
 
 
 
3818a51
 
ea9eade
3818a51
 
 
 
af4e958
ea9eade
af4e958
 
ea9eade
 
 
 
 
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4ffe61
ea9eade
af4e958
ea9eade
af4e958
 
ea9eade
 
3818a51
 
af4e958
ea9eade
af4e958
 
ea9eade
3818a51
 
 
 
ea9eade
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea9eade
e4ffe61
ea9eade
 
 
 
 
 
3818a51
 
 
ea9eade
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3818a51
ea9eade
3818a51
ea9eade
3818a51
ea9eade
3818a51
ea9eade
 
 
3818a51
 
 
ea9eade
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3818a51
 
 
ea9eade
 
3818a51
ea9eade
3818a51
ea9eade
 
 
3818a51
 
 
 
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea9eade
 
 
 
 
3818a51
 
 
 
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3818a51
 
ea9eade
e4ffe61
ea9eade
 
 
 
 
e4ffe61
3818a51
 
 
 
 
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3818a51
 
 
ea9eade
af4e958
3818a51
 
 
ea9eade
af4e958
3818a51
 
 
ea9eade
af4e958
ea9eade
3818a51
 
 
 
ea9eade
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea9eade
 
3818a51
 
 
ea9eade
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3818a51
 
ea9eade
 
3818a51
 
 
 
 
 
ea9eade
3818a51
 
 
 
c2dd900
af4e958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3818a51
ea9eade
af4e958
 
 
c2dd900
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
"""FastAPI server exposing the FraudShield OpenEnv environment.

This module provides HTTP endpoints for the FraudShield fraud detection environment,
enabling agent access via standard REST/JSON interfaces. All endpoints follow OpenEnv
conventions (reset→step→state→done).

Architecture:
  - FraudShieldEnvironment: Core RL environment (manages episodes, rewards, grading)
  - FastAPI Server: HTTP API wrapper (async, production-grade error handling)
  - Data Pipeline: Frozen Kaggle snapshot (reproducible, seed=42)

Key Endpoints:
  - POST /reset: Start new episode (task=easy|medium|hard)
  - POST /step: Submit fraud decision (confidence [0,1] + decision)
  - GET /state: Current observation + history
  - GET /health: Service readiness probe
  - GET /info: Static metadata (task counts, schema)

Error Handling:
  - 400: Invalid request (missing fields, type mismatch)
  - 500: Server error (data load failure, internal exception)
  - 503: Service degraded (data not loaded)

Deployment:
  - Docker: python:3.11-slim, PORT=7860
  - HuggingFace Spaces: Supports auto-scaling
  - Health probe: GET /health before accepting traffic
"""

from __future__ import annotations

import logging
import os
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any, Dict

from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse

from fraudshield_env import FraudShieldEnvironment
from models import FraudCheckAction, TaskDifficulty

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Data path: points to ../data/ relative to this file
DATA_PATH = Path(__file__).resolve().parents[1] / "data"

# Global environment instance (singleton per process)
env = FraudShieldEnvironment(data_path=str(DATA_PATH), seed=42)


@asynccontextmanager
async def lifespan(_: FastAPI):
    """Load the bundled task set when the API process starts.
    
    This async context manager runs:
    - On startup: Load frozen data snapshot from disk (108 fraud cases)
    - On shutdown: Clean up environment resources (if needed)
    
    Raises:
        Error logged (not raised): If data fails to load, server starts in "degraded" mode.
        Health probe will return status="degraded" until data is available.
    
    Note:
        Non-blocking: Startup completes even if data load fails. Requests will fail
        with 503 if data isn't available. This allows graceful degradation and retry.
    """
    # On startup
    if not env.load_data():
        logger.error("FraudShield failed to load its bundled data from %s", DATA_PATH)
    # Yield control back to FastAPI (server is ready to accept requests)
    yield
    # On shutdown (cleanup if needed)
    logger.info("FraudShield server shutting down")


app = FastAPI(
    title="FraudShield",
    description="OpenEnv-compatible e-commerce fraud review environment. Agents submit fraud review decisions and receive dense rewards based on business cost (true positives valuable, false positives costly).",
    version="0.2.0",
    docs_url="/docs",  # Swagger UI
    openapi_url="/openapi.json",
    lifespan=lifespan,
)


@app.get("/health")
async def health_check() -> Dict[str, Any]:
    """Container health probe response.
    
    This endpoint is used by load balancers (HuggingFace, Docker Swarm) to determine
    if the service is ready to accept traffic. Always returns 200 with status indicator.
    
    Returns:
        Dict with keys:
        - status: "healthy" if data loaded, "degraded" if not
        - service: "fraudshield" (service identifier)
        - data_loaded: bool (whether frozen snapshot is available)
    
    Example:
        GET /health
        200 OK
        {
          "status": "healthy",
          "service": "fraudshield",
          "data_loaded": true
        }
    
    Note:
        Even if data is not loaded on startup, this probe succeeds at 200.
        Load balancers should check the status field to determine readiness.
    """
    if not env.data_loaded:
        env.load_data()

    return {
        "status": "healthy" if env.data_loaded else "degraded",
        "service": "fraudshield",
        "data_loaded": env.data_loaded,
    }


@app.post("/reset")
async def reset(task: TaskDifficulty = TaskDifficulty.EASY) -> Dict[str, Any]:
    """Start a new episode for the requested task difficulty.
    
    This endpoint initializes a fresh episode with a random transaction sequence
    from the requested task difficulty level.
    
    Args:
        task: Task difficulty ("easy", "medium", or "hard").
            - easy: 45 transactions, clear separation between fraud/legitimate
            - medium: 50 transactions, mixed signals, confidence calibration matters
            - hard: 65 transactions, coordinated abuse, edge cases, tight thresholds
    
    Returns:
        Dict with keys:
        - observation: First observation dict (transaction + history)
        - info: Episode metadata (task, episode_id, max_steps)
        - episode_id: Unique identifier for this episode (string)
    
    Raises:
        500: Data not loaded or internal error
    
    Example:
        POST /reset?task=easy
        200 OK
        {
          "observation": {
            "transaction_id": "txn_001",
            "amount": 125.50,
            "merchant": "ACME_STORE",
            "country": "US",
            "is_fraud": null,
            ...26 more fields...
            "history": [...]
          },
          "info": {
            "task": "easy",
            "episode_id": "ep_abc123",
            "max_steps": 45
          },
          "episode_id": "ep_abc123"
        }
    
    Note:
        Each call to /reset generates a new episode. Previous episode state is discarded.
        Seed=42 ensures reproducibility across runs.
    """
    try:
        result = env.reset(task.value)
        return {
            "observation": result.observation.model_dump(mode="json"),
            "info": result.info,
            "episode_id": env.episode_id,
        }
    except Exception as exc:
        logger.exception("Reset error")
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@app.post("/step")
async def step(action: FraudCheckAction) -> Dict[str, Any]:
    """Submit one fraud review decision to the environment.
    
    This endpoint processes an agent's fraud decision and returns the reward,
    next observation, and episode termination status.
    
    Args:
        action: FraudCheckAction model with fields:
            - decision: "APPROVE" or "REJECT" (enum)
            - confidence: float in [0.0, 1.0] (confidence in decision)
            Additional optional fields: reason, metadata
    
    Returns:
        Dict with keys:
        - observation: Next observation dict (new transaction or final state)
        - reward: Reward dict with dense reward component
            {
              "dense": -1.0 to +1.0,  # Business cost-sensitive signal
              "info": {...}            # Breakdown (TP/FP/TN/FN signals)
            }
        - done: bool (True if episode complete)
        - info: Episode state info
    
    Raises:
        400: Invalid action format or invalid confidence range
        500: Environment step error
    
    Example:
        POST /step
        Content-Type: application/json
        {
          "decision": "REJECT",
          "confidence": 0.92
        }
        
        200 OK
        {
          "observation": {
            "transaction_id": "txn_002",
            "amount": 250.00,
            ...26 more fields...
          },
          "reward": {
            "dense": 0.95,
            "info": {"signal": "true_positive"}
          },
          "done": false,
          "info": {"step": 1, "episode_id": "ep_abc123"}
        }
    
    Reward Breakdown:
        - True Positive (detected fraud correctly): +0.95
        - True Negative (approved legitimate): +0.80
        - False Positive (rejected legitimate): -0.50
        - False Negative (approved fraud): -1.00
        - Confidence penalty: applied if |confidence - accuracy| > threshold
    
    Note:
        Must call /reset before first /step in an episode.
        Episode terminates when all transactions reviewed (done=true).
    """
    try:
        result = env.step(action)
        return {
            "observation": result.observation.model_dump(mode="json"),
            "reward": result.reward.model_dump(mode="json"),
            "done": result.done,
            "info": result.info,
        }
    except Exception as exc:
        logger.exception("Step error")
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@app.get("/state")
async def get_state() -> Dict[str, Any]:
    """Return the current episode state (observation + metadata).
    
    This endpoint retrieves the complete state without taking any action.
    Useful for agents that want to inspect the current episode before submitting
    the next decision.
    
    Returns:
        EpisodeState dict with keys:
        - observation: Current transaction dict (same as last reward.observation)
        - episode_id: Unique episode identifier
        - task: Current task difficulty ("easy", "medium", "hard")
        - step_count: Number of decisions made so far
        - done: Whether episode is complete
        - cumulative_reward: Sum of all dense rewards so far
        - reward_history: List of all past reward dicts
    
    Raises:
        500: Environment state error
    
    Example:
        GET /state
        200 OK
        {
          "observation": {...},
          "episode_id": "ep_abc123",
          "task": "easy",
          "step_count": 5,
          "done": false,
          "cumulative_reward": 2.45,
          "reward_history": [
            {"dense": 0.95},
            {"dense": 0.80},
            ...
          ]
        }
    
    Note:
        Returns 500 if no episode is active (call /reset first).
    """
    try:
        return env.state().model_dump(mode="json")
    except Exception as exc:
        logger.exception("State error")
        raise HTTPException(status_code=500, detail=str(exc)) from exc


@app.get("/info")
async def get_info() -> Dict[str, Any]:
    """Return static environment metadata and schema.
    
    This endpoint provides read-only information about the environment:
    - Task configuration (max steps per difficulty)
    - Data snapshot version and statistics
    - Schema information
    
    Returns:
        Dict with keys:
        - name: "fraudshield"
        - version: "0.2.0"
        - description: Environment description
        - tasks: Dict mapping task names to max_steps counts
        - data_path: Path to frozen data directory
        - data_snapshot: Snapshot metadata (row count, seed, URL)
    
    Example:
        GET /info
        200 OK
        {
          "name": "fraudshield",
          "version": "0.2.0",
          "description": "E-commerce fraud review environment...",
          "tasks": {
            "easy": 45,
            "medium": 50,
            "hard": 65
          },
          "data_path": "/app/data",
          "data_snapshot": {
            "rows": 108,
            "seed": 42,
            "source": "kaggle/creditcard (frozen)"
          }
        }
    
    Note:
        This endpoint does not require an active episode.
        Safe to call for introspection before /reset.
    """
    return {
        "name": "fraudshield",
        "version": "0.2.0",
        "description": "E-commerce fraud review environment built from a frozen public-data snapshot.",
        "tasks": {
            task.value: {"max_steps": max_steps}
            for task, max_steps in env.max_steps.items()
        },
        "data_path": str(DATA_PATH),
        "data_snapshot": env.data_loader.get_bundle_summary(),
    }


@app.get("/tasks")
async def get_tasks() -> Dict[str, Any]:
    """Describe the available task variants and their characteristics.
    
    Tasks differ in difficulty, transaction count, data distribution, and reward signals:
    
    - **easy**: Clear patterns, few edge cases
      - 45 transactions from frozen Kaggle snapshot
      - Fraud indicators obvious (e.g., repeated high-value failures)
      - Good baseline: naive heuristics score ~0.95 F1
    
    - **medium**: Mixed signals, confidence matters
      - 50 transactions with intentional noise
      - Some legitimate high-value txns, some fraud with low signals
      - Confidence calibration important: overconfident = lower score
      - Good baseline: ~0.88 F1 with learnable thresholds
    
    - **hard**: Adversarial patterns, tight thresholds
      - 65 transactions including coordinated abuse schemes
      - Edge cases: buying cards from physical stores (weird but legitimate)
      - Flash-sale spike patterns (legitimate but unusual)
      - Good baseline: ~0.72 F1 with domain knowledge + anomaly detection
    
    Returns:
        Dict mapping task names to task descriptors with:
        - difficulty: one of "easy", "medium", "hard"
        - num_transactions: transaction count in this task
        - description: Human-readable summary of patterns and difficulty factors
    
    Example:
        GET /tasks
        200 OK
        {
          "easy": {
            "difficulty": "easy",
            "num_transactions": 45,
            "description": "Clear-cut fraud indicators and low-noise legitimate cases."
          },
          "medium": {
            "difficulty": "medium",
            "num_transactions": 50,
            "description": "Mixed-signal reviews where confidence calibration matters."
          },
          "hard": {
            "difficulty": "hard",
            "num_transactions": 65,
            "description": "Coordinated abuse and legitimate flash-sale edge cases."
          }
        }
    
    Note:
        All tasks use the same underlying data (Kaggle snapshot), but pre-shuffled
        into difficulty-stratified subsets. Randomization via seed=42 ensures reproducibility.
    """
    return {
        "easy": {
            "difficulty": "easy",
            "num_transactions": env.max_steps[TaskDifficulty.EASY],
            "description": "Clear-cut fraud indicators and low-noise legitimate cases. Baseline: simple heuristics (high-value repeated failures) score ~0.95 F1.",
        },
        "medium": {
            "difficulty": "medium",
            "num_transactions": env.max_steps[TaskDifficulty.MEDIUM],
            "description": "Mixed-signal reviews where confidence calibration matters. Legitimate high-value txns exist; some fraud is subtle. Requires careful thresholding. Baseline: ~0.88 F1.",
        },
        "hard": {
            "difficulty": "hard",
            "num_transactions": env.max_steps[TaskDifficulty.HARD],
            "description": "Coordinated abuse and legitimate flash-sale edge cases. Card testing patterns, unusual geographies, spike behavior. Requires domain expertise. Baseline: ~0.72 F1.",
        },
    }


@app.exception_handler(Exception)
async def global_exception_handler(_, exc: Exception) -> JSONResponse:
    """Catch any unhandled exception with a JSON error body.
    
    This handler ensures all exceptions (even unexpected ones) return valid JSON
    with appropriate HTTP status codes, rather than letting FastAPI's default
    HTML error pages leak internal details.
    
    Args:
        exc: The uncaught exception
    
    Returns:
        JSONResponse with status_code=500 and error detail
    
    Note:
        HTTPException handlers (reset, step, state) are caught and handled separately.
        This handler catches everything else (programming errors, system errors).
    """
    logger.exception("Unhandled exception")
    return JSONResponse(status_code=500, content={"detail": str(exc)})


@app.get("/")
async def root() -> Dict[str, Any]:
    """Service landing page and API route reference.
    
    This endpoint provides a quick reference for all available routes.
    Primary users: documentation crawlers, health dashboards, agent clients.
    
    Returns:
        Dict with service metadata and endpoint map
    
    Example:
        GET /
        200 OK
        {
          "service": "FraudShield OpenEnv",
          "version": "0.2.0",
          "description": "E-commerce fraud review environment...",
          "endpoints": {
            "health": "GET /health",
            "reset": "POST /reset?task=easy|medium|hard",
            "step": "POST /step",
            "state": "GET /state",
            "info": "GET /info",
            "tasks": "GET /tasks"
          },
          "docs": "/docs"
        }
    
    Note:
        Visit /docs (Swagger UI) for interactive endpoint testing.
        Visit /openapi.json for machine-readable OpenAPI schema.
    """
    return {
        "service": "FraudShield OpenEnv",
        "version": "0.2.0",
        "description": "E-commerce fraud review environment for agent training and evaluation.",
        "endpoints": {
            "health": "GET /health",
            "reset": "POST /reset?task=easy|medium|hard",
            "step": "POST /step",
            "state": "GET /state",
            "info": "GET /info",
            "tasks": "GET /tasks",
        },
        "docs": "/docs",
    }

def main() -> None:
    """Launch the FraudShield API server.
    
    Starts a production-grade Uvicorn ASGI server with:
    - Host: 0.0.0.0 (accessible from all interfaces)
    - Port: 7860 (default) or via PORT env var
    - Workers: 1 (single worker; for scaling use Kubernetes/Docker Swarm)
    - Async: Full async/await support via FastAPI
    
    Environment Variables:
        PORT: Server port (default: 7860)
    
    Example:
        PORT=8000 python -m server.app
        # or:
        python -m server.app
        # Then: curl http://localhost:7860/health
    
    Note:
        This is the entrypoint for Docker containers.
        Designed for HuggingFace Spaces and Kubernetes.
    """
    import uvicorn

    port = int(os.getenv("PORT", "7860"))
    logger.info("Launching FraudShield server on port %d", port)
    uvicorn.run(app, host="0.0.0.0", port=port, workers=1)


if __name__ == "__main__":  # pragma: no cover
    main()