AR1769 commited on
Commit
9c5a0d7
·
verified ·
1 Parent(s): deb842f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -29
app.py CHANGED
@@ -1,31 +1,74 @@
1
  import gradio as gr
 
 
2
 
3
- # Function to calculate load, breaker size, and cable size
4
- def calculate_load(load_rating, num_of_loads, voltage, load_type):
5
- # Convert total load to watts
6
- total_load_kw = load_rating * num_of_loads
7
- total_load_w = total_load_kw * 1000 # Convert kW to W
8
-
9
- # Calculate breaker size
10
- if load_type == "Resistive":
11
- breaker_size = total_load_w / (voltage * 0.8)
12
- elif load_type == "Inductive":
13
- breaker_size = total_load_w / (voltage * 0.9)
14
-
15
- # Calculate cable size (simplified estimation)
16
- cable_size = total_load_w / (voltage * 2)
17
-
18
- # Prepare the output
19
- result = f"Total Load: {total_load_kw} kW\nBreaker Size: {breaker_size:.2f} A\nCable Size: {cable_size:.2f} mm²"
20
- return result
21
-
22
- # Set up the Gradio interface
23
- interface = gr.Interface(fn=calculate_load,
24
- inputs=[gr.Number(label="Load Rating per Device (kW)"),
25
- gr.Number(label="Number of Loads"),
26
- gr.Number(label="Voltage (V)"),
27
- gr.Dropdown(choices=["Resistive", "Inductive"], label="Load Type")],
28
- outputs="text")
29
-
30
- if __name__ == "__main__":
31
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
 
5
+ # Updated Calculation Function
6
+ def load_calculator(load_rating, num_of_loads, voltage, power_factor, safety_factor, phase_type):
7
+ total_load = load_rating * num_of_loads # Total Load in kW
8
+ total_current = (total_load * 1000) / (voltage * power_factor) # Current in Amperes
9
+
10
+ if phase_type == "Three-Phase":
11
+ total_current /= (3 ** 0.5)
12
+
13
+ # Adjust breaker and cable sizes using safety factor
14
+ breaker_size = total_current * safety_factor
15
+ cable_size = total_current * safety_factor * 0.75 # Example adjustment for cable size
16
+
17
+ apparent_power = total_load / power_factor # Apparent Power in kVA
18
+
19
+ # Save results to DataFrame
20
+ results = pd.DataFrame({
21
+ "Metric": ["Total Load (kW)", "Total Current (A)", "Breaker Size (A)", "Cable Size (mm²)", "Apparent Power (kVA)"],
22
+ "Value": [total_load, total_current, breaker_size, cable_size, apparent_power],
23
+ })
24
+
25
+ # Generate bar chart
26
+ plt.figure(figsize=(6, 4))
27
+ plt.bar(results["Metric"], results["Value"], color="skyblue")
28
+ plt.title("Load Calculation Results")
29
+ plt.xticks(rotation=45, ha='right')
30
+ plt.tight_layout()
31
+ plt.savefig("results_chart.png")
32
+ plt.close()
33
+
34
+ # Save results to Excel
35
+ results.to_excel("load_results.xlsx", index=False)
36
+
37
+ return (
38
+ round(total_load, 2),
39
+ round(total_current, 2),
40
+ round(breaker_size, 2),
41
+ round(cable_size, 2),
42
+ round(apparent_power, 2),
43
+ "results_chart.png",
44
+ )
45
+
46
+ # Gradio Interface
47
+ def interface():
48
+ load_rating = gr.Number(label="Load Rating per Device (kW)")
49
+ num_of_loads = gr.Number(label="Number of Loads")
50
+ voltage = gr.Number(label="Voltage (230V for single-phase, 400V for three-phase)")
51
+ power_factor = gr.Number(label="Power Factor (0.8 for inductive loads, 1 for resistive)")
52
+ safety_factor = gr.Number(label="Safety Factor (e.g., 1.25)", value=1.25)
53
+ phase_type = gr.Radio(["Single-Phase", "Three-Phase"], label="Phase Type")
54
+
55
+ # Outputs
56
+ outputs = [
57
+ gr.Textbox(label="Total Load (kW)"),
58
+ gr.Textbox(label="Total Current (A)"),
59
+ gr.Textbox(label="Recommended Breaker Size (A)"),
60
+ gr.Textbox(label="Recommended Cable Size (mm²)"),
61
+ gr.Textbox(label="Total Apparent Power (kVA)"),
62
+ gr.Image(label="Graphical Visualization"), # Graph output
63
+ ]
64
+
65
+ # Launch Interface
66
+ gr.Interface(
67
+ fn=load_calculator,
68
+ inputs=[load_rating, num_of_loads, voltage, power_factor, safety_factor, phase_type],
69
+ outputs=outputs,
70
+ title="Enhanced Load Calculation Assistant",
71
+ description="Calculate electrical loads with safety factors, export results, and visualize data.",
72
+ ).launch()
73
+
74
+ interface()