gradio/demo/calculator.py

31 lines
788 B
Python
Raw Normal View History

2020-10-27 06:27:28 +08:00
import gradio as gr
import random
2020-10-27 06:27:28 +08:00
def calculator(num1, operation, num2):
2021-05-13 00:29:18 +08:00
print(num1, operation, num2)
2020-10-27 06:27:28 +08:00
if operation == "add":
return num1 + num2
elif operation == "subtract":
return num1 - num2
elif operation == "multiply":
return num1 * num2
elif operation == "divide":
return num1 / num2
iface = gr.Interface(calculator,
2021-05-24 14:53:30 +08:00
["number", gr.inputs.Radio(["add", "subtract", "multiply", "divide"]), "number"],
2020-10-27 06:27:28 +08:00
"number",
examples=[
2020-11-05 04:16:10 +08:00
[5, "add", 3],
[4, "divide", 2],
[-4, "multiply", 2.5],
[0, "subtract", 1.2],
2021-01-30 02:23:17 +08:00
],
2021-05-24 23:31:44 +08:00
title="test calculator",
description="heres a sample toy calculator. enjoy!",
flagging_options=["this", "or", "that"]
2020-10-27 06:27:28 +08:00
)
2020-11-11 22:15:53 +08:00
2020-10-27 06:27:28 +08:00
if __name__ == "__main__":
iface.launch()