Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def calculator(num1, operation, num2):
|
| 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 |
+
if num2 == 0:
|
| 12 |
+
raise gr.Error("Cannot divide by zero!")
|
| 13 |
+
return num1 / num2
|
| 14 |
+
|
| 15 |
+
with gr.Blocks() as demo:
|
| 16 |
+
gr.Markdown(
|
| 17 |
+
"""
|
| 18 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 19 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" style="
|
| 20 |
+
color: #6e6bdc;
|
| 21 |
+
text-decoration: none;
|
| 22 |
+
font-weight: 600;
|
| 23 |
+
font-size: 14px;
|
| 24 |
+
">
|
| 25 |
+
🔥 Built with anycoder
|
| 26 |
+
</a>
|
| 27 |
+
""")
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
with gr.Column(scale=1, min_width=200):
|
| 31 |
+
num1 = gr.Number(
|
| 32 |
+
label="First Number",
|
| 33 |
+
placeholder="Enter a number...",
|
| 34 |
+
elem_classes=["calculator-input"]
|
| 35 |
+
)
|
| 36 |
+
operation = gr.Radio(
|
| 37 |
+
choices=["add", "subtract", "multiply", "divide"],
|
| 38 |
+
value="add",
|
| 39 |
+
label="Operation"
|
| 40 |
+
)
|
| 41 |
+
num2 = gr.Number(
|
| 42 |
+
label="Second Number",
|
| 43 |
+
placeholder="Enter a number...",
|
| 44 |
+
elem_classes=["calculator-input"]
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
with gr.Row():
|
| 48 |
+
submit_btn = gr.Button(
|
| 49 |
+
"Calculate",
|
| 50 |
+
variant="primary",
|
| 51 |
+
size="lg"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
result = gr.Number(
|
| 55 |
+
label="Result",
|
| 56 |
+
interactive=False,
|
| 57 |
+
elem_classes=["calculator-result"]
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Event handling with modern Gradio 6 syntax
|
| 61 |
+
submit_btn.click(
|
| 62 |
+
fn=calculator,
|
| 63 |
+
inputs=[num1, operation, num2],
|
| 64 |
+
outputs=[result],
|
| 65 |
+
api_visibility="public"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Examples with clean styling
|
| 69 |
+
gr.Examples(
|
| 70 |
+
examples=[
|
| 71 |
+
[45, "add", 3],
|
| 72 |
+
[3.14, "divide", 2],
|
| 73 |
+
[144, "multiply", 2.5],
|
| 74 |
+
[0, "subtract", 1.2],
|
| 75 |
+
],
|
| 76 |
+
inputs=[num1, operation, num2],
|
| 77 |
+
label="Try these examples:"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
demo.launch(
|
| 82 |
+
theme=gr.themes.Soft(
|
| 83 |
+
primary_hue="indigo",
|
| 84 |
+
secondary_hue="indigo",
|
| 85 |
+
neutral_hue="slate"
|
| 86 |
+
)
|