GMARTINEZMILLA commited on
Commit
4246bc2
·
1 Parent(s): ca8d4de

bugfix: Manufacturers Alerts

Browse files
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -78,6 +78,9 @@ with st.sidebar:
78
  if page == "Customer Analysis":
79
  st.sidebar.title("Filter Options")
80
  show_all = st.sidebar.checkbox('Show All Manufacturers', value=True)
 
 
 
81
 
82
  # If not showing all, allow filtering by manufacturer code
83
  if not show_all:
@@ -298,6 +301,18 @@ elif page == "Customer Analysis":
298
  help="Start typing to search for a specific customer code"
299
  )
300
 
 
 
 
 
 
 
 
 
 
 
 
 
301
  if st.button("Calcular"):
302
  if customer_code:
303
  with st.spinner("We are identifying the customer's cluster..."):
@@ -321,13 +336,27 @@ elif page == "Customer Analysis":
321
  predict_data['cliente_id'] = predict_data['cliente_id'].astype(str)
322
 
323
  with st.spinner("Filtering data..."):
324
- # Filter for the specific customer
325
- customer_code_str = str(customer_code)
326
- customer_data = predict_data[predict_data['cliente_id'] == customer_code_str]
327
-
328
- # Apply filter for the selected manufacturer if not showing all manufacturers
329
  if selected_manufacturer:
330
- customer_data = customer_data[customer_data['marca_id_encoded'] == selected_manufacturer]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
 
332
  with st.spinner("Generating sales predictions..."):
333
  if not customer_data.empty:
@@ -351,7 +380,7 @@ elif page == "Customer Analysis":
351
  results['ventas_predichas'] = y_pred
352
 
353
  # Load actual data from df_agg_2024
354
- actual_sales = df_agg_2024[df_agg_2024['cliente_id'] == customer_code_str]
355
 
356
  if selected_manufacturer:
357
  actual_sales = actual_sales[actual_sales['marca_id_encoded'] == selected_manufacturer]
@@ -369,11 +398,11 @@ elif page == "Customer Analysis":
369
  # Ensure any missing sales data is filled with 0
370
  results['ventas_reales'].fillna(0, inplace=True)
371
 
372
- # Filtrar los datos históricos por cliente y fabricante, si se ha seleccionado uno
373
  filtered_historical_data = historical_data[
374
- (historical_data['cliente_id'] == customer_code_str) &
375
  (historical_data['marca_id_encoded'] == selected_manufacturer)
376
- ] if selected_manufacturer else historical_data[historical_data['cliente_id'] == customer_code_str]
377
 
378
  # Define the cutoff date for the last 12 months
379
  fecha_inicio = pd.to_datetime("2023-01-01")
 
78
  if page == "Customer Analysis":
79
  st.sidebar.title("Filter Options")
80
  show_all = st.sidebar.checkbox('Show All Manufacturers', value=True)
81
+
82
+ # Initialize selected_manufacturer
83
+ selected_manufacturer = None
84
 
85
  # If not showing all, allow filtering by manufacturer code
86
  if not show_all:
 
301
  help="Start typing to search for a specific customer code"
302
  )
303
 
304
+ # If show_all is checked, selected_manufacturer will be None
305
+ selected_manufacturer = None
306
+
307
+ # Add a checkbox for showing all manufacturers
308
+ show_all = st.sidebar.checkbox('Show All Manufacturers', value=True)
309
+ if not show_all:
310
+ # If not showing all manufacturers, allow user to select a specific manufacturer
311
+ selected_manufacturer = st.sidebar.selectbox(
312
+ 'Select Manufacturer Code',
313
+ historical_data['marca_id_encoded'].unique()
314
+ )
315
+
316
  if st.button("Calcular"):
317
  if customer_code:
318
  with st.spinner("We are identifying the customer's cluster..."):
 
336
  predict_data['cliente_id'] = predict_data['cliente_id'].astype(str)
337
 
338
  with st.spinner("Filtering data..."):
339
+ # If selected_manufacturer is not None, filter by manufacturer, otherwise use all
 
 
 
 
340
  if selected_manufacturer:
341
+ customer_data = predict_data[predict_data['marca_id_encoded'] == selected_manufacturer]
342
+ else:
343
+ # Sum all manufacturers if no specific manufacturer is selected
344
+ customer_data = predict_data.groupby(['cliente_id', 'fecha_mes']).agg({
345
+ 'precio_total': 'sum',
346
+ 'ventas_predichas': 'sum'
347
+ }).reset_index()
348
+
349
+ # Historical data filtering based on whether a manufacturer is selected or not
350
+ if selected_manufacturer:
351
+ filtered_historical_data = historical_data[
352
+ (historical_data['cliente_id'] == customer_code) &
353
+ (historical_data['marca_id_encoded'] == selected_manufacturer)
354
+ ]
355
+ else:
356
+ # Sum the historical data for all manufacturers if none is selected
357
+ filtered_historical_data = historical_data[
358
+ historical_data['cliente_id'] == customer_code
359
+ ].groupby('fecha_mes')['precio_total'].sum().reset_index()
360
 
361
  with st.spinner("Generating sales predictions..."):
362
  if not customer_data.empty:
 
380
  results['ventas_predichas'] = y_pred
381
 
382
  # Load actual data from df_agg_2024
383
+ actual_sales = df_agg_2024[df_agg_2024['cliente_id'] == customer_code]
384
 
385
  if selected_manufacturer:
386
  actual_sales = actual_sales[actual_sales['marca_id_encoded'] == selected_manufacturer]
 
398
  # Ensure any missing sales data is filled with 0
399
  results['ventas_reales'].fillna(0, inplace=True)
400
 
401
+ # Filter historical data by customer and manufacturer, if one is selected
402
  filtered_historical_data = historical_data[
403
+ (historical_data['cliente_id'] == customer_code) &
404
  (historical_data['marca_id_encoded'] == selected_manufacturer)
405
+ ] if selected_manufacturer else historical_data[historical_data['cliente_id'] == customer_code]
406
 
407
  # Define the cutoff date for the last 12 months
408
  fecha_inicio = pd.to_datetime("2023-01-01")