ysharma HF staff commited on
Commit
1767d8a
·
verified ·
1 Parent(s): 84e1642

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -24
app.py CHANGED
@@ -1,30 +1,37 @@
1
  import gradio as gr
2
- import random
3
- import time
4
 
 
 
 
 
 
 
 
 
 
5
 
6
- def textbox_dynamic(example: str):
7
- yield gr.Textbox(placeholder="What is 2+2?")
8
- time.sleep(1)
9
- yield gr.Textbox(placeholder="What is the meaning of Life?")
10
- time.sleep(1)
11
- yield gr.Textbox(placeholder="What is Gradio?")
12
- time.sleep(1)
13
-
14
  with gr.Blocks() as demo:
15
- chatbot = gr.Chatbot(type="messages")
16
- msg = gr.Textbox()
17
- clear = gr.ClearButton([msg, chatbot])
 
 
 
 
 
18
 
19
- def respond(message, chat_history):
20
- bot_message = random.choice(["How are you?", "Today is a great day", "I'm very hungry"])
21
- chat_history.append({"role": "user", "content": message})
22
- chat_history.append({"role": "assistant", "content": bot_message})
23
- time.sleep(2)
24
- return "", chat_history
 
 
 
 
 
 
25
 
26
- msg.submit(respond, [msg, chatbot], [msg, chatbot])
27
- #submit_btn.click(calculator, inputs=[num_1, operation, num_2], outputs=[result], js=True, preprocess=False, postprocess=False )
28
- msg.blur(textbox_dynamic, inputs=[msg], outputs=[msg], js=True, preprocess=False, postprocess=False)
29
-
30
- demo.launch()
 
1
  import gradio as gr
 
 
2
 
3
+ def calculator(num1: int, operation: str, num2: int):
4
+ if operation == "add":
5
+ return num1 + num2
6
+ elif operation == "subtract":
7
+ return num1 - num2
8
+ elif operation == "multiply":
9
+ return num1 * num2
10
+ elif operation == "divide":
11
+ return num1 / num2
12
 
 
 
 
 
 
 
 
 
13
  with gr.Blocks() as demo:
14
+ with gr.Row():
15
+ with gr.Column():
16
+ num_1 = gr.Number(value=4)
17
+ operation = gr.Radio(["add", "subtract", "multiply", "divide"])
18
+ num_2 = gr.Number(value=0)
19
+ submit_btn = gr.Button(value="Calculate")
20
+ with gr.Column():
21
+ result = gr.Number()
22
 
23
+ submit_btn.click(
24
+ calculator, inputs=[num_1, operation, num_2], outputs=[result], js=True, preprocess=False, postprocess=False
25
+ )
26
+ examples = gr.Examples(
27
+ examples=[
28
+ [5, "add", 3],
29
+ [4, "divide", 2],
30
+ [-4, "multiply", 2.5],
31
+ [0, "subtract", 1.2],
32
+ ],
33
+ inputs=[num_1, operation, num_2],
34
+ )
35
 
36
+ if __name__ == "__main__":
37
+ demo.launch()