0Hojoon0 commited on
Commit
c7bf7e6
·
verified ·
1 Parent(s): dd63f12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -79
app.py CHANGED
@@ -1,81 +1,38 @@
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)
 
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()