import gradio as gr # Function to calculate percentage def calculate_percentage(part, whole): try: part = float(part) whole = float(whole) if whole == 0: return "Whole cannot be zero" return f"{(part / whole) * 100:.2f}%" except ValueError: return "Invalid input" # Function to calculate percentage increase def percentage_increase(original, new): try: original = float(original) new = float(new) if original == 0: return "Original value cannot be zero" increase = new - original return f"{(increase / original) * 100:.2f}%" except ValueError: return "Invalid input" # Function to calculate percentage decrease def percentage_decrease(original, new): try: original = float(original) new = float(new) if original == 0: return "Original value cannot be zero" decrease = original - new return f"{(decrease / original) * 100:.2f}%" except ValueError: return "Invalid input" # Function to calculate a value from percentage def value_from_percentage(percentage, whole): try: percentage = float(percentage) whole = float(whole) return f"{(percentage / 100) * whole:.2f}" except ValueError: return "Invalid input" # Creating Gradio interface with gr.Blocks() as demo: gr.Markdown("# Percentage Calculator") with gr.Tabs(): with gr.TabItem("Calculate Percentage"): part = gr.Number(label="Part") whole = gr.Number(label="Whole") percentage_output = gr.Textbox(label="Percentage", interactive=False) calc_button = gr.Button("Calculate") calc_button.click(calculate_percentage, inputs=[part, whole], outputs=percentage_output) with gr.TabItem("Percentage Increase"): original_value_inc = gr.Number(label="Original Value") new_value_inc = gr.Number(label="New Value") increase_output = gr.Textbox(label="Percentage Increase", interactive=False) calc_button_inc = gr.Button("Calculate") calc_button_inc.click(percentage_increase, inputs=[original_value_inc, new_value_inc], outputs=increase_output) with gr.TabItem("Percentage Decrease"): original_value_dec = gr.Number(label="Original Value") new_value_dec = gr.Number(label="New Value") decrease_output = gr.Textbox(label="Percentage Decrease", interactive=False) calc_button_dec = gr.Button("Calculate") calc_button_dec.click(percentage_decrease, inputs=[original_value_dec, new_value_dec], outputs=decrease_output) with gr.TabItem("Value from Percentage"): percentage_value = gr.Number(label="Percentage") whole_value = gr.Number(label="Whole") value_output = gr.Textbox(label="Value", interactive=False) calc_button_value = gr.Button("Calculate") calc_button_value.click(value_from_percentage, inputs=[percentage_value, whole_value], outputs=value_output) demo.launch()