Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -123,23 +123,29 @@ def optimize_signal_rl(congestion_level):
|
|
123 |
# Train the model
|
124 |
model.learn(total_timesteps=1000)
|
125 |
|
126 |
-
# Reset environment
|
127 |
-
obs, info = env.reset() # For Gymnasium 1.0.0
|
128 |
-
obs = obs[0] # Extract
|
129 |
|
130 |
for _ in range(10):
|
131 |
# Predict action
|
132 |
-
action, _ = model.predict(obs)
|
133 |
|
134 |
# Take a step
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
if done or truncated:
|
144 |
break
|
145 |
|
@@ -151,6 +157,7 @@ def optimize_signal_rl(congestion_level):
|
|
151 |
return "Error in RL Optimization"
|
152 |
|
153 |
|
|
|
154 |
def process_traffic_image(image):
|
155 |
"""
|
156 |
Orchestrates the traffic analysis workflow.
|
|
|
123 |
# Train the model
|
124 |
model.learn(total_timesteps=1000)
|
125 |
|
126 |
+
# Reset the environment
|
127 |
+
obs, info = env.reset() # For Gymnasium 1.0.0
|
128 |
+
obs = obs[0] # Extract single observation from the batched observation
|
129 |
|
130 |
for _ in range(10):
|
131 |
# Predict action
|
132 |
+
action, _ = model.predict(obs, deterministic=True)
|
133 |
|
134 |
# Take a step
|
135 |
+
step_output = env.step(action)
|
136 |
+
|
137 |
+
# Handle batched output of step()
|
138 |
+
if len(step_output) == 5:
|
139 |
+
obs, reward, done, truncated, info = step_output
|
140 |
+
obs = obs[0] # Extract single observation
|
141 |
+
reward = reward[0] # Extract single reward
|
142 |
+
done = done[0] # Extract single done flag
|
143 |
+
truncated = truncated[0] # Extract single truncated flag
|
144 |
+
info = info[0] # Extract single info
|
145 |
+
else:
|
146 |
+
raise ValueError(f"Unexpected step output: {step_output}")
|
147 |
+
|
148 |
+
# End simulation if done or truncated
|
149 |
if done or truncated:
|
150 |
break
|
151 |
|
|
|
157 |
return "Error in RL Optimization"
|
158 |
|
159 |
|
160 |
+
|
161 |
def process_traffic_image(image):
|
162 |
"""
|
163 |
Orchestrates the traffic analysis workflow.
|