0Hojoon0 oldgarden21 commited on
Commit
f117cfc
·
verified ·
1 Parent(s): 4a91085

Create app.py (#1)

Browse files

- Create app.py (122f32482d26ab4b3a2db89db5b6a0f5f56b5103)


Co-authored-by: Jaecheon Jeon <oldgarden21@users.noreply.huggingface.co>

Files changed (1) hide show
  1. app.py +19 -58
app.py CHANGED
@@ -1,70 +1,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
  else:
61
- result = num1 / num2
 
 
 
 
 
 
62
 
63
- # Display the result in the final column
64
- layout.set_value(6, str(result))
65
 
66
- # Attach the calculate function to the button
67
- layout.get_widget(5).on_click(calculate)
68
 
69
- # Start the GUI event loop
70
- gradio.run(layout)
 
1
  import gradio as gr
2
 
3
+ def calculate(num1, num2, operator):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  if operator == '+':
5
  result = num1 + num2
6
  elif operator == '-':
7
  result = num1 - num2
8
  elif operator == '*':
9
  result = num1 * num2
10
+ elif operator == '/':
11
+ if num2 != 0:
12
+ result = num1 / num2
13
+ else:
14
+ result = "Cannot divide by zero"
15
  else:
16
+ result = "Invalid operator"
17
+ return result
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
+ # Define outputs
25
+ output = gr.outputs.Textbox(label="Result")
26
 
27
+ # Create interface
28
+ interface = gr.Interface(fn=calculate, inputs=[num1, num2, operator], outputs=output, title="Simple Calculator")
29
 
30
+ # Launch interface
31
+ interface.launch()