Spaces:
Sleeping
Sleeping
Download environment/cloud_env.py from hackrock18/cloud-data-janitor: direct link, hf CLI and curl.
- Browser
- Download file 2.41 kB
-
https://huggingface.co/spaces/hackrock18/cloud-data-janitor/resolve/main/environment/cloud_env.py
- Command line
-
hf download hf://spaces/hackrock18/cloud-data-janitor/environment/cloud_env.py
-
curl -L -o cloud_env.py https://huggingface.co/spaces/hackrock18/cloud-data-janitor/resolve/main/environment/cloud_env.py
2.41 kB
| from environment.file_system import FileSystem | |
| from actions.file_actions import execute_action | |
| from grader.grader import grade_state | |
| from utils.helpers import load_initial_state | |
| from utils.helpers import load_task_criteria | |
| class CloudDataJanitorEnv: | |
| def __init__(self, task_name="easy"): | |
| self.task_name = task_name | |
| self.max_steps = 10 | |
| self.current_step = 0 | |
| self.done = False | |
| self.fs = None | |
| # REQUIRED | |
| def reset(self): | |
| self.current_step = 0 | |
| self.done = False | |
| initial_state = load_initial_state() | |
| criteria = load_task_criteria( | |
| self.task_name | |
| ) | |
| self.fs = FileSystem( | |
| initial_state, | |
| criteria | |
| ) | |
| # REQUIRED folders for grading | |
| self.fs.create_folder("Documents") | |
| self.fs.create_folder("Images") | |
| self.fs.create_folder("Misc") | |
| return self.state() | |
| # REQUIRED | |
| def state(self): | |
| return { | |
| "files": self.fs.list_files(), | |
| "step": self.current_step, | |
| "task": self.task_name | |
| } | |
| # REQUIRED | |
| def step(self, action): | |
| if self.done: | |
| return self.state(), 0.0, True, {} | |
| self.current_step += 1 | |
| error = None | |
| # Handle done action | |
| if action.get("action") == "done": | |
| self.done = True | |
| return ( | |
| self.state(), | |
| 1.0, | |
| True, | |
| { | |
| "last_action_error": None | |
| } | |
| ) | |
| try: | |
| action_type = action.get("action") | |
| params = { | |
| k: v | |
| for k, v in action.items() | |
| if k != "action" | |
| } | |
| execute_action( | |
| self.fs, | |
| action_type, | |
| params | |
| ) | |
| except Exception as e: | |
| error = str(e) | |
| score = grade_state( | |
| self.fs, | |
| self.task_name | |
| ) | |
| reward = float(score) | |
| if score >= 1.0: | |
| self.done = True | |
| if self.current_step >= self.max_steps: | |
| self.done = True | |
| return ( | |
| self.state(), | |
| reward, | |
| self.done, | |
| { | |
| "last_action_error": error | |
| } | |
| ) | |
| def close(self): | |
| pass |