Spaces:
Build error
Build error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556571 unmodified line596061626364652 unmodified lines686970717273747576777879808182838485import gradio as grimport pandas as pdimport matplotlib.pyplot as pltfrom io import BytesIO# Updated Calculation Functiondef load_calculator(load_rating, num_of_loads, voltage, power_factor, safety_factor, phase_type):try: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# Prepare resultsresults = 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()# Save the plot to a BytesIO objectbuf = BytesIO()plt.savefig(buf, format='png')buf.seek(0)plt.close()# Save Excel data to a BytesIO objectexcel_buf = BytesIO()results.to_excel(excel_buf, index=False)excel_buf.seek(0)return (round(total_load, 2),round(total_current, 2),round(breaker_size, 2),round(cable_size, 2),round(apparent_power, 2),buf,excel_buf,)except Exception as e:return f"An error occurred: {e}"# Gradio Interfacedef interface():1 unmodified linenum_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")# Outputs2 unmodified linesgr.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 outputgr.File(label="Download Excel Results"), # Excel file 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()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152531 unmodified line5556575859602 unmodified lines63646566676869707172737475767778import gradio as grimport pandas as pd# Updated Calculation Function (No Safety Factor)def load_calculator(load_rating, num_of_loads, voltage, power_factor, phase_type):try:# Check if any input is invalidif not all([load_rating, num_of_loads, voltage, power_factor]):raise ValueError("All input fields must be filled with valid numbers.")# Step 1: Calculate total load in kWtotal_load = load_rating * num_of_loads # Total Load in kWprint(f"Total Load (kW): {total_load}")# Step 2: Calculate current based on the type of phase (Single or Three Phase)total_load_watts = total_load * 1000 # Convert kW to Wattsprint(f"Total Load (W): {total_load_watts}")# Current formula for Single Phase: I = P / (V * PF)if phase_type == "Single-Phase":total_current = total_load_watts / (voltage * power_factor)print(f"Total Current (A - Single-Phase): {total_current}")else: # Three-Phase: I = P / (sqrt(3) * V * PF)total_current = total_load_watts / (3 ** 0.5 * voltage * power_factor)print(f"Total Current (A - Three-Phase): {total_current}")# Step 3: Calculate breaker size (no safety factor)breaker_size = total_current # Breaker size in Amperesprint(f"Breaker Size (A): {breaker_size}")# Step 4: Calculate cable size (assuming 0.75mm² per Ampere of current as an example)cable_size = total_current * 0.75 # Cable size in mm²print(f"Cable Size (mm²): {cable_size}")# Step 5: Calculate apparent power in kVAapparent_power = total_load / power_factor # Apparent Power in kVAprint(f"Apparent Power (kVA): {apparent_power}")# Prepare results for returning to Gradioreturn (str(round(total_load, 2)),str(round(total_current, 2)),str(round(breaker_size, 2)),str(round(cable_size, 2)),str(round(apparent_power, 2)))except Exception as e:# Return the error message if any exception occursreturn f"An error occurred: {str(e)}"# Gradio Interfacedef interface():1 unmodified linenum_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)")phase_type = gr.Radio(["Single-Phase", "Three-Phase"], label="Phase Type")# Outputs2 unmodified linesgr.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)")]# Launch Interfacegr.Interface(fn=load_calculator,inputs=[load_rating, num_of_loads, voltage, power_factor, phase_type],outputs=outputs,title="Load Calculation Assistant",description="Calculate electrical loads and get the results.").launch()interface()
app.py+40-47