Spaces:
Running
Running
import gradio as gr | |
def money_calculator(amount, operation, operand): | |
if operation == 'Add': | |
result = amount + operand | |
elif operation == 'Subtract': | |
result = amount - operand | |
elif operation == 'Multiply': | |
result = amount * operand | |
elif operation == 'Divide': | |
try: | |
result = amount / operand | |
except ZeroDivisionError: | |
result = "Cannot divide by zero." | |
else: | |
result = 'Invalid operation' | |
return result | |
def exponentiation_calculator(base, desired_result): | |
try: | |
# The exponentiation is solved as finding the n-th root of the desired_result | |
exponent = round(desired_result ** (1 / float(base)), 4) | |
return f"{base}^{exponent} = {desired_result}" | |
except Exception as e: | |
return str(e) | |
with gr.Blocks() as app: | |
with gr.Tab("Money Calculator"): | |
with gr.Row(): | |
amount = gr.Number(label="Amount") | |
operation = gr.Dropdown(choices=['Add', 'Subtract', 'Multiply', 'Divide'], label="Operation") | |
operand = gr.Number(label="Operand") | |
money_result = gr.Number(label="Result", interactive=False) | |
money_calculate_btn = gr.Button("Calculate") | |
money_calculate_btn.click(money_calculator, inputs=[amount, operation, operand], outputs=[money_result]) | |
with gr.Tab("Exponentiation Calculator"): | |
with gr.Row(): | |
base = gr.Number(label="Base (n) [To find n^x]") | |
desired_result = gr.Number(label="Desired Result (To get from n^x)") | |
exp_result = gr.Textbox(label="Expression Result", interactive=False) | |
exponentiation_btn = gr.Button("Calculate") | |
exponentiation_btn.click(exponentiation_calculator, inputs=[base, desired_result], outputs=[exp_result]) | |
app.launch() | |