Spaces:
Sleeping
Sleeping
Upload data_wrangler_env/client.py with huggingface_hub
Browse files- data_wrangler_env/client.py +77 -0
data_wrangler_env/client.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""DataWrangler Environment Client."""
|
| 8 |
+
|
| 9 |
+
from typing import Dict
|
| 10 |
+
|
| 11 |
+
from openenv.core import EnvClient
|
| 12 |
+
from openenv.core.client_types import StepResult
|
| 13 |
+
from openenv.core.env_server.types import State
|
| 14 |
+
|
| 15 |
+
from .models import DataWranglerAction, DataWranglerObservation
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class DataWranglerEnv(
|
| 19 |
+
EnvClient[DataWranglerAction, DataWranglerObservation, State]
|
| 20 |
+
):
|
| 21 |
+
"""
|
| 22 |
+
Client for the DataWrangler Environment.
|
| 23 |
+
|
| 24 |
+
Connects to the DataWrangler server over WebSocket for efficient
|
| 25 |
+
multi-step data cleaning interactions.
|
| 26 |
+
|
| 27 |
+
Example:
|
| 28 |
+
>>> with DataWranglerEnv(base_url="http://localhost:8000") as client:
|
| 29 |
+
... result = client.reset(task="task_1_easy")
|
| 30 |
+
... print(result.observation.response)
|
| 31 |
+
...
|
| 32 |
+
... result = client.step(DataWranglerAction(message="profile"))
|
| 33 |
+
... print(result.observation.response)
|
| 34 |
+
|
| 35 |
+
Example with Docker:
|
| 36 |
+
>>> client = DataWranglerEnv.from_docker_image("data_wrangler_env:latest")
|
| 37 |
+
>>> try:
|
| 38 |
+
... result = client.reset(task="task_1_easy")
|
| 39 |
+
... result = client.step(DataWranglerAction(message="find_missing"))
|
| 40 |
+
... finally:
|
| 41 |
+
... client.close()
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
def _step_payload(self, action: DataWranglerAction) -> Dict:
|
| 45 |
+
"""Convert DataWranglerAction to JSON payload for step message."""
|
| 46 |
+
return {
|
| 47 |
+
"message": action.message,
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
def _parse_result(self, payload: Dict) -> StepResult[DataWranglerObservation]:
|
| 51 |
+
"""Parse server response into StepResult[DataWranglerObservation]."""
|
| 52 |
+
obs_data = payload.get("observation", {})
|
| 53 |
+
observation = DataWranglerObservation(
|
| 54 |
+
response=obs_data.get("response", ""),
|
| 55 |
+
dataset_shape=obs_data.get("dataset_shape", ""),
|
| 56 |
+
current_score=obs_data.get("current_score", 0.0),
|
| 57 |
+
step_number=obs_data.get("step_number", 0),
|
| 58 |
+
max_steps=obs_data.get("max_steps", 30),
|
| 59 |
+
task_name=obs_data.get("task_name", ""),
|
| 60 |
+
available_commands=obs_data.get("available_commands", ""),
|
| 61 |
+
done=payload.get("done", False),
|
| 62 |
+
reward=payload.get("reward"),
|
| 63 |
+
metadata=obs_data.get("metadata", {}),
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
return StepResult(
|
| 67 |
+
observation=observation,
|
| 68 |
+
reward=payload.get("reward"),
|
| 69 |
+
done=payload.get("done", False),
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
def _parse_state(self, payload: Dict) -> State:
|
| 73 |
+
"""Parse server response into State object."""
|
| 74 |
+
return State(
|
| 75 |
+
episode_id=payload.get("episode_id"),
|
| 76 |
+
step_count=payload.get("step_count", 0),
|
| 77 |
+
)
|