Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,81 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
if operator == '+':
|
| 5 |
result = num1 + num2
|
| 6 |
elif operator == '-':
|
| 7 |
result = num1 - num2
|
| 8 |
elif operator == '*':
|
| 9 |
result = num1 * num2
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
else:
|
| 16 |
-
result =
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Define inputs
|
| 20 |
-
num1 = gr.inputs.Number(label="Number 1")
|
| 21 |
-
num2 = gr.inputs.Number(label="Number 2")
|
| 22 |
-
operator = gr.inputs.Dropdown(choices=['+', '-', '*', '/'], label="Operator")
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Create a row for the first number
|
| 4 |
+
row1 = gr.Row(children=[
|
| 5 |
+
gr.Label('Number 1:'),
|
| 6 |
+
gr.Entry(),
|
| 7 |
+
])
|
| 8 |
+
|
| 9 |
+
# Create a row for the second number
|
| 10 |
+
row2 = gr.Row(children=[
|
| 11 |
+
gr.Label('Number 2:'),
|
| 12 |
+
gr.Entry(),
|
| 13 |
+
])
|
| 14 |
+
|
| 15 |
+
# Create a row for the operator
|
| 16 |
+
row3 = gr.Row(children=[
|
| 17 |
+
gr.Label('Operator:'),
|
| 18 |
+
gr.Choice(options=['+', '-', '*', '/'])
|
| 19 |
+
])
|
| 20 |
+
|
| 21 |
+
# Create a row for the result
|
| 22 |
+
row4 = gr.Row(children=[
|
| 23 |
+
gr.Label('Result:')
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
# Create a column to hold the rows
|
| 27 |
+
column = gr.Column(children=[row1, row2, row3, row4])
|
| 28 |
+
|
| 29 |
+
# Create a widget to hold the column
|
| 30 |
+
widget = gr.Widget(column)
|
| 31 |
+
|
| 32 |
+
# Run the GUI event loop
|
| 33 |
+
gradio.run(widget)
|
| 34 |
+
# Create a layout for the calculator
|
| 35 |
+
layout = gr.GridLayout(cols=3, rows=4)
|
| 36 |
+
|
| 37 |
+
# Add labels and entries for the numbers and operators
|
| 38 |
+
layout.add(gr.Label('Number 1:'))
|
| 39 |
+
layout.add(gr.Entry())
|
| 40 |
+
layout.add(gr. Label('Number 2:'))
|
| 41 |
+
layout.add(gr.Entry())
|
| 42 |
+
layout.add(gr.Label('Operator:'))
|
| 43 |
+
layout.add(gr.Choice(options=['+', '-', '*', '/']))
|
| 44 |
+
|
| 45 |
+
# Add a button to submit the calculation
|
| 46 |
+
layout.add(gr.Button('Calculate'))
|
| 47 |
+
|
| 48 |
+
# Define a function to perform the calculation when the button is clicked
|
| 49 |
+
def calculate():
|
| 50 |
+
num1 = float(layout.get_value(0))
|
| 51 |
+
num2 = float(layout.get_value(2))
|
| 52 |
+
operator = layout.get_value(4)
|
| 53 |
+
|
| 54 |
if operator == '+':
|
| 55 |
result = num1 + num2
|
| 56 |
elif operator == '-':
|
| 57 |
result = num1 - num2
|
| 58 |
elif operator == '*':
|
| 59 |
result = num1 * num2
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
else:
|
| 66 |
+
result = num1 / num2
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Display the result in the final column
|
| 75 |
+
layout.set_value(6, str(result))
|
| 76 |
|
| 77 |
+
# Attach the calculate function to the button
|
| 78 |
+
layout.get_widget(5).on_click(calculate)
|
| 79 |
|
| 80 |
+
# Start the GUI event loop
|
| 81 |
+
gradio.run(layout)
|