Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,74 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
#
|
4 |
-
def
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|