MBG0903 commited on
Commit
c7f64e3
Β·
verified Β·
1 Parent(s): caada9a

Update agents/reasoner.py

Browse files
Files changed (1) hide show
  1. agents/reasoner.py +16 -14
agents/reasoner.py CHANGED
@@ -2,6 +2,8 @@ import pandas as pd
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  import io
 
 
5
 
6
  # ============================================================
7
  # SLOTING OPTIMIZATION β€” PHASE 1
@@ -61,6 +63,7 @@ def run_picking_optimization(message, picking_df):
61
  df = df.sort_values("Distance").reset_index(drop=True)
62
  reasoning_steps.append("Calculated Manhattan distance & sorted sequence.")
63
 
 
64
  plt.figure(figsize=(6, 6))
65
  plt.plot(df["x"], df["y"], marker="o", linestyle="-")
66
  plt.title("Optimized Picking Route")
@@ -69,8 +72,11 @@ def run_picking_optimization(message, picking_df):
69
 
70
  buffer = io.BytesIO()
71
  plt.savefig(buffer, format="png")
72
- buffer.seek(0)
73
  plt.close()
 
 
 
 
74
 
75
  reasoning_steps.append("Generated walking route visualization.")
76
 
@@ -80,7 +86,7 @@ def run_picking_optimization(message, picking_df):
80
  "#### πŸ” Reasoning\n" + "\n".join([f"- {r}" for r in reasoning_steps])
81
  )
82
 
83
- return explanation, buffer
84
 
85
 
86
  # ============================================================
@@ -114,7 +120,7 @@ def run_demand_forecast(message, slotting_df):
114
 
115
  reasoning_steps.append("Generated 7-day demand projection.")
116
 
117
- # Plot forecast
118
  plt.figure(figsize=(6, 4))
119
  plt.plot(forecast_df["Day"], forecast_df["Forecasted_Demand"], marker="o")
120
  plt.title("7-Day Demand Forecast")
@@ -123,8 +129,11 @@ def run_demand_forecast(message, slotting_df):
123
 
124
  buffer = io.BytesIO()
125
  plt.savefig(buffer, format="png")
126
- buffer.seek(0)
127
  plt.close()
 
 
 
 
128
 
129
  explanation = (
130
  "### πŸ“ˆ Demand Forecasting\n"
@@ -132,11 +141,11 @@ def run_demand_forecast(message, slotting_df):
132
  "#### πŸ” Reasoning\n" + "\n".join([f"- {r}" for r in reasoning_steps])
133
  )
134
 
135
- return explanation, buffer, forecast_df
136
 
137
 
138
  # ============================================================
139
- # REPLENISHMENT OPTIMIZATION β€” MODULE 2
140
  # ============================================================
141
 
142
  def run_replenishment_analysis(message, slotting_df):
@@ -244,16 +253,13 @@ def run_rebalancing_analysis(message, slotting_df):
244
  # ============================================================
245
 
246
  def run_workforce_optimization(message, slotting_df):
247
- """
248
- Estimate workforce needed based on SKU demand load.
249
- """
250
  reasoning = []
251
  df = slotting_df.copy()
252
 
253
  if "Frequency" not in df.columns:
254
  return "Cannot calculate workforce β€” missing Frequency column.", None
255
 
256
- df["Workload"] = df["Frequency"] * 1.2 # weight multiplier
257
  total_workload = df["Workload"].sum()
258
  workers_needed = max(1, int(total_workload // 150))
259
 
@@ -279,10 +285,6 @@ def run_workforce_optimization(message, slotting_df):
279
  # ============================================================
280
 
281
  def run_dock_scheduling(message, slotting_df):
282
- """
283
- Simple dock scheduling:
284
- - Assigns SKUs to docks based on demand priority
285
- """
286
  reasoning = []
287
  df = slotting_df.copy()
288
 
 
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  import io
5
+ from PIL import Image # ⭐ REQUIRED FOR IMAGE FIX
6
+
7
 
8
  # ============================================================
9
  # SLOTING OPTIMIZATION β€” PHASE 1
 
63
  df = df.sort_values("Distance").reset_index(drop=True)
64
  reasoning_steps.append("Calculated Manhattan distance & sorted sequence.")
65
 
66
+ # --- Generate plot ---
67
  plt.figure(figsize=(6, 6))
68
  plt.plot(df["x"], df["y"], marker="o", linestyle="-")
69
  plt.title("Optimized Picking Route")
 
72
 
73
  buffer = io.BytesIO()
74
  plt.savefig(buffer, format="png")
 
75
  plt.close()
76
+ buffer.seek(0)
77
+
78
+ # ⭐ Convert BytesIO β†’ PIL (Gradio requirement)
79
+ image = Image.open(buffer)
80
 
81
  reasoning_steps.append("Generated walking route visualization.")
82
 
 
86
  "#### πŸ” Reasoning\n" + "\n".join([f"- {r}" for r in reasoning_steps])
87
  )
88
 
89
+ return explanation, image
90
 
91
 
92
  # ============================================================
 
120
 
121
  reasoning_steps.append("Generated 7-day demand projection.")
122
 
123
+ # --- Plot forecast ---
124
  plt.figure(figsize=(6, 4))
125
  plt.plot(forecast_df["Day"], forecast_df["Forecasted_Demand"], marker="o")
126
  plt.title("7-Day Demand Forecast")
 
129
 
130
  buffer = io.BytesIO()
131
  plt.savefig(buffer, format="png")
 
132
  plt.close()
133
+ buffer.seek(0)
134
+
135
+ # ⭐ Convert BytesIO β†’ PIL
136
+ image = Image.open(buffer)
137
 
138
  explanation = (
139
  "### πŸ“ˆ Demand Forecasting\n"
 
141
  "#### πŸ” Reasoning\n" + "\n".join([f"- {r}" for r in reasoning_steps])
142
  )
143
 
144
+ return explanation, image, forecast_df
145
 
146
 
147
  # ============================================================
148
+ # REPLENISHMENT ANALYSIS β€” MODULE 2
149
  # ============================================================
150
 
151
  def run_replenishment_analysis(message, slotting_df):
 
253
  # ============================================================
254
 
255
  def run_workforce_optimization(message, slotting_df):
 
 
 
256
  reasoning = []
257
  df = slotting_df.copy()
258
 
259
  if "Frequency" not in df.columns:
260
  return "Cannot calculate workforce β€” missing Frequency column.", None
261
 
262
+ df["Workload"] = df["Frequency"] * 1.2
263
  total_workload = df["Workload"].sum()
264
  workers_needed = max(1, int(total_workload // 150))
265
 
 
285
  # ============================================================
286
 
287
  def run_dock_scheduling(message, slotting_df):
 
 
 
 
288
  reasoning = []
289
  df = slotting_df.copy()
290