Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,39 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
# Create a layout for the calculator
|
| 4 |
-
layout = gr.
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def
|
| 19 |
-
num1 = float(
|
| 20 |
-
num2 = float(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
return num1 ** 0.5
|
| 41 |
-
|
| 42 |
-
def exp():
|
| 43 |
-
num1 = float(input_box.get_value('num1'))
|
| 44 |
-
return num1 ** Math.E
|
| 45 |
-
|
| 46 |
-
# Set up the buttons and outputs
|
| 47 |
-
button_equal = gr.Button(id='equal', label='=')
|
| 48 |
-
button_add = gr.Button(id='add', label='+')
|
| 49 |
-
button_subtract = gr.Button(id='subtract', label='-')
|
| 50 |
-
button_multiply = gr.Button(id='multiply', label='*')
|
| 51 |
-
button_divide = gr.Button(id='divide', label='/')
|
| 52 |
-
button_power = gr.Button(id='power', label='^')
|
| 53 |
-
button_sqrt = gr.Button(id='sqrt', label='√')
|
| 54 |
-
button_exp = gr.Button(id='exp', label='e^x')
|
| 55 |
-
|
| 56 |
-
output = gr.TextOutput(id='result', text='')
|
| 57 |
-
|
| 58 |
-
# Set up the event handlers
|
| 59 |
-
def update_output(sender):
|
| 60 |
-
global output
|
| 61 |
-
if sender == button_equal:
|
| 62 |
-
num1 = float(input_box.get_value('num1'))
|
| 63 |
-
num2 = float(input_box.get_value('num2'))
|
| 64 |
-
output.text = f'{num1} {sender.label} {num2} = {num1} {sender.label} {num2}'
|
| 65 |
-
elif sender == button_add or sender == button_subtract or sender == button_multiply or sender == button_divide:
|
| 66 |
-
num1 = float(input_box.get_value('num1'))
|
| 67 |
-
num2 = float(input_ box.get_value('num2'))
|
| 68 |
-
result = None
|
| 69 |
-
if sender == button_add:
|
| 70 |
-
result = add()
|
| 71 |
-
elif sender == button_subtract:
|
| 72 |
-
result = subtract()
|
| 73 |
-
elif sender == button_multiply:
|
| 74 |
-
result = multiply()
|
| 75 |
-
else:
|
| 76 |
-
result = divide()
|
| 77 |
-
output.text = f'{num1} {sender.label} {num2} = {result}'
|
| 78 |
-
elif sender == button_power or sender == button_sqrt or sender == button_exp:
|
| 79 |
-
num1 = float(input_box.get_value('num1'))
|
| 80 |
-
num2 = float(input_box.get_value('num2'))
|
| 81 |
-
result = None
|
| 82 |
-
if sender == button_power:
|
| 83 |
-
result = power()
|
| 84 |
-
elif sender == button_sqrt:
|
| 85 |
-
result = sqrt()
|
| 86 |
-
else:
|
| 87 |
-
result = exp()
|
| 88 |
-
output.text = f'{num1} {sender.label} {num2} = {result}'
|
| 89 |
-
|
| 90 |
-
# Run the application
|
| 91 |
-
gr.run_application(layout)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
# Create a layout for the calculator
|
| 4 |
+
layout = gr.GridLayout(cols=3, rows=4)
|
| 5 |
+
|
| 6 |
+
# Add labels and entries for the numbers and operators
|
| 7 |
+
layout.add(gr.Label('Number 1:'))
|
| 8 |
+
layout.add(gr.Entry())
|
| 9 |
+
layout.add(gr. Label('Number 2:'))
|
| 10 |
+
layout.add(gr.Entry())
|
| 11 |
+
layout.add(gr.Label('Operator:'))
|
| 12 |
+
layout.add(gr.Choice(options=['+', '-', '*', '/']))
|
| 13 |
+
|
| 14 |
+
# Add a button to submit the calculation
|
| 15 |
+
layout.add(gr.Button('Calculate'))
|
| 16 |
+
|
| 17 |
+
# Define a function to perform the calculation when the button is clicked
|
| 18 |
+
def calculate():
|
| 19 |
+
num1 = float(layout.get_value(0))
|
| 20 |
+
num2 = float(layout.get_value(2))
|
| 21 |
+
operator = layout.get_value(4)
|
| 22 |
+
|
| 23 |
+
if operator == '+':
|
| 24 |
+
result = num1 + num2
|
| 25 |
+
elif operator == '-':
|
| 26 |
+
result = num1 - num2
|
| 27 |
+
elif operator == '*':
|
| 28 |
+
result = num1 * num2
|
| 29 |
+
else:
|
| 30 |
+
result = num1 / num2
|
| 31 |
+
|
| 32 |
+
# Display the result in the final column
|
| 33 |
+
layout.set_value(6, str(result))
|
| 34 |
+
|
| 35 |
+
# Attach the calculate function to the button
|
| 36 |
+
layout.get_widget(5).on_click(calculate)
|
| 37 |
+
|
| 38 |
+
# Start the GUI event loop
|
| 39 |
+
gradio.run(layout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|