Update app.py
Browse files40 unmodified lines4142434445464748495051525354555617 unmodified lines747576777879802 unmodified lines8384858687888925 unmodified lines1151161171181191201217 unmodified lines12913013113213313416 unmodified lines15115215315415515615740 unmodified linesreturn Nonedef design_capacitor_bank(reactive_power, num_caps):try:num_caps = int(num_caps)except (ValueError, TypeError):return {"message": "Number of capacitors must be a positive integer."}if reactive_power > 0 and num_caps > 0:best_combo = Nonemin_error = float('inf')# Allow repetition freely to get best matchcombos = itertools.combinations_with_replacement(available_capacitors, num_caps)for combo in combos:total = sum(combo)17 unmodified lineselse:return {"message": "Could not find a suitable combination."}else:return {"message": "Reactive power and number of capacitors must be greater than zero."}def create_dxf_capacitor_bank(capacitors):doc = ezdxf.new()2 unmodified linesfor idx, cap in enumerate(capacitors):label = f"{cap} kVAR"msp.add_lwpolyline([(x, 0), (x+10, 0), (x+10, 10), (x, 10), (x, 0)])msp.add_text(label, dxfattribs={'height': 2.5}).set_pos((x, 12))x += 15output_path = "/mnt/data/capacitor_bank.dxf"doc.saveas(output_path)25 unmodified linesdxf_path = create_dxf_capacitor_bank(cap_bank_design["combo"])return suggested_capacitors_text, cap_bank_design['message'], dxf_pathelse:return cap_bank_design.get("message", "Could not find a suitable combination."), "", Nonewith gr.Blocks() as iface:gr.Markdown("# β‘ Three-Phase Power Calculator - Reactive Power Compensation")7 unmodified linesvoltage = gr.Number(label="Enter Voltage (V)", value=415)current = gr.Number(label="Enter Current (A)", value=250)power_factor = gr.Slider(label="Power Factor", minimum=0.0, maximum=1.0, value=0.85, step=0.01)calc_btn = gr.Button("π Calculate Power Parameters")16 unmodified lines)gr.Markdown("### β Enter number of capacitors to compensate reactive power:")num_caps_input = gr.Number(label="Number of Capacitors", precision=0, value=1)finalize_btn = gr.Button("βοΈ Generate Capacitor Bank")capacitor_out = gr.HTML()40 unmodified lines414243444546474849505117 unmodified lines697071727374752 unmodified lines787980818283848525 unmodified lines1111121131141151161177 unmodified lines12512612712812913013116 unmodified lines14814915015115215315440 unmodified linesreturn Nonedef design_capacitor_bank(reactive_power, num_caps):if reactive_power > 0 and num_caps > 0:best_combo = Nonemin_error = float('inf')# Allow repetition freely to match reactive powercombos = itertools.combinations_with_replacement(available_capacitors, num_caps)for combo in combos:total = sum(combo)17 unmodified lineselse:return {"message": "Could not find a suitable combination."}else:return Nonedef create_dxf_capacitor_bank(capacitors):doc = ezdxf.new()2 unmodified linesfor idx, cap in enumerate(capacitors):label = f"{cap} kVAR"msp.add_lwpolyline([(x, 0), (x+10, 0), (x+10, 10), (x, 10), (x, 0)])# Fixed here: use insert= to set text positionmsp.add_text(label, dxfattribs={'height': 2.5}, insert=(x, 12))x += 15output_path = "/mnt/data/capacitor_bank.dxf"doc.saveas(output_path)25 unmodified linesdxf_path = create_dxf_capacitor_bank(cap_bank_design["combo"])return suggested_capacitors_text, cap_bank_design['message'], dxf_pathelse:return "Could not find a suitable combination.", "", Nonewith gr.Blocks() as iface:gr.Markdown("# β‘ Three-Phase Power Calculator - Reactive Power Compensation")7 unmodified linesvoltage = gr.Number(label="Enter Voltage (V)", value=415)current = gr.Number(label="Enter Current (A)", value=250)power_factor = gr.Slider(label="Power Factor", minimum=0.0, maximum=1.0, value=0.85, step=0.01)frequency = gr.Radio(label="Select Frequency", choices=[50, 60], value=50)calc_btn = gr.Button("π Calculate Power Parameters")16 unmodified lines)gr.Markdown("### β Enter number of capacitors to compensate reactive power:")num_caps_input = gr.Number(label="Number of Capacitors", precision=0)finalize_btn = gr.Button("βοΈ Generate Capacitor Bank")capacitor_out = gr.HTML()
app.py+7-10