Spaces:
Runtime error
Runtime error
Create app.py (#1)
Browse files- Create app.py (122f32482d26ab4b3a2db89db5b6a0f5f56b5103)
Co-authored-by: Jaecheon Jeon <oldgarden21@users.noreply.huggingface.co>
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import gradio as gr# Create a row for the first numberrow1 = gr.Row(children=[gr.Label('Number 1:'),gr.Entry(),])# Create a row for the second numberrow2 = gr.Row(children=[gr.Label('Number 2:'),gr.Entry(),])# Create a row for the operatorrow3 = gr.Row(children=[gr.Label('Operator:'),gr.Choice(options=['+', '-', '*', '/'])])# Create a row for the resultrow4 = gr.Row(children=[gr.Label('Result:')])# Create a column to hold the rowscolumn = gr.Column(children=[row1, row2, row3, row4])# Create a widget to hold the columnwidget = gr.Widget(column)# Run the GUI event loopgradio.run(widget)# Create a layout for the calculatorlayout = gr.GridLayout(cols=3, rows=4)# Add labels and entries for the numbers and operatorslayout.add(gr.Label('Number 1:'))layout.add(gr.Entry())layout.add(gr. Label('Number 2:'))layout.add(gr.Entry())layout.add(gr.Label('Operator:'))layout.add(gr.Choice(options=['+', '-', '*', '/']))# Add a button to submit the calculationlayout.add(gr.Button('Calculate'))# Define a function to perform the calculation when the button is clickeddef calculate():num1 = float(layout.get_value(0))num2 = float(layout.get_value(2))operator = layout.get_value(4)if operator == '+':result = num1 + num2elif operator == '-':result = num1 - num2elif operator == '*':result = num1 * num2else:result = num1 / num2# Display the result in the final columnlayout.set_value(6, str(result))# Attach the calculate function to the buttonlayout.get_widget(5).on_click(calculate)# Start the GUI event loopgradio.run(layout)12345678910111213141516171819202122232425262728293031import gradio as grdef calculate(num1, num2, operator):if operator == '+':result = num1 + num2elif operator == '-':result = num1 - num2elif operator == '*':result = num1 * num2elif operator == '/':if num2 != 0:result = num1 / num2else:result = "Cannot divide by zero"else:result = "Invalid operator"return result# Define inputsnum1 = gr.inputs.Number(label="Number 1")num2 = gr.inputs.Number(label="Number 2")operator = gr.inputs.Dropdown(choices=['+', '-', '*', '/'], label="Operator")# Define outputsoutput = gr.outputs.Textbox(label="Result")# Create interfaceinterface = gr.Interface(fn=calculate, inputs=[num1, num2, operator], outputs=output, title="Simple Calculator")# Launch interfaceinterface.launch()
app.py+19-58