Spaces:
Build error
Build error
Update app.py
Browse files12345678910111213141516171819202122232425262728293031import gradio as gr# Function to calculate load, breaker size, and cable sizedef calculate_load(load_rating, num_of_loads, voltage, load_type):# Convert total load to wattstotal_load_kw = load_rating * num_of_loadstotal_load_w = total_load_kw * 1000 # Convert kW to W# Calculate breaker sizeif load_type == "Resistive":breaker_size = total_load_w / (voltage * 0.8)elif load_type == "Inductive":breaker_size = total_load_w / (voltage * 0.9)# Calculate cable size (simplified estimation)cable_size = total_load_w / (voltage * 2)# Prepare the outputresult = f"Total Load: {total_load_kw} kW\nBreaker Size: {breaker_size:.2f} A\nCable Size: {cable_size:.2f} mm²"return result# Set up the Gradio interfaceinterface = gr.Interface(fn=calculate_load,inputs=[gr.Number(label="Load Rating per Device (kW)"),gr.Number(label="Number of Loads"),gr.Number(label="Voltage (V)"),gr.Dropdown(choices=["Resistive", "Inductive"], label="Load Type")],outputs="text")if __name__ == "__main__":interface.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374import gradio as grimport pandas as pdimport matplotlib.pyplot as plt# Updated Calculation Functiondef load_calculator(load_rating, num_of_loads, voltage, power_factor, safety_factor, phase_type):total_load = load_rating * num_of_loads # Total Load in kWtotal_current = (total_load * 1000) / (voltage * power_factor) # Current in Amperesif phase_type == "Three-Phase":total_current /= (3 ** 0.5)# Adjust breaker and cable sizes using safety factorbreaker_size = total_current * safety_factorcable_size = total_current * safety_factor * 0.75 # Example adjustment for cable sizeapparent_power = total_load / power_factor # Apparent Power in kVA# Save results to DataFrameresults = pd.DataFrame({"Metric": ["Total Load (kW)", "Total Current (A)", "Breaker Size (A)", "Cable Size (mm²)", "Apparent Power (kVA)"],"Value": [total_load, total_current, breaker_size, cable_size, apparent_power],})# Generate bar chartplt.figure(figsize=(6, 4))plt.bar(results["Metric"], results["Value"], color="skyblue")plt.title("Load Calculation Results")plt.xticks(rotation=45, ha='right')plt.tight_layout()plt.savefig("results_chart.png")plt.close()# Save results to Excelresults.to_excel("load_results.xlsx", index=False)return (round(total_load, 2),round(total_current, 2),round(breaker_size, 2),round(cable_size, 2),round(apparent_power, 2),"results_chart.png",)# Gradio Interfacedef interface():load_rating = gr.Number(label="Load Rating per Device (kW)")num_of_loads = gr.Number(label="Number of Loads")voltage = gr.Number(label="Voltage (230V for single-phase, 400V for three-phase)")power_factor = gr.Number(label="Power Factor (0.8 for inductive loads, 1 for resistive)")safety_factor = gr.Number(label="Safety Factor (e.g., 1.25)", value=1.25)phase_type = gr.Radio(["Single-Phase", "Three-Phase"], label="Phase Type")# Outputsoutputs = [gr.Textbox(label="Total Load (kW)"),gr.Textbox(label="Total Current (A)"),gr.Textbox(label="Recommended Breaker Size (A)"),gr.Textbox(label="Recommended Cable Size (mm²)"),gr.Textbox(label="Total Apparent Power (kVA)"),gr.Image(label="Graphical Visualization"), # Graph output]# Launch Interfacegr.Interface(fn=load_calculator,inputs=[load_rating, num_of_loads, voltage, power_factor, safety_factor, phase_type],outputs=outputs,title="Enhanced Load Calculation Assistant",description="Calculate electrical loads with safety factors, export results, and visualize data.",).launch()interface()
app.py+72-29