AR1769 commited on
Commit
867928f
·
verified ·
1 Parent(s): a5ad080

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -47
app.py CHANGED
@@ -1,57 +1,53 @@
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():
@@ -59,7 +55,6 @@ def interface():
59
  num_of_loads = gr.Number(label="Number of Loads")
60
  voltage = gr.Number(label="Voltage (230V for single-phase, 400V for three-phase)")
61
  power_factor = gr.Number(label="Power Factor (0.8 for inductive loads, 1 for resistive)")
62
- safety_factor = gr.Number(label="Safety Factor (e.g., 1.25)", value=1.25)
63
  phase_type = gr.Radio(["Single-Phase", "Three-Phase"], label="Phase Type")
64
 
65
  # Outputs
@@ -68,18 +63,16 @@ def interface():
68
  gr.Textbox(label="Total Current (A)"),
69
  gr.Textbox(label="Recommended Breaker Size (A)"),
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
77
  gr.Interface(
78
  fn=load_calculator,
79
- inputs=[load_rating, num_of_loads, voltage, power_factor, safety_factor, phase_type],
80
  outputs=outputs,
81
- title="Enhanced Load Calculation Assistant",
82
- description="Calculate electrical loads with safety factors, export results, and visualize data.",
83
  ).launch()
84
 
85
  interface()
 
1
  import gradio as gr
2
  import pandas as pd
 
 
3
 
4
+ # Updated Calculation Function (No Safety Factor)
5
+ def load_calculator(load_rating, num_of_loads, voltage, power_factor, phase_type):
6
  try:
7
+ # Check if any input is invalid
8
+ if not all([load_rating, num_of_loads, voltage, power_factor]):
9
+ raise ValueError("All input fields must be filled with valid numbers.")
10
+
11
+ # Step 1: Calculate total load in kW
12
  total_load = load_rating * num_of_loads # Total Load in kW
13
+ print(f"Total Load (kW): {total_load}")
14
 
15
+ # Step 2: Calculate current based on the type of phase (Single or Three Phase)
16
+ total_load_watts = total_load * 1000 # Convert kW to Watts
17
+ print(f"Total Load (W): {total_load_watts}")
18
 
19
+ # Current formula for Single Phase: I = P / (V * PF)
20
+ if phase_type == "Single-Phase":
21
+ total_current = total_load_watts / (voltage * power_factor)
22
+ print(f"Total Current (A - Single-Phase): {total_current}")
23
+ else: # Three-Phase: I = P / (sqrt(3) * V * PF)
24
+ total_current = total_load_watts / (3 ** 0.5 * voltage * power_factor)
25
+ print(f"Total Current (A - Three-Phase): {total_current}")
26
 
27
+ # Step 3: Calculate breaker size (no safety factor)
28
+ breaker_size = total_current # Breaker size in Amperes
29
+ print(f"Breaker Size (A): {breaker_size}")
 
 
30
 
31
+ # Step 4: Calculate cable size (assuming 0.75mm² per Ampere of current as an example)
32
+ cable_size = total_current * 0.75 # Cable size in mm²
33
+ print(f"Cable Size (mm²): {cable_size}")
 
 
 
 
 
 
 
 
 
34
 
35
+ # Step 5: Calculate apparent power in kVA
36
+ apparent_power = total_load / power_factor # Apparent Power in kVA
37
+ print(f"Apparent Power (kVA): {apparent_power}")
 
38
 
39
+ # Prepare results for returning to Gradio
40
  return (
41
+ str(round(total_load, 2)),
42
+ str(round(total_current, 2)),
43
+ str(round(breaker_size, 2)),
44
+ str(round(cable_size, 2)),
45
+ str(round(apparent_power, 2))
 
 
46
  )
47
+
48
  except Exception as e:
49
+ # Return the error message if any exception occurs
50
+ return f"An error occurred: {str(e)}"
51
 
52
  # Gradio Interface
53
  def interface():
 
55
  num_of_loads = gr.Number(label="Number of Loads")
56
  voltage = gr.Number(label="Voltage (230V for single-phase, 400V for three-phase)")
57
  power_factor = gr.Number(label="Power Factor (0.8 for inductive loads, 1 for resistive)")
 
58
  phase_type = gr.Radio(["Single-Phase", "Three-Phase"], label="Phase Type")
59
 
60
  # Outputs
 
63
  gr.Textbox(label="Total Current (A)"),
64
  gr.Textbox(label="Recommended Breaker Size (A)"),
65
  gr.Textbox(label="Recommended Cable Size (mm²)"),
66
+ gr.Textbox(label="Total Apparent Power (kVA)")
 
 
67
  ]
68
 
69
  # Launch Interface
70
  gr.Interface(
71
  fn=load_calculator,
72
+ inputs=[load_rating, num_of_loads, voltage, power_factor, phase_type],
73
  outputs=outputs,
74
+ title="Load Calculation Assistant",
75
+ description="Calculate electrical loads and get the results."
76
  ).launch()
77
 
78
  interface()