agentbaba commited on
Commit
bfc51b8
·
verified ·
1 Parent(s): fd4d8d6

Upload productivity_env/env.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. productivity_env/env.py +95 -44
productivity_env/env.py CHANGED
@@ -1,31 +1,44 @@
1
- from typing import Dict, Any
2
- from openenv.core import OpenEnv, StepResult
3
 
4
- from .models import ProductivityAction, ProductivityObservation
5
  import os
6
  import sys
7
 
8
- # Add parent directory to path so data_pipeline can be imported
 
 
 
 
9
  sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
10
  sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".")))
11
 
12
  from data_pipeline.inference import copilot
13
 
14
- class ProductivityEnv(OpenEnv[ProductivityAction, ProductivityObservation]):
 
15
  def __init__(self, task_name: str = "triage"):
16
  super().__init__()
17
  self.task_name = task_name
18
  self.state_data: Dict[str, Any] = {}
19
  self.max_steps = 10
20
  self.current_step = 0
 
21
  try:
22
  copilot.load()
23
- except Exception as e:
24
  print("Warning: Models could not be loaded. Please ensure model_artifacts are present.")
25
 
26
- async def reset(self) -> StepResult[ProductivityObservation]:
 
 
 
 
 
27
  self.current_step = 0
28
- # Initialize different scenarios based on the task name
 
 
 
 
29
  if self.task_name == "triage":
30
  self.state_data = {
31
  "session_duration_minutes": 180,
@@ -43,7 +56,7 @@ class ProductivityEnv(OpenEnv[ProductivityAction, ProductivityObservation]):
43
  "focus_score": 0.3,
44
  "motivation_level": 4,
45
  "study_hours_weekly": 15,
46
- "current_task": "Write Final Report"
47
  }
48
  elif self.task_name == "schedule_optimization":
49
  self.state_data = {
@@ -51,7 +64,7 @@ class ProductivityEnv(OpenEnv[ProductivityAction, ProductivityObservation]):
51
  "break_count": 3,
52
  "social_media_minutes_before": 10,
53
  "task_complexity": 5,
54
- "work_style_score": 0.9, # Turtle
55
  "time_of_day_hour": 10,
56
  "day_of_week": 1,
57
  "stress_level": 4,
@@ -62,7 +75,7 @@ class ProductivityEnv(OpenEnv[ProductivityAction, ProductivityObservation]):
62
  "focus_score": 0.8,
63
  "motivation_level": 6,
64
  "study_hours_weekly": 40,
65
- "current_task": "Code Review"
66
  }
67
  elif self.task_name == "distraction_mitigation":
68
  self.state_data = {
@@ -70,7 +83,7 @@ class ProductivityEnv(OpenEnv[ProductivityAction, ProductivityObservation]):
70
  "break_count": 1,
71
  "social_media_minutes_before": 120,
72
  "task_complexity": 2,
73
- "work_style_score": 0.2, # Hare
74
  "time_of_day_hour": 18,
75
  "day_of_week": 5,
76
  "stress_level": 5,
@@ -81,25 +94,41 @@ class ProductivityEnv(OpenEnv[ProductivityAction, ProductivityObservation]):
81
  "focus_score": 0.2,
82
  "motivation_level": 2,
83
  "study_hours_weekly": 5,
84
- "current_task": "Update Documentation"
85
  }
86
  else:
87
- # default
88
  self.state_data = {
89
- "session_duration_minutes": 120, "break_count": 2, "social_media_minutes_before": 15,
90
- "task_complexity": 3, "work_style_score": 0.5, "time_of_day_hour": 10, "day_of_week": 1,
91
- "stress_level": 5, "sleep_hours": 7, "distraction_events": 5, "deadline_days_remaining": 3.0,
92
- "previous_completion_rate": 0.7, "focus_score": 0.6, "motivation_level": 6,
93
- "study_hours_weekly": 20, "current_task": "General Work"
 
 
 
 
 
 
 
 
 
 
 
94
  }
95
-
96
  obs = self._get_obs()
97
- return StepResult(observation=obs, reward=0.0, done=False)
98
-
 
 
 
99
  def _get_obs(self) -> ProductivityObservation:
 
 
 
100
  fp_res = copilot.predict_failure(self.state_data)
101
  dist_res = copilot.score_distraction(self.state_data)
102
-
103
  return ProductivityObservation(
104
  time_of_day_hour=float(self.state_data["time_of_day_hour"]),
105
  stress_level=float(max(0, min(10, self.state_data["stress_level"]))),
@@ -111,19 +140,22 @@ class ProductivityEnv(OpenEnv[ProductivityAction, ProductivityObservation]):
111
  social_media_minutes=int(self.state_data["social_media_minutes_before"]),
112
  current_task=str(self.state_data["current_task"]),
113
  deadline_days_remaining=float(self.state_data["deadline_days_remaining"]),
114
- failure_probability=float(fp_res["failure_probability"])
115
  )
116
 
117
- async def step(self, action: ProductivityAction) -> StepResult[ProductivityObservation]:
 
 
 
 
 
118
  self.current_step += 1
119
-
120
- # In a real environment, step increments time.
121
  self.state_data["session_duration_minutes"] += 30
122
  self.state_data["time_of_day_hour"] = (self.state_data["time_of_day_hour"] + 0.5) % 24
123
- self.state_data["deadline_days_remaining"] -= (30.0 / 1440.0) # fraction of day
124
- self.state_data["distraction_events"] += 1 # Default environment hostility
125
-
126
- # Apply action effect on human state
127
  action_type = action.action_type.upper()
128
  if action_type == "FORCE_BREAK":
129
  self.state_data["break_count"] += 1
@@ -138,21 +170,40 @@ class ProductivityEnv(OpenEnv[ProductivityAction, ProductivityObservation]):
138
  self.state_data["motivation_level"] = min(10, self.state_data["motivation_level"] + 2)
139
  self.state_data["stress_level"] = max(0, self.state_data["stress_level"] - 0.5)
140
  self.state_data["distraction_events"] = max(0, self.state_data["distraction_events"] - 1)
141
- elif action_type == "WAIT":
142
- # the environment natively worsens without intervention if it's already bad.
143
- if self.state_data["stress_level"] > 6:
144
- self.state_data["stress_level"] += 0.5
145
 
146
  obs = self._get_obs()
147
-
148
- # Base reward on inverse of failure probability
149
- # A good agent lowers failure probability.
150
  reward = (1.0 - obs.failure_probability) * 0.1
151
-
152
- # Penalties for stressing the user
153
  if obs.stress_level >= 8.0:
154
  reward -= 0.05
155
-
156
- done = self.current_step >= self.max_steps
157
-
158
- return StepResult(observation=obs, reward=reward, done=done)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Dict, Optional
 
2
 
 
3
  import os
4
  import sys
5
 
6
+ from openenv.core import Environment
7
+
8
+ from .models import ProductivityAction, ProductivityObservation, ProductivityState
9
+
10
+ # Add parent directory to path so data_pipeline can be imported.
11
  sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
12
  sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".")))
13
 
14
  from data_pipeline.inference import copilot
15
 
16
+
17
+ class ProductivityEnv(Environment[ProductivityAction, ProductivityObservation, ProductivityState]):
18
  def __init__(self, task_name: str = "triage"):
19
  super().__init__()
20
  self.task_name = task_name
21
  self.state_data: Dict[str, Any] = {}
22
  self.max_steps = 10
23
  self.current_step = 0
24
+ self.episode_id: Optional[str] = None
25
  try:
26
  copilot.load()
27
+ except Exception:
28
  print("Warning: Models could not be loaded. Please ensure model_artifacts are present.")
29
 
30
+ def reset(
31
+ self,
32
+ seed: Optional[int] = None,
33
+ episode_id: Optional[str] = None,
34
+ **kwargs: Any,
35
+ ) -> ProductivityObservation:
36
  self.current_step = 0
37
+ self.episode_id = episode_id
38
+ task_name = kwargs.get("task_name", self.task_name)
39
+ if task_name:
40
+ self.task_name = str(task_name)
41
+
42
  if self.task_name == "triage":
43
  self.state_data = {
44
  "session_duration_minutes": 180,
 
56
  "focus_score": 0.3,
57
  "motivation_level": 4,
58
  "study_hours_weekly": 15,
59
+ "current_task": "Write Final Report",
60
  }
61
  elif self.task_name == "schedule_optimization":
62
  self.state_data = {
 
64
  "break_count": 3,
65
  "social_media_minutes_before": 10,
66
  "task_complexity": 5,
67
+ "work_style_score": 0.9,
68
  "time_of_day_hour": 10,
69
  "day_of_week": 1,
70
  "stress_level": 4,
 
75
  "focus_score": 0.8,
76
  "motivation_level": 6,
77
  "study_hours_weekly": 40,
78
+ "current_task": "Code Review",
79
  }
80
  elif self.task_name == "distraction_mitigation":
81
  self.state_data = {
 
83
  "break_count": 1,
84
  "social_media_minutes_before": 120,
85
  "task_complexity": 2,
86
+ "work_style_score": 0.2,
87
  "time_of_day_hour": 18,
88
  "day_of_week": 5,
89
  "stress_level": 5,
 
94
  "focus_score": 0.2,
95
  "motivation_level": 2,
96
  "study_hours_weekly": 5,
97
+ "current_task": "Update Documentation",
98
  }
99
  else:
 
100
  self.state_data = {
101
+ "session_duration_minutes": 120,
102
+ "break_count": 2,
103
+ "social_media_minutes_before": 15,
104
+ "task_complexity": 3,
105
+ "work_style_score": 0.5,
106
+ "time_of_day_hour": 10,
107
+ "day_of_week": 1,
108
+ "stress_level": 5,
109
+ "sleep_hours": 7,
110
+ "distraction_events": 5,
111
+ "deadline_days_remaining": 3.0,
112
+ "previous_completion_rate": 0.7,
113
+ "focus_score": 0.6,
114
+ "motivation_level": 6,
115
+ "study_hours_weekly": 20,
116
+ "current_task": "General Work",
117
  }
118
+
119
  obs = self._get_obs()
120
+ obs.reward = 0.0
121
+ obs.done = False
122
+ obs.metadata = {"task_name": self.task_name, "episode_id": self.episode_id, "seed": seed}
123
+ return obs
124
+
125
  def _get_obs(self) -> ProductivityObservation:
126
+ if not self.state_data:
127
+ self.reset()
128
+
129
  fp_res = copilot.predict_failure(self.state_data)
130
  dist_res = copilot.score_distraction(self.state_data)
131
+
132
  return ProductivityObservation(
133
  time_of_day_hour=float(self.state_data["time_of_day_hour"]),
134
  stress_level=float(max(0, min(10, self.state_data["stress_level"]))),
 
140
  social_media_minutes=int(self.state_data["social_media_minutes_before"]),
141
  current_task=str(self.state_data["current_task"]),
142
  deadline_days_remaining=float(self.state_data["deadline_days_remaining"]),
143
+ failure_probability=float(fp_res["failure_probability"]),
144
  )
145
 
146
+ def step(
147
+ self,
148
+ action: ProductivityAction,
149
+ timeout_s: Optional[float] = None,
150
+ **kwargs: Any,
151
+ ) -> ProductivityObservation:
152
  self.current_step += 1
153
+
 
154
  self.state_data["session_duration_minutes"] += 30
155
  self.state_data["time_of_day_hour"] = (self.state_data["time_of_day_hour"] + 0.5) % 24
156
+ self.state_data["deadline_days_remaining"] -= 30.0 / 1440.0
157
+ self.state_data["distraction_events"] += 1
158
+
 
159
  action_type = action.action_type.upper()
160
  if action_type == "FORCE_BREAK":
161
  self.state_data["break_count"] += 1
 
170
  self.state_data["motivation_level"] = min(10, self.state_data["motivation_level"] + 2)
171
  self.state_data["stress_level"] = max(0, self.state_data["stress_level"] - 0.5)
172
  self.state_data["distraction_events"] = max(0, self.state_data["distraction_events"] - 1)
173
+ elif action_type == "WAIT" and self.state_data["stress_level"] > 6:
174
+ self.state_data["stress_level"] += 0.5
 
 
175
 
176
  obs = self._get_obs()
 
 
 
177
  reward = (1.0 - obs.failure_probability) * 0.1
 
 
178
  if obs.stress_level >= 8.0:
179
  reward -= 0.05
180
+
181
+ obs.reward = reward
182
+ obs.done = self.current_step >= self.max_steps
183
+ obs.metadata = {
184
+ "task_name": self.task_name,
185
+ "step_count": self.current_step,
186
+ "timeout_s": timeout_s,
187
+ }
188
+ return obs
189
+
190
+ @property
191
+ def state(self) -> ProductivityState:
192
+ obs = self._get_obs()
193
+ return ProductivityState(
194
+ episode_id=self.episode_id,
195
+ step_count=self.current_step,
196
+ task_name=self.task_name,
197
+ current_task=obs.current_task,
198
+ deadline_days_remaining=obs.deadline_days_remaining,
199
+ stress_level=obs.stress_level,
200
+ motivation_level=obs.motivation_level,
201
+ distraction_events=obs.distraction_events,
202
+ focus_score=obs.focus_score,
203
+ failure_probability=obs.failure_probability,
204
+ session_duration_minutes=obs.session_duration_minutes,
205
+ break_count=obs.break_count,
206
+ social_media_minutes=obs.social_media_minutes,
207
+ time_of_day_hour=obs.time_of_day_hour,
208
+ raw_state=dict(self.state_data),
209
+ )