import gradio as gr import pandas as pd import joblib # Load model best_model = joblib.load("xgb_model.pkl") # Define function def simulate( loaded_drv, empty_drv, eng_speed, empty_stop, loading_stop, loaded_stop, unit_count, cycle_per_day, unit_payload ): # Prediction input_data = { 'loaded_drv_time_percycle': loaded_drv, 'empty_drv_time_percycle': empty_drv, 'Eng_Speed_Ave': eng_speed, 'empty_stop_time_percycle': empty_stop, 'loadingstoptime_percycle': loading_stop, 'loaded_stop_time_percycle': loaded_stop } input_df = pd.DataFrame([input_data]) fuel_rate = best_model.predict(input_df)[0] # Calculations production_per_day = unit_count * cycle_per_day * unit_payload fuel_per_day = unit_count * cycle_per_day * fuel_rate fuel_per_month = 30 * fuel_per_day return ( f"{fuel_rate:,.2f}", f"{production_per_day:,.2f}", f"{fuel_per_day:,.2f}", f"{fuel_per_month:,.2f}" ) # Interface # Use gr.Blocks for custom layout with gr.Blocks() as interface: gr.Markdown("## 🚛 Fuel - Digital Twin (What-If Simulator)") gr.Markdown("Adjust inputs to simulate operating conditions and predict production & fuel usage.") # Top 3-column row for business inputs with gr.Row(): with gr.Column(): unit_count = gr.Number(value=5, label="Unit Count") with gr.Column(): cycle_per_day = gr.Number(value=60, label="Cycle Per Day") with gr.Column(): unit_payload = gr.Number(value=90, label="Unit Payload (Ton)") # Main 2-column layout: left = input sliders, right = output with gr.Row(): with gr.Column(): # Left side = input sliders (one column) loaded_drv = gr.Slider(3, 60, value=18, label="Loaded Drive Time per Cycle") empty_drv = gr.Slider(2, 51, value=16, label="Empty Drive Time per Cycle") eng_speed = gr.Slider(1051, 1596, value=1416, label="Engine Speed Average") empty_stop = gr.Slider(0.2, 24.6, value=4.2, label="Empty Stop Time per Cycle") loading_stop = gr.Slider(2, 18, value=2, label="Loading Stop Time per Cycle") loaded_stop = gr.Slider(0.4, 9, value=0.4, label="Loaded Stop Time per Cycle") submit_btn = gr.Button("Submit") with gr.Column(): # Right side = output results output1 = gr.Textbox(label="🚛 Predicted Fuel Rate per Cycle (L)") output2 = gr.Textbox(label="📦 Production Per Day (Ton)") output3 = gr.Textbox(label="⛽ Fuel Consumption Per Day (L)") output4 = gr.Textbox(label="⛽ Fuel Consumption Per Month (L)") submit_btn.click( simulate, inputs=[ loaded_drv, empty_drv, eng_speed, empty_stop, loading_stop, loaded_stop, unit_count, cycle_per_day, unit_payload ], outputs=[output1, output2, output3, output4] ) interface.launch()