Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import math
|
| 3 |
import itertools
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Available Capacitor Units (kVAR)
|
| 6 |
available_capacitors = [25, 20, 15, 10, 5, 2.5, 1.5, 1]
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def calculate_power_parameters(voltage, current, power_factor):
|
| 9 |
if voltage > 0 and current > 0:
|
| 10 |
apparent_power = math.sqrt(3) * voltage * current
|
|
@@ -28,14 +45,16 @@ def design_capacitor_bank(reactive_power, num_caps):
|
|
| 28 |
best_combo = None
|
| 29 |
min_error = float('inf')
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
total = sum(combo)
|
| 33 |
error = abs(total - reactive_power)
|
| 34 |
-
|
| 35 |
if error < min_error:
|
| 36 |
min_error = error
|
| 37 |
best_combo = combo
|
| 38 |
-
|
| 39 |
if error == 0:
|
| 40 |
break
|
| 41 |
|
|
@@ -47,19 +66,33 @@ def design_capacitor_bank(reactive_power, num_caps):
|
|
| 47 |
"suggested_capacitors": suggested_capacitors,
|
| 48 |
"total_kvar": round(total_kvar, 2),
|
| 49 |
"message": message,
|
|
|
|
| 50 |
}
|
| 51 |
else:
|
| 52 |
return {"message": "Could not find a suitable combination."}
|
| 53 |
else:
|
| 54 |
return None
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def reactive_power_first(voltage, current, power_factor):
|
| 57 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 58 |
if power_results:
|
| 59 |
-
apparent_power_out = f"Apparent Power: {power_results['apparent_power']} VA"
|
| 60 |
-
real_power_out = f"Real Power: {power_results['real_power']} kW"
|
| 61 |
-
reactive_power_out = f"Reactive Power: {power_results['reactive_power']} kVAR"
|
| 62 |
-
calculated_pf_out = f"Calculated Power Factor: {power_results['calculated_pf']}"
|
| 63 |
return (
|
| 64 |
apparent_power_out,
|
| 65 |
real_power_out,
|
|
@@ -74,17 +107,19 @@ def finalize_capacitor_bank(reactive_power, num_caps):
|
|
| 74 |
cap_bank_design = design_capacitor_bank(reactive_power, num_caps)
|
| 75 |
if cap_bank_design and cap_bank_design.get("suggested_capacitors"):
|
| 76 |
suggested_capacitors_text = "<br>".join(
|
| 77 |
-
[f"🔹 Capacitor {idx + 1}: {cap}" for idx, cap in enumerate(cap_bank_design['suggested_capacitors'])]
|
| 78 |
)
|
| 79 |
-
|
|
|
|
| 80 |
else:
|
| 81 |
-
return "Could not find a suitable combination.", ""
|
| 82 |
|
| 83 |
with gr.Blocks() as iface:
|
| 84 |
gr.Markdown("# ⚡ Three-Phase Power Calculator - Reactive Power Compensation")
|
| 85 |
gr.Markdown("""
|
| 86 |
-
Step 1: Enter system parameters to calculate apparent and reactive power
|
| 87 |
-
Step 2: Input number of capacitors to compute optimal configuration
|
|
|
|
| 88 |
""")
|
| 89 |
|
| 90 |
with gr.Row():
|
|
@@ -99,7 +134,7 @@ with gr.Blocks() as iface:
|
|
| 99 |
real_power_out = gr.HTML()
|
| 100 |
reactive_power_out = gr.HTML()
|
| 101 |
calculated_pf_out = gr.HTML()
|
| 102 |
-
reactive_value = gr.
|
| 103 |
|
| 104 |
calc_btn.click(
|
| 105 |
fn=reactive_power_first,
|
|
@@ -119,11 +154,12 @@ with gr.Blocks() as iface:
|
|
| 119 |
|
| 120 |
capacitor_out = gr.HTML()
|
| 121 |
total_comp_out = gr.HTML()
|
|
|
|
| 122 |
|
| 123 |
finalize_btn.click(
|
| 124 |
fn=finalize_capacitor_bank,
|
| 125 |
inputs=[reactive_value, num_caps_input],
|
| 126 |
-
outputs=[capacitor_out, total_comp_out]
|
| 127 |
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import math
|
| 3 |
import itertools
|
| 4 |
+
import ezdxf
|
| 5 |
+
import os
|
| 6 |
+
import groq
|
| 7 |
+
|
| 8 |
+
# Initialize Groq client
|
| 9 |
+
client = groq.Client(api_key=os.getenv("GROQ_API_KEY"))
|
| 10 |
|
| 11 |
# Available Capacitor Units (kVAR)
|
| 12 |
available_capacitors = [25, 20, 15, 10, 5, 2.5, 1.5, 1]
|
| 13 |
|
| 14 |
+
# Prompt Groq for explanation (optional)
|
| 15 |
+
def ask_groq(prompt):
|
| 16 |
+
try:
|
| 17 |
+
response = client.chat.completions.create(
|
| 18 |
+
model="llama3-8b-8192",
|
| 19 |
+
messages=[{"role": "user", "content": prompt}]
|
| 20 |
+
)
|
| 21 |
+
return response.choices[0].message.content
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Groq Error: {str(e)}"
|
| 24 |
+
|
| 25 |
def calculate_power_parameters(voltage, current, power_factor):
|
| 26 |
if voltage > 0 and current > 0:
|
| 27 |
apparent_power = math.sqrt(3) * voltage * current
|
|
|
|
| 45 |
best_combo = None
|
| 46 |
min_error = float('inf')
|
| 47 |
|
| 48 |
+
# Allow repetition only when needed
|
| 49 |
+
combos = itertools.combinations_with_replacement(available_capacitors, num_caps)
|
| 50 |
+
for combo in combos:
|
| 51 |
+
if len(set(combo)) < len(combo) and len(set(combo)) != 1:
|
| 52 |
+
continue
|
| 53 |
total = sum(combo)
|
| 54 |
error = abs(total - reactive_power)
|
|
|
|
| 55 |
if error < min_error:
|
| 56 |
min_error = error
|
| 57 |
best_combo = combo
|
|
|
|
| 58 |
if error == 0:
|
| 59 |
break
|
| 60 |
|
|
|
|
| 66 |
"suggested_capacitors": suggested_capacitors,
|
| 67 |
"total_kvar": round(total_kvar, 2),
|
| 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 |
+
for idx, cap in enumerate(capacitors):
|
| 81 |
+
label = f"{cap} kVAR"
|
| 82 |
+
msp.add_lwpolyline([(x, 0), (x+10, 0), (x+10, 10), (x, 10), (x, 0)])
|
| 83 |
+
msp.add_text(label, dxfattribs={'height': 2.5}).set_pos((x, 12))
|
| 84 |
+
x += 15
|
| 85 |
+
output_path = "/mnt/data/capacitor_bank.dxf"
|
| 86 |
+
doc.saveas(output_path)
|
| 87 |
+
return output_path
|
| 88 |
+
|
| 89 |
def reactive_power_first(voltage, current, power_factor):
|
| 90 |
power_results = calculate_power_parameters(voltage, current, power_factor)
|
| 91 |
if power_results:
|
| 92 |
+
apparent_power_out = f"Apparent Power: **{power_results['apparent_power']} VA**"
|
| 93 |
+
real_power_out = f"Real Power: **{power_results['real_power']} kW**"
|
| 94 |
+
reactive_power_out = f"Reactive Power: **{power_results['reactive_power']} kVAR**"
|
| 95 |
+
calculated_pf_out = f"Calculated Power Factor: **{power_results['calculated_pf']}**"
|
| 96 |
return (
|
| 97 |
apparent_power_out,
|
| 98 |
real_power_out,
|
|
|
|
| 107 |
cap_bank_design = design_capacitor_bank(reactive_power, num_caps)
|
| 108 |
if cap_bank_design and cap_bank_design.get("suggested_capacitors"):
|
| 109 |
suggested_capacitors_text = "<br>".join(
|
| 110 |
+
[f"🔹 Capacitor {idx + 1}: **{cap}**" for idx, cap in enumerate(cap_bank_design['suggested_capacitors'])]
|
| 111 |
)
|
| 112 |
+
dxf_path = create_dxf_capacitor_bank(cap_bank_design["combo"])
|
| 113 |
+
return suggested_capacitors_text, cap_bank_design['message'], dxf_path
|
| 114 |
else:
|
| 115 |
+
return "Could not find a suitable combination.", "", None
|
| 116 |
|
| 117 |
with gr.Blocks() as iface:
|
| 118 |
gr.Markdown("# ⚡ Three-Phase Power Calculator - Reactive Power Compensation")
|
| 119 |
gr.Markdown("""
|
| 120 |
+
Step 1: Enter system parameters to calculate apparent and reactive power.<br>
|
| 121 |
+
Step 2: Input number of capacitors to compute optimal configuration.<br>
|
| 122 |
+
Step 3: Download AutoCAD (.dxf) layout.
|
| 123 |
""")
|
| 124 |
|
| 125 |
with gr.Row():
|
|
|
|
| 134 |
real_power_out = gr.HTML()
|
| 135 |
reactive_power_out = gr.HTML()
|
| 136 |
calculated_pf_out = gr.HTML()
|
| 137 |
+
reactive_value = gr.Number(visible=False)
|
| 138 |
|
| 139 |
calc_btn.click(
|
| 140 |
fn=reactive_power_first,
|
|
|
|
| 154 |
|
| 155 |
capacitor_out = gr.HTML()
|
| 156 |
total_comp_out = gr.HTML()
|
| 157 |
+
dxf_file = gr.File(label="📥 Download AutoCAD File", visible=False)
|
| 158 |
|
| 159 |
finalize_btn.click(
|
| 160 |
fn=finalize_capacitor_bank,
|
| 161 |
inputs=[reactive_value, num_caps_input],
|
| 162 |
+
outputs=[capacitor_out, total_comp_out, dxf_file]
|
| 163 |
)
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|