File size: 836 Bytes
a735cb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
import gradio as gr

def calculator(num1, num2, operation):
  if operation == '+':
    return num1 + num2
  elif operation == '-':
    return num1 - num2
  elif operation == '*':
    return num1 * num2
  elif operation == '/':
    if num2 == 0:
      return "除數不能為零"
    return num1 / num2
  else:
    return "無效運算符號"

demo = gr.Interface(
    fn=calculator,
    inputs=[
        gr.Number(label="數字1"),
        gr.Number(label="數字2"),
        gr.Dropdown(["+", "-", "*", "/"], label="運算符號")
    ],
    outputs=gr.Textbox(label="結果"),
    title="簡易計算機",
    description="輸入兩個數字和運算符號,計算結果",
    allow_flagging='never',
    examples=[
        [45, 3, "-"],
        [3.14, 2, "/"],
        [144, 2.5, "*"],
        [1, 1.2, "+"],
    ],
)

demo.launch()