Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
# Create a
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
|
| 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)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Create a Gradio interface
|
| 4 |
+
interface = gr.Interface()
|
| 5 |
+
|
| 6 |
+
# Add a text input for the user to enter their equation
|
| 7 |
+
text_input = gr.TextInput(interface, 'Enter an equation:')
|
| 8 |
+
|
| 9 |
+
# Add a button to submit the equation
|
| 10 |
+
submit_button = gr.Button(interface, 'Submit')
|
| 11 |
+
|
| 12 |
+
# Create a function to evaluate the equation
|
| 13 |
+
def evaluate_equation(equ):
|
| 14 |
+
# Parse the equation string into tokens
|
| 15 |
+
tokens = equ.split('')
|
| 16 |
+
|
| 17 |
+
# Define a dictionary to map operators to their corresponding functions
|
| 18 |
+
op_dict = {'+': lambda x, y: x + y,
|
| 19 |
+
'-': lambda x, y: x - y,
|
| 20 |
+
'*': lambda x, y: x * y,
|
| 21 |
+
'/': lambda x, y: x / y}
|
| 22 |
+
|
| 23 |
+
# Evaluate the expression recursively
|
| 24 |
+
def eval_expression(expr):
|
| 25 |
+
if expr == '':
|
| 26 |
+
return None
|
| 27 |
+
elif expr in op_dict:
|
| 28 |
+
return op_dict[expr](eval_expression(tokens[:-1]), eval_expression(tokens[-1]))
|
| 29 |
+
else:
|
| 30 |
+
return float(expr)
|
| 31 |
+
|
| 32 |
+
return eval_expression(tokens)
|
| 33 |
+
|
| 34 |
+
# Add a callback function to the button to evaluate the equation when clicked
|
| 35 |
+
submit_button.on_click(lambda: interface.update_output(evaluate_equation(text_input.value)))
|
| 36 |
+
|
| 37 |
+
# Run the Gradio interface
|
| 38 |
+
interface.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|