An IsolationForest is fitted once on a synthetic normal dataset and then serves anomaly scores for
incoming data points. In a production monitoring setting many independent scoring requests arrive
continuously — each request contains a batch of 2D points and expects back a classification
(normal / anomaly) and a raw anomaly score.
Processing these requests individually underutilises the forest's vectorised scoring. Instead, many
independent prediction requests that can be efficiently processed together as a batch should be
collected and scored with one score_samples call.
Task
Port workflow.py so that the batch anomaly scoring function runs as a remote service.
The workflow:
Fits IsolationForest(contamination=0.1, random_state=0) on
make_blobs(n_samples=300, centers=1, cluster_std=0.5, random_state=0)[0].
Scales with StandardScaler fitted on the same training data.
Accepts a case dict with key samples: a list of N 2D points.
Scales the samples; calls predict (1 = normal, -1 = anomaly) and score_samples in one pass.
Returns predictions (list of int), anomaly_scores (list of float), n_anomalies (int),
batch_size (int).
The fitted model and scaler must be initialised once and reused across many requests.
Both predict and score_samples must operate on the full batch array, not individual points.
The service should handle batches of varying sizes (20–60 points).
Required solution interface
Write the solution in /app/solution/modal_app.py. Define a Modal application
object named app and expose a callable named run_case that accepts one case
dictionary and returns a JSON-serializable result matching the original
workflow.
Runtime file layout
The finalized task workspace is copied into /app when the sandbox starts.
Source files from environment/workspace are available at paths such as
/app/workflow.py, /app/original_workflow.py, /app/original.py, or other
task-specific filenames.
Capability contract
Expose the workflow as a single Modal remote function: define run_case as an @app.function so the harness can invoke it with run_case.remote(case).