data-cleaning-env / client.py
vedastra's picture
Upload folder using huggingface_hub
7af055a verified
Raw
History Blame Contribute Delete
2.24 kB
"""Data Cleaning Env Environment Client."""
from typing import Dict, Optional
from openenv.core import EnvClient
from openenv.core.client_types import StepResult
from openenv.core.env_server.types import State
from .models import DataCleaningAction, DataCleaningObservation
class DataCleaningEnv(
EnvClient[DataCleaningAction, DataCleaningObservation, State]
):
"""
Client for the Data Cleaning Env Environment.
Example:
>>> with DataCleaningEnv(base_url="http://localhost:8000") as client:
... result = client.reset()
... obs = result.observation
... print(obs.current_text)
... result = client.step(DataCleaningAction(operation="remove_duplicates"))
... print(result.reward)
"""
def _step_payload(self, action: DataCleaningAction) -> Dict:
"""Convert DataCleaningAction to JSON payload."""
payload = {"operation": action.operation}
if action.column is not None:
payload["column"] = action.column
return payload
def _parse_result(self, payload: Dict) -> StepResult[DataCleaningObservation]:
"""Parse server response into StepResult[DataCleaningObservation]."""
obs_data = payload.get("observation", {})
# done and reward live at top-level in the server response
done = payload.get("done", obs_data.get("done", False))
reward = payload.get("reward", obs_data.get("reward"))
observation = DataCleaningObservation(
current_text=obs_data.get("current_text", ""),
is_normalized=obs_data.get("is_normalized", False),
html_found=obs_data.get("html_found", False),
remaining_typos=obs_data.get("remaining_typos", 0),
done=done,
reward=reward,
metadata=obs_data.get("metadata", {}),
)
return StepResult(
observation=observation,
reward=reward,
done=done,
)
def _parse_state(self, payload: Dict) -> State:
"""Parse server response into State object."""
return State(
episode_id=payload.get("episode_id"),
step_count=payload.get("step_count", 0),
)