Spaces:
Sleeping
Sleeping
File size: 5,633 Bytes
0be3d56 a6a478a 0be3d56 a6a478a 0be3d56 a6a478a 0be3d56 9b29538 0be3d56 a6a478a 9b29538 0be3d56 a6a478a 9b29538 a6a478a 9b29538 a6a478a 9b29538 a6a478a 9b29538 a6a478a 9b29538 0be3d56 a6a478a 0be3d56 a6a478a 0be3d56 a6a478a 9b29538 a6a478a 9b29538 a6a478a 9b29538 a6a478a 9b29538 |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
import gradio as gr
def calculator(num1, operation, num2):
if operation == "add":
return num1 + num2
elif operation == "subtract":
return num1 - num2
elif operation == "multiply":
return num1 * num2
elif operation == "divide":
if num2 == 0:
raise gr.Error("Cannot divide by zero!")
return num1 / num2
# Custom CSS for modern, mobile-friendly design
custom_css = """
/* Mobile-first responsive design */
.gradio-container {
max-width: 500px !important;
margin: 0 auto !important;
padding: 1rem !important;
}
/* Header styling */
.header-section {
text-align: center;
margin-bottom: 2rem;
}
.header-section h1 {
font-size: 2.5rem;
font-weight: 700;
margin-bottom: 0.5rem;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.header-link {
font-size: 0.875rem;
opacity: 0.7;
margin-top: 0.25rem;
}
/* Calculator card */
.calculator-card {
background: rgba(255, 255, 255, 0.05);
border-radius: 1.5rem;
padding: 1.5rem;
backdrop-filter: blur(10px);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}
/* Input styling */
.input-group {
margin-bottom: 1rem;
}
/* Operation buttons */
.operation-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 0.75rem;
margin: 1.5rem 0;
}
/* Result display */
.result-display {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 1rem;
padding: 1.5rem;
text-align: center;
margin: 1.5rem 0;
min-height: 80px;
display: flex;
align-items: center;
justify-content: center;
}
.result-display .output-class {
font-size: 2rem !important;
font-weight: 700 !important;
color: white !important;
}
/* Button styling */
.calculate-btn {
width: 100%;
padding: 1rem !important;
font-size: 1.125rem !important;
font-weight: 600 !important;
border-radius: 0.75rem !important;
margin: 1rem 0 !important;
}
/* Examples section */
.examples-section {
margin-top: 2rem;
padding-top: 2rem;
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
/* Mobile responsiveness */
@media (max-width: 640px) {
.header-section h1 {
font-size: 2rem;
}
.calculator-card {
padding: 1rem;
}
.result-display .output-class {
font-size: 1.5rem !important;
}
}
/* Smooth transitions */
* {
transition: all 0.2s ease-in-out;
}
"""
with gr.Blocks(fill_height=True) as demo:
# Header
with gr.Row(elem_classes="header-section"):
gr.Markdown(
"""
# 🧮 Calculator
[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
""",
elem_classes="header-link"
)
# Main calculator interface
with gr.Column(elem_classes="calculator-card"):
# Input numbers
num1 = gr.Number(
label="First Number",
placeholder="0",
value=0,
elem_classes="input-group"
)
# Operation selector - using radio for clarity
operation = gr.Radio(
choices=["add", "subtract", "multiply", "divide"],
value="add",
label="Operation",
elem_classes="input-group"
)
num2 = gr.Number(
label="Second Number",
placeholder="0",
value=0,
elem_classes="input-group"
)
# Calculate button
submit_btn = gr.Button(
"Calculate",
variant="primary",
size="lg",
elem_classes="calculate-btn"
)
# Result display
result = gr.Number(
label="Result",
interactive=False,
elem_classes="result-display"
)
# Examples section
with gr.Column(elem_classes="examples-section"):
gr.Markdown("### Quick Examples")
gr.Examples(
examples=[
[45, "add", 3],
[100, "subtract", 25],
[12, "multiply", 8],
[144, "divide", 12],
],
inputs=[num1, operation, num2],
outputs=[result],
fn=calculator,
cache_examples=False
)
# Event handlers
submit_btn.click(
fn=calculator,
inputs=[num1, operation, num2],
outputs=[result],
api_visibility="public"
)
# Allow Enter key to calculate
num2.submit(
fn=calculator,
inputs=[num1, operation, num2],
outputs=[result]
)
if __name__ == "__main__":
demo.launch(
theme=gr.themes.Soft(
primary_hue="violet",
secondary_hue="purple",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="md",
radius_size="lg"
).set(
button_primary_background_fill="linear-gradient(135deg, #667eea 0%, #764ba2 100%)",
button_primary_background_fill_hover="linear-gradient(135deg, #764ba2 0%, #667eea 100%)",
block_title_text_weight="600",
block_border_width="0px",
input_background_fill="rgba(255, 255, 255, 0.05)",
),
css=custom_css,
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
]
) |