Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,57 @@
|
|
| 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 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"Metric"
|
| 22 |
-
"
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
plt.xticks(rotation=45, ha='right')
|
| 30 |
-
plt.tight_layout()
|
| 31 |
-
plt.savefig("results_chart.png")
|
| 32 |
-
plt.close()
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
# Gradio Interface
|
| 47 |
def interface():
|
|
@@ -60,6 +70,7 @@ def interface():
|
|
| 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
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
+
from io import BytesIO
|
| 5 |
|
| 6 |
# Updated Calculation Function
|
| 7 |
def load_calculator(load_rating, num_of_loads, voltage, power_factor, safety_factor, phase_type):
|
| 8 |
+
try:
|
| 9 |
+
total_load = load_rating * num_of_loads # Total Load in kW
|
| 10 |
+
total_current = (total_load * 1000) / (voltage * power_factor) # Current in Amperes
|
| 11 |
|
| 12 |
+
if phase_type == "Three-Phase":
|
| 13 |
+
total_current /= (3 ** 0.5)
|
| 14 |
|
| 15 |
+
# Adjust breaker and cable sizes using safety factor
|
| 16 |
+
breaker_size = total_current * safety_factor
|
| 17 |
+
cable_size = total_current * safety_factor * 0.75 # Example adjustment for cable size
|
| 18 |
+
apparent_power = total_load / power_factor # Apparent Power in kVA
|
| 19 |
|
| 20 |
+
# Prepare results
|
| 21 |
+
results = pd.DataFrame({
|
| 22 |
+
"Metric": ["Total Load (kW)", "Total Current (A)", "Breaker Size (A)", "Cable Size (mm²)", "Apparent Power (kVA)"],
|
| 23 |
+
"Value": [total_load, total_current, breaker_size, cable_size, apparent_power],
|
| 24 |
+
})
|
| 25 |
|
| 26 |
+
# Generate bar chart
|
| 27 |
+
plt.figure(figsize=(6, 4))
|
| 28 |
+
plt.bar(results["Metric"], results["Value"], color="skyblue")
|
| 29 |
+
plt.title("Load Calculation Results")
|
| 30 |
+
plt.xticks(rotation=45, ha='right')
|
| 31 |
+
plt.tight_layout()
|
| 32 |
+
|
| 33 |
+
# Save the plot to a BytesIO object
|
| 34 |
+
buf = BytesIO()
|
| 35 |
+
plt.savefig(buf, format='png')
|
| 36 |
+
buf.seek(0)
|
| 37 |
+
plt.close()
|
| 38 |
|
| 39 |
+
# Save Excel data to a BytesIO object
|
| 40 |
+
excel_buf = BytesIO()
|
| 41 |
+
results.to_excel(excel_buf, index=False)
|
| 42 |
+
excel_buf.seek(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
return (
|
| 45 |
+
round(total_load, 2),
|
| 46 |
+
round(total_current, 2),
|
| 47 |
+
round(breaker_size, 2),
|
| 48 |
+
round(cable_size, 2),
|
| 49 |
+
round(apparent_power, 2),
|
| 50 |
+
buf,
|
| 51 |
+
excel_buf,
|
| 52 |
+
)
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"An error occurred: {e}"
|
| 55 |
|
| 56 |
# Gradio Interface
|
| 57 |
def interface():
|
|
|
|
| 70 |
gr.Textbox(label="Recommended Cable Size (mm²)"),
|
| 71 |
gr.Textbox(label="Total Apparent Power (kVA)"),
|
| 72 |
gr.Image(label="Graphical Visualization"), # Graph output
|
| 73 |
+
gr.File(label="Download Excel Results"), # Excel file output
|
| 74 |
]
|
| 75 |
|
| 76 |
# Launch Interface
|