Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -492,12 +492,44 @@ 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, line_item_value, cost_per_kg,
|
| 496 |
-
shipment_mode, 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
try:
|
| 502 |
features = {
|
| 503 |
'weight (kilograms)': weight,
|
|
@@ -870,7 +902,13 @@ def create_interface():
|
|
| 870 |
)
|
| 871 |
|
| 872 |
predict_button.click(
|
| 873 |
-
fn=lambda
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 874 |
inputs=[weight, line_item_value, shipment_mode],
|
| 875 |
outputs=[freight_result]
|
| 876 |
)
|
|
|
|
| 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 |
|
| 501 |
+
try:
|
| 502 |
+
# Set weights based on mode
|
| 503 |
+
if "Air" in shipment_mode:
|
| 504 |
+
air_charter_weight = weight
|
| 505 |
+
air_charter_value = line_item_value
|
| 506 |
+
elif "Ocean" in shipment_mode:
|
| 507 |
+
ocean_weight = weight
|
| 508 |
+
ocean_value = line_item_value
|
| 509 |
+
else:
|
| 510 |
+
truck_weight = weight
|
| 511 |
+
truck_value = line_item_value
|
| 512 |
+
|
| 513 |
+
features = {
|
| 514 |
+
'weight (kilograms)': weight,
|
| 515 |
+
'line item value': line_item_value,
|
| 516 |
+
'cost per kilogram': cost_per_kg,
|
| 517 |
+
'shipment mode_Air Charter_weight': air_charter_weight,
|
| 518 |
+
'shipment mode_Ocean_weight': ocean_weight,
|
| 519 |
+
'shipment mode_Truck_weight': truck_weight,
|
| 520 |
+
'shipment mode_Air Charter_line_item_value': air_charter_value,
|
| 521 |
+
'shipment mode_Ocean_line_item_value': ocean_value,
|
| 522 |
+
'shipment mode_Truck_line_item_value': truck_value
|
| 523 |
+
}
|
| 524 |
+
input_data = pd.DataFrame([features])
|
| 525 |
+
|
| 526 |
+
prediction = state.freight_model.predict(input_data)
|
| 527 |
+
return round(float(prediction[0]), 2)
|
| 528 |
+
except Exception as e:
|
| 529 |
+
return f"Error making prediction: {str(e)}"
|
| 530 |
+
if state.freight_model is None:
|
| 531 |
+
return "Error: Freight prediction model not loaded"
|
| 532 |
+
|
| 533 |
try:
|
| 534 |
features = {
|
| 535 |
'weight (kilograms)': weight,
|
|
|
|
| 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 |
)
|