Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,17 @@ import math
|
|
| 3 |
import itertools
|
| 4 |
import ezdxf
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
from ezdxf import zoom
|
| 8 |
from ezdxf.enums import TextEntityAlignment
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
client =
|
| 12 |
|
|
|
|
| 13 |
available_capacitors = [25, 20, 15, 10, 5, 2.5, 1.5, 1]
|
| 14 |
|
|
|
|
| 15 |
def ask_groq(prompt):
|
| 16 |
try:
|
| 17 |
response = client.chat.completions.create(
|
|
@@ -37,13 +39,15 @@ def calculate_power_parameters(voltage, current, power_factor):
|
|
| 37 |
"reactive_power": round(reactive_power, 2),
|
| 38 |
"calculated_pf": round(calculated_pf, 2)
|
| 39 |
}
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
def design_capacitor_bank(reactive_power, num_caps):
|
| 43 |
if reactive_power > 0 and num_caps > 0:
|
| 44 |
best_combo = None
|
| 45 |
min_error = float('inf')
|
| 46 |
|
|
|
|
| 47 |
combos = itertools.combinations_with_replacement(available_capacitors, num_caps)
|
| 48 |
for combo in combos:
|
| 49 |
total = sum(combo)
|
|
@@ -64,37 +68,47 @@ def design_capacitor_bank(reactive_power, num_caps):
|
|
| 64 |
"message": message,
|
| 65 |
"combo": best_combo
|
| 66 |
}
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
def create_dxf_capacitor_bank(capacitors):
|
| 70 |
doc = ezdxf.new()
|
| 71 |
msp = doc.modelspace()
|
| 72 |
x = 0
|
| 73 |
y = 0
|
| 74 |
-
row_width = 15
|
| 75 |
row_height = 20
|
| 76 |
max_in_row = 5
|
| 77 |
|
| 78 |
for idx, cap in enumerate(capacitors):
|
| 79 |
label = f"{cap} kVAR"
|
|
|
|
| 80 |
points = [(x, y), (x + 10, y), (x + 10, y + 10), (x, y + 10), (x, y)]
|
| 81 |
-
msp.add_lwpolyline(points, close=True, dxfattribs={'color': 3})
|
|
|
|
|
|
|
| 82 |
text = msp.add_text(label, dxfattribs={
|
| 83 |
'height': 2.5,
|
| 84 |
-
'color': 4,
|
| 85 |
-
'style': 'STANDARD',
|
| 86 |
-
'halign': TextEntityAlignment.CENTER,
|
| 87 |
-
'valign': TextEntityAlignment.BOTTOM,
|
| 88 |
})
|
| 89 |
-
text.dxf.insert = (x + 5, y + 5)
|
|
|
|
| 90 |
x += row_width
|
| 91 |
-
if (idx + 1) % max_in_row == 0:
|
| 92 |
x = 0
|
| 93 |
y += row_height
|
| 94 |
|
|
|
|
| 95 |
title_text = msp.add_text("Capacitor Bank Layout", dxfattribs={'height': 5, 'color': 1})
|
| 96 |
title_text.dxf.insert = (0, y + 30)
|
| 97 |
-
|
|
|
|
|
|
|
| 98 |
|
| 99 |
output_path = "capacitor_bank_layout.dxf"
|
| 100 |
doc.saveas(output_path)
|
|
@@ -103,14 +117,19 @@ def create_dxf_capacitor_bank(capacitors):
|
|
| 103 |
def reactive_power_first(voltage, current, power_factor):
|
| 104 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 105 |
if power_results:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
return (
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
power_results['reactive_power']
|
| 112 |
)
|
| 113 |
-
|
|
|
|
| 114 |
|
| 115 |
def finalize_capacitor_bank(reactive_power, num_caps):
|
| 116 |
cap_bank_design = design_capacitor_bank(reactive_power, num_caps)
|
|
@@ -120,7 +139,8 @@ def finalize_capacitor_bank(reactive_power, num_caps):
|
|
| 120 |
)
|
| 121 |
dxf_path = create_dxf_capacitor_bank(cap_bank_design["combo"])
|
| 122 |
return suggested_capacitors_text, cap_bank_design['message'], dxf_path
|
| 123 |
-
|
|
|
|
| 124 |
|
| 125 |
with gr.Blocks() as iface:
|
| 126 |
gr.Markdown("# β‘ Three-Phase Power Calculator - Reactive Power Compensation")
|
|
@@ -137,6 +157,7 @@ with gr.Blocks() as iface:
|
|
| 137 |
frequency = gr.Radio(label="Select Frequency", choices=[50, 60], value=50)
|
| 138 |
|
| 139 |
calc_btn = gr.Button("π Calculate Power Parameters")
|
|
|
|
| 140 |
apparent_power_out = gr.HTML()
|
| 141 |
real_power_out = gr.HTML()
|
| 142 |
reactive_power_out = gr.HTML()
|
|
@@ -146,12 +167,19 @@ with gr.Blocks() as iface:
|
|
| 146 |
calc_btn.click(
|
| 147 |
fn=reactive_power_first,
|
| 148 |
inputs=[voltage, current, power_factor],
|
| 149 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
)
|
| 151 |
|
| 152 |
gr.Markdown("### β Enter number of capacitors to compensate reactive power:")
|
| 153 |
num_caps_input = gr.Number(label="Number of Capacitors", precision=0)
|
| 154 |
finalize_btn = gr.Button("βοΈ Generate Capacitor Bank")
|
|
|
|
| 155 |
capacitor_out = gr.HTML()
|
| 156 |
total_comp_out = gr.HTML()
|
| 157 |
dxf_file = gr.File(label="π₯ Download AutoCAD File")
|
|
|
|
| 3 |
import itertools
|
| 4 |
import ezdxf
|
| 5 |
import os
|
| 6 |
+
import groq
|
| 7 |
from ezdxf import zoom
|
| 8 |
from ezdxf.enums import TextEntityAlignment
|
| 9 |
|
| 10 |
+
# Initialize Groq client
|
| 11 |
+
client = groq.Client(api_key=os.getenv("GROQ_API_KEY"))
|
| 12 |
|
| 13 |
+
# Available Capacitor Units (kVAR)
|
| 14 |
available_capacitors = [25, 20, 15, 10, 5, 2.5, 1.5, 1]
|
| 15 |
|
| 16 |
+
# Prompt Groq for explanation (optional)
|
| 17 |
def ask_groq(prompt):
|
| 18 |
try:
|
| 19 |
response = client.chat.completions.create(
|
|
|
|
| 39 |
"reactive_power": round(reactive_power, 2),
|
| 40 |
"calculated_pf": round(calculated_pf, 2)
|
| 41 |
}
|
| 42 |
+
else:
|
| 43 |
+
return None
|
| 44 |
|
| 45 |
def design_capacitor_bank(reactive_power, num_caps):
|
| 46 |
if reactive_power > 0 and num_caps > 0:
|
| 47 |
best_combo = None
|
| 48 |
min_error = float('inf')
|
| 49 |
|
| 50 |
+
# Allow repetition freely to match reactive power
|
| 51 |
combos = itertools.combinations_with_replacement(available_capacitors, num_caps)
|
| 52 |
for combo in combos:
|
| 53 |
total = sum(combo)
|
|
|
|
| 68 |
"message": message,
|
| 69 |
"combo": best_combo
|
| 70 |
}
|
| 71 |
+
else:
|
| 72 |
+
return {"message": "Could not find a suitable combination."}
|
| 73 |
+
else:
|
| 74 |
+
return None
|
| 75 |
|
| 76 |
def create_dxf_capacitor_bank(capacitors):
|
| 77 |
doc = ezdxf.new()
|
| 78 |
msp = doc.modelspace()
|
| 79 |
x = 0
|
| 80 |
y = 0
|
| 81 |
+
row_width = 15 # Distance between capacitors in a row
|
| 82 |
row_height = 20
|
| 83 |
max_in_row = 5
|
| 84 |
|
| 85 |
for idx, cap in enumerate(capacitors):
|
| 86 |
label = f"{cap} kVAR"
|
| 87 |
+
# Draw rectangle for capacitor
|
| 88 |
points = [(x, y), (x + 10, y), (x + 10, y + 10), (x, y + 10), (x, y)]
|
| 89 |
+
msp.add_lwpolyline(points, close=True, dxfattribs={'color': 3}) # Color 3 = Green
|
| 90 |
+
|
| 91 |
+
# Add Text with more control
|
| 92 |
text = msp.add_text(label, dxfattribs={
|
| 93 |
'height': 2.5,
|
| 94 |
+
'color': 4, # color Cyan
|
| 95 |
+
'style': 'STANDARD', # You can define text styles in DXF
|
| 96 |
+
'halign': TextEntityAlignment.CENTER, # Horizontal alignment
|
| 97 |
+
'valign': TextEntityAlignment.BOTTOM if hasattr(TextEntityAlignment, 'BOTTOM') else 2,
|
| 98 |
})
|
| 99 |
+
text.dxf.insert = (x + 5, y + 5) # Position at center of rectangle
|
| 100 |
+
|
| 101 |
x += row_width
|
| 102 |
+
if (idx + 1) % max_in_row == 0: # Move to the next row
|
| 103 |
x = 0
|
| 104 |
y += row_height
|
| 105 |
|
| 106 |
+
# Add a title
|
| 107 |
title_text = msp.add_text("Capacitor Bank Layout", dxfattribs={'height': 5, 'color': 1})
|
| 108 |
title_text.dxf.insert = (0, y + 30)
|
| 109 |
+
|
| 110 |
+
# Zoom to extents
|
| 111 |
+
zoom.extents(msp, factor=1.1) # Add a small padding
|
| 112 |
|
| 113 |
output_path = "capacitor_bank_layout.dxf"
|
| 114 |
doc.saveas(output_path)
|
|
|
|
| 117 |
def reactive_power_first(voltage, current, power_factor):
|
| 118 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 119 |
if power_results:
|
| 120 |
+
apparent_power_out = f"Apparent Power: **{power_results['apparent_power']} VA**"
|
| 121 |
+
real_power_out = f"Real Power: **{power_results['real_power']} kW**"
|
| 122 |
+
reactive_power_out = f"Reactive Power: **{power_results['reactive_power']} kVAR**"
|
| 123 |
+
calculated_pf_out = f"Calculated Power Factor: **{power_results['calculated_pf']}**"
|
| 124 |
return (
|
| 125 |
+
apparent_power_out,
|
| 126 |
+
real_power_out,
|
| 127 |
+
reactive_power_out,
|
| 128 |
+
calculated_pf_out,
|
| 129 |
power_results['reactive_power']
|
| 130 |
)
|
| 131 |
+
else:
|
| 132 |
+
return ("β οΈ Please enter valid Voltage and Current!", "", "", "", 0)
|
| 133 |
|
| 134 |
def finalize_capacitor_bank(reactive_power, num_caps):
|
| 135 |
cap_bank_design = design_capacitor_bank(reactive_power, num_caps)
|
|
|
|
| 139 |
)
|
| 140 |
dxf_path = create_dxf_capacitor_bank(cap_bank_design["combo"])
|
| 141 |
return suggested_capacitors_text, cap_bank_design['message'], dxf_path
|
| 142 |
+
else:
|
| 143 |
+
return "Could not find a suitable combination.", "", None
|
| 144 |
|
| 145 |
with gr.Blocks() as iface:
|
| 146 |
gr.Markdown("# β‘ Three-Phase Power Calculator - Reactive Power Compensation")
|
|
|
|
| 157 |
frequency = gr.Radio(label="Select Frequency", choices=[50, 60], value=50)
|
| 158 |
|
| 159 |
calc_btn = gr.Button("π Calculate Power Parameters")
|
| 160 |
+
|
| 161 |
apparent_power_out = gr.HTML()
|
| 162 |
real_power_out = gr.HTML()
|
| 163 |
reactive_power_out = gr.HTML()
|
|
|
|
| 167 |
calc_btn.click(
|
| 168 |
fn=reactive_power_first,
|
| 169 |
inputs=[voltage, current, power_factor],
|
| 170 |
+
outputs=[
|
| 171 |
+
apparent_power_out,
|
| 172 |
+
real_power_out,
|
| 173 |
+
reactive_power_out,
|
| 174 |
+
calculated_pf_out,
|
| 175 |
+
reactive_value
|
| 176 |
+
]
|
| 177 |
)
|
| 178 |
|
| 179 |
gr.Markdown("### β Enter number of capacitors to compensate reactive power:")
|
| 180 |
num_caps_input = gr.Number(label="Number of Capacitors", precision=0)
|
| 181 |
finalize_btn = gr.Button("βοΈ Generate Capacitor Bank")
|
| 182 |
+
|
| 183 |
capacitor_out = gr.HTML()
|
| 184 |
total_comp_out = gr.HTML()
|
| 185 |
dxf_file = gr.File(label="π₯ Download AutoCAD File")
|