AdityaRatan commited on
Commit
ee9d69c
Β·
verified Β·
1 Parent(s): 3d6ef7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +192 -28
app.py CHANGED
@@ -492,9 +492,89 @@ def perform_sustainability_analysis(state):
492
  except Exception as e:
493
  return f"❌ Error in sustainability analysis: {str(e)}", None, "Analysis failed"
494
 
495
- def predict_freight_cost(state, weight=1000, line_item_value=10000, cost_per_kg=50,
496
- shipment_mode="Air", air_charter_weight=0, ocean_weight=0, truck_weight=0,
497
- air_charter_value=0, ocean_value=0, truck_value=0):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
498
  if state.freight_model is None:
499
  return "Error: Freight prediction model not loaded"
500
 
@@ -717,10 +797,13 @@ def run_analyses(state, choices, sales_file, supplier_file, text_data):
717
  return combined_results, final_figure, combined_status
718
 
719
  def predict_and_store_freight(state, *args):
720
- result = predict_freight_cost(state, *args)
721
- if isinstance(result, (int, float)):
722
- state.freight_predictions.append(result)
723
- return result
 
 
 
724
 
725
  def create_interface():
726
  state = SupplyChainState()
@@ -812,15 +895,15 @@ def create_interface():
812
 
813
  # Cost Prediction Tab
814
  with gr.Tab("πŸ’° Cost Prediction", elem_classes="tab-content"):
815
- with gr.Row():
816
- shipment_mode = gr.Dropdown(
817
- choices=["✈️ Air", "🚒 Ocean", "πŸš› Truck"],
818
- label="Transport Mode",
819
- value="✈️ Air"
820
- )
821
-
822
  with gr.Row():
823
  with gr.Column():
 
 
 
 
 
 
 
824
  weight = gr.Slider(
825
  label="πŸ“¦ Weight (kg)",
826
  minimum=1,
@@ -828,7 +911,6 @@ def create_interface():
828
  step=1,
829
  value=1000
830
  )
831
- with gr.Column():
832
  line_item_value = gr.Slider(
833
  label="πŸ’΅ Item Value (USD)",
834
  minimum=1,
@@ -836,16 +918,86 @@ def create_interface():
836
  step=1,
837
  value=10000
838
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
839
 
840
  predict_button = gr.Button(
841
- "πŸ” Calculate Cost",
842
  variant="primary",
843
  elem_classes="action-button"
844
  )
845
- freight_result = gr.Number(
846
- label="Predicted Cost (USD)",
847
- elem_classes="result-box"
848
- )
 
 
 
 
 
 
 
 
 
849
 
850
  # Chat Tab
851
  with gr.Tab("πŸ’¬ Chat", elem_classes="tab-content"):
@@ -902,15 +1054,27 @@ def create_interface():
902
  )
903
 
904
  predict_button.click(
905
- fn=lambda weight, value, mode: predict_and_store_freight(
906
- state,
907
- weight=weight,
908
- line_item_value=value,
909
- cost_per_kg=50, # Default value
910
- shipment_mode=mode
 
 
 
 
 
 
 
 
911
  ),
912
- inputs=[weight, line_item_value, shipment_mode],
913
- outputs=[freight_result]
 
 
 
 
914
  )
915
 
916
  chat_button.click(
 
492
  except Exception as e:
493
  return f"❌ Error in sustainability analysis: {str(e)}", None, "Analysis failed"
494
 
495
+ def calculate_shipping_cost(base_cost, params):
496
+ """Calculate total shipping cost with all factors"""
497
+ total_cost = base_cost
498
+
499
+ # Fuel surcharge
500
+ fuel_charge = base_cost * (params['fuel_surcharge'] / 100)
501
+
502
+ # Insurance
503
+ insurance = params['line_item_value'] * (params['insurance_rate'] / 100)
504
+
505
+ # Customs duty
506
+ duty = params['line_item_value'] * (params['customs_duty'] / 100)
507
+
508
+ # Special handling charges
509
+ handling_charges = 0
510
+ handling_rates = {
511
+ "Temperature Controlled": 0.15,
512
+ "Hazardous Materials": 0.25,
513
+ "Fragile Items": 0.10,
514
+ "Express Delivery": 0.20,
515
+ "Door-to-Door Service": 0.15
516
+ }
517
+
518
+ for requirement in params['special_handling']:
519
+ if requirement in handling_rates:
520
+ handling_charges += base_cost * handling_rates[requirement]
521
+
522
+ # Distance-based charge
523
+ distance_rate = {
524
+ "Air": 0.1,
525
+ "Ocean": 0.05,
526
+ "Truck": 0.15
527
+ }
528
+ distance_charge = params['distance'] * distance_rate[params['shipment_mode']]
529
+
530
+ # Time-based charge
531
+ transit_charge = params['transit_time'] * (base_cost * 0.01)
532
+
533
+ total_cost = base_cost + fuel_charge + insurance + duty + handling_charges + distance_charge + transit_charge
534
+
535
+ return {
536
+ 'base_cost': round(base_cost, 2),
537
+ 'fuel_charge': round(fuel_charge, 2),
538
+ 'insurance': round(insurance, 2),
539
+ 'customs_duty': round(duty, 2),
540
+ 'handling_charges': round(handling_charges, 2),
541
+ 'distance_charge': round(distance_charge, 2),
542
+ 'transit_charge': round(transit_charge, 2),
543
+ 'total_cost': round(total_cost, 2)
544
+ }
545
+
546
+ def predict_freight_cost(state, params):
547
+ """Predict freight cost with enhanced parameters"""
548
+ if state.freight_model is None:
549
+ return "Error: Freight prediction model not loaded"
550
+
551
+ try:
552
+ # Clean shipment mode string
553
+ mode = params['shipment_mode'].replace("✈️ ", "").replace("🚒 ", "").replace("πŸš› ", "")
554
+
555
+ # Prepare features for the model
556
+ features = {
557
+ 'weight (kilograms)': params['weight'],
558
+ 'line item value': params['line_item_value'],
559
+ 'cost per kilogram': params['cost_per_kg'],
560
+ 'shipment mode_Air Charter_weight': params['weight'] if mode == "Air" else 0,
561
+ 'shipment mode_Ocean_weight': params['weight'] if mode == "Ocean" else 0,
562
+ 'shipment mode_Truck_weight': params['weight'] if mode == "Truck" else 0,
563
+ 'shipment mode_Air Charter_line_item_value': params['line_item_value'] if mode == "Air" else 0,
564
+ 'shipment mode_Ocean_line_item_value': params['line_item_value'] if mode == "Ocean" else 0,
565
+ 'shipment mode_Truck_line_item_value': params['line_item_value'] if mode == "Truck" else 0
566
+ }
567
+
568
+ input_data = pd.DataFrame([features])
569
+ base_prediction = state.freight_model.predict(input_data)[0]
570
+
571
+ # Calculate total cost with all factors
572
+ cost_breakdown = calculate_shipping_cost(base_prediction, params)
573
+
574
+ return cost_breakdown
575
+
576
+ except Exception as e:
577
+ return f"Error making prediction: {str(e)}"
578
  if state.freight_model is None:
579
  return "Error: Freight prediction model not loaded"
580
 
 
797
  return combined_results, final_figure, combined_status
798
 
799
  def predict_and_store_freight(state, *args):
800
+ if len(args) >= 3:
801
+ weight, line_item_value, shipment_mode = args[:3]
802
+ result = predict_freight_cost(state, weight, line_item_value, 50, shipment_mode)
803
+ if isinstance(result, (int, float)):
804
+ state.freight_predictions.append(result)
805
+ return result
806
+ return "Error: Invalid parameters"
807
 
808
  def create_interface():
809
  state = SupplyChainState()
 
895
 
896
  # Cost Prediction Tab
897
  with gr.Tab("πŸ’° Cost Prediction", elem_classes="tab-content"):
 
 
 
 
 
 
 
898
  with gr.Row():
899
  with gr.Column():
900
+ shipment_mode = gr.Dropdown(
901
+ choices=["✈️ Air", "🚒 Ocean", "πŸš› Truck"],
902
+ label="Transport Mode",
903
+ value="✈️ Air"
904
+ )
905
+
906
+ # Basic Parameters
907
  weight = gr.Slider(
908
  label="πŸ“¦ Weight (kg)",
909
  minimum=1,
 
911
  step=1,
912
  value=1000
913
  )
 
914
  line_item_value = gr.Slider(
915
  label="πŸ’΅ Item Value (USD)",
916
  minimum=1,
 
918
  step=1,
919
  value=10000
920
  )
921
+ cost_per_kg = gr.Slider(
922
+ label="πŸ’² Base Cost per kg (USD)",
923
+ minimum=1,
924
+ maximum=500,
925
+ step=1,
926
+ value=50
927
+ )
928
+
929
+ # Advanced Parameters
930
+ gr.Markdown("### Advanced Parameters")
931
+ transit_time = gr.Slider(
932
+ label="πŸ•’ Transit Time (Days)",
933
+ minimum=1,
934
+ maximum=60,
935
+ step=1,
936
+ value=7
937
+ )
938
+ distance = gr.Slider(
939
+ label="πŸ“ Distance (km)",
940
+ minimum=100,
941
+ maximum=20000,
942
+ step=100,
943
+ value=1000
944
+ )
945
+ fuel_surcharge = gr.Slider(
946
+ label="β›½ Fuel Surcharge (%)",
947
+ minimum=0,
948
+ maximum=50,
949
+ step=0.5,
950
+ value=5
951
+ )
952
+
953
+ # Risk Factors
954
+ gr.Markdown("### Risk Factors")
955
+ insurance_rate = gr.Slider(
956
+ label="πŸ›‘οΈ Insurance Rate (%)",
957
+ minimum=0.1,
958
+ maximum=10,
959
+ step=0.1,
960
+ value=1
961
+ )
962
+ customs_duty = gr.Slider(
963
+ label="πŸ›οΈ Customs Duty (%)",
964
+ minimum=0,
965
+ maximum=40,
966
+ step=0.5,
967
+ value=5
968
+ )
969
+
970
+ # Special Handling
971
+ gr.Markdown("### Special Handling")
972
+ special_handling = gr.CheckboxGroup(
973
+ choices=[
974
+ "Temperature Controlled",
975
+ "Hazardous Materials",
976
+ "Fragile Items",
977
+ "Express Delivery",
978
+ "Door-to-Door Service"
979
+ ],
980
+ label="Special Requirements"
981
+ )
982
 
983
  predict_button = gr.Button(
984
+ "πŸ” Calculate Total Cost",
985
  variant="primary",
986
  elem_classes="action-button"
987
  )
988
+ with gr.Row():
989
+ freight_result = gr.Number(
990
+ label="Base Freight Cost (USD)",
991
+ elem_classes="result-box"
992
+ )
993
+ total_cost = gr.Number(
994
+ label="Total Cost Including All Charges (USD)",
995
+ elem_classes="result-box"
996
+ )
997
+ cost_breakdown = gr.JSON(
998
+ label="Cost Breakdown",
999
+ elem_classes="result-box"
1000
+ )
1001
 
1002
  # Chat Tab
1003
  with gr.Tab("πŸ’¬ Chat", elem_classes="tab-content"):
 
1054
  )
1055
 
1056
  predict_button.click(
1057
+ fn=lambda mode, w, val, cost, time, dist, fuel, ins, duty, special: predict_and_store_freight(
1058
+ state,
1059
+ {
1060
+ 'shipment_mode': mode,
1061
+ 'weight': w,
1062
+ 'line_item_value': val,
1063
+ 'cost_per_kg': cost,
1064
+ 'transit_time': time,
1065
+ 'distance': dist,
1066
+ 'fuel_surcharge': fuel,
1067
+ 'insurance_rate': ins,
1068
+ 'customs_duty': duty,
1069
+ 'special_handling': special
1070
+ }
1071
  ),
1072
+ inputs=[
1073
+ shipment_mode, weight, line_item_value, cost_per_kg,
1074
+ transit_time, distance, fuel_surcharge,
1075
+ insurance_rate, customs_duty, special_handling
1076
+ ],
1077
+ outputs=[freight_result, total_cost, cost_breakdown]
1078
  )
1079
 
1080
  chat_button.click(