Spaces:
Sleeping
Sleeping
import gradio as gr | |
import matplotlib.pyplot as plt | |
import numpy as np | |
def calculate_power_factor_correction(load_kw, pf_initial, pf_target): | |
try: | |
# Validate inputs | |
if load_kw <= 0: | |
raise ValueError("Load (kW) must be greater than zero.") | |
if not (0 < pf_initial < 1): | |
raise ValueError("Initial Power Factor must be between 0 and 1.") | |
if not (0 < pf_target < 1): | |
raise ValueError("Target Power Factor must be between 0 and 1.") | |
# Convert power factor to angle in radians | |
theta_initial = np.arccos(pf_initial) | |
theta_target = np.arccos(pf_target) | |
# Calculate initial and target reactive power | |
q_initial = load_kw * np.tan(theta_initial) | |
q_target = load_kw * np.tan(theta_target) | |
# Calculate the required capacitor size in kVAR | |
capacitor_size = q_initial - q_target | |
# Calculate the new power factor | |
pf_corrected = np.cos(np.arctan((q_initial - capacitor_size) / load_kw)) | |
# Generate graphs | |
fig, axs = plt.subplots(1, 2, figsize=(12, 6)) | |
# Before correction | |
axs[0].bar(["Initial"], [pf_initial], color='blue') | |
axs[0].set_ylim(0, 1) | |
axs[0].set_title('Power Factor Before Correction') | |
axs[0].set_ylabel('Power Factor') | |
# After correction | |
axs[1].bar(["Corrected"], [pf_corrected], color='green') | |
axs[1].set_ylim(0, 1) | |
axs[1].set_title('Power Factor After Correction') | |
axs[1].set_ylabel('Power Factor') | |
plt.tight_layout() | |
plt.savefig("power_factor_correction.png") | |
plt.close(fig) | |
return (f"Required Capacitor Size: {capacitor_size:.2f} kVAR", | |
f"Corrected Power Factor: {pf_corrected:.2f}", | |
"power_factor_correction.png") | |
except Exception as e: | |
return str(e), "", "" | |
# Create Gradio interface | |
def power_factor_correction_ui(load_kw, pf_initial, pf_target): | |
result = calculate_power_factor_correction(load_kw, pf_initial, pf_target) | |
return result | |
with gr.Blocks() as demo: | |
gr.Markdown("# Power Factor Correction Calculator") | |
with gr.Row(): | |
load_kw = gr.Number(label="Load (kW)", value=100) | |
pf_initial = gr.Number(label="Initial Power Factor", value=0.7) | |
pf_target = gr.Number(label="Target Power Factor", value=0.9) | |
with gr.Row(): | |
output1 = gr.Textbox(label="Required Capacitor Size", placeholder="kVAR") | |
output2 = gr.Textbox(label="Corrected Power Factor", placeholder="PF") | |
output3 = gr.Image(label="Graphs") | |
submit_btn = gr.Button("Calculate") | |
submit_btn.click(fn=power_factor_correction_ui, | |
inputs=[load_kw, pf_initial, pf_target], | |
outputs=[output1, output2, output3]) | |
demo.launch() | |