Spaces:
Sleeping
Sleeping
File size: 7,091 Bytes
0be3d56 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 0be3d56 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 716b385 0be3d56 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 9b29538 0be3d56 dcb6f38 716b385 dcb6f38 716b385 dcb6f38 9b29538 716b385 dcb6f38 9b29538 716b385 dcb6f38 716b385 dcb6f38 0be3d56 dcb6f38 716b385 9b29538 dcb6f38 9b29538 716b385 dcb6f38 716b385 dcb6f38 9b29538 716b385 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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
import gradio as gr
def calculate(num1, operation, num2):
"""Perform basic arithmetic operations."""
try:
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
except Exception as e:
raise gr.Error(f"Calculation error: {str(e)}")
# Modern, minimal CSS
custom_css = """
/* Global container */
.gradio-container {
max-width: 420px !important;
margin: 0 auto !important;
padding: 1.5rem 1rem !important;
}
/* Header */
.calc-header {
text-align: center;
margin-bottom: 2rem !important;
}
.calc-title {
font-size: 2rem !important;
font-weight: 700 !important;
margin-bottom: 0.5rem !important;
color: #1e293b !important;
}
.calc-subtitle {
font-size: 0.875rem !important;
color: #64748b !important;
}
/* Calculator card */
.calc-card {
background: #ffffff !important;
border-radius: 20px !important;
padding: 1.5rem !important;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06) !important;
}
/* Input styling */
.calc-input label {
font-size: 0.875rem !important;
font-weight: 600 !important;
color: #475569 !important;
margin-bottom: 0.5rem !important;
}
.calc-input input {
font-size: 1.25rem !important;
text-align: center !important;
border-radius: 12px !important;
border: 2px solid #e2e8f0 !important;
transition: all 0.2s ease !important;
}
.calc-input input:focus {
border-color: #6366f1 !important;
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1) !important;
}
/* Operation buttons */
.operation-btns {
display: grid !important;
grid-template-columns: repeat(4, 1fr) !important;
gap: 0.75rem !important;
margin: 1.5rem 0 !important;
}
.operation-btns button {
height: 60px !important;
font-size: 1.5rem !important;
border-radius: 12px !important;
border: 2px solid #e2e8f0 !important;
background: #ffffff !important;
color: #475569 !important;
font-weight: 600 !important;
transition: all 0.2s ease !important;
cursor: pointer !important;
}
.operation-btns button:hover {
background: #f8fafc !important;
border-color: #6366f1 !important;
transform: translateY(-2px) !important;
}
.operation-btns button.selected {
background: #6366f1 !important;
color: white !important;
border-color: #6366f1 !important;
}
/* Result display */
.result-box {
background: linear-gradient(135deg, #667eea15 0%, #764ba215 100%) !important;
border-radius: 16px !important;
padding: 1.5rem !important;
margin-top: 1.5rem !important;
border: 2px solid #e0e7ff !important;
}
.result-box label {
font-size: 0.875rem !important;
font-weight: 600 !important;
color: #4338ca !important;
text-transform: uppercase !important;
letter-spacing: 0.05em !important;
}
.result-box input {
font-size: 2rem !important;
font-weight: 700 !important;
text-align: center !important;
color: #4338ca !important;
background: transparent !important;
border: none !important;
}
/* Examples */
.examples-section {
margin-top: 2rem !important;
padding-top: 1.5rem !important;
border-top: 1px solid #e2e8f0 !important;
}
.examples-title {
font-size: 0.875rem !important;
font-weight: 600 !important;
color: #64748b !important;
text-align: center !important;
margin-bottom: 1rem !important;
}
/* Mobile optimization */
@media (max-width: 480px) {
.gradio-container {
padding: 1rem 0.75rem !important;
}
.calc-title {
font-size: 1.75rem !important;
}
.calc-card {
padding: 1.25rem !important;
}
.operation-btns button {
height: 55px !important;
font-size: 1.25rem !important;
}
.result-box input {
font-size: 1.75rem !important;
}
}
/* Footer link styling */
footer a {
color: #6366f1 !important;
font-weight: 600 !important;
}
"""
# Build the app with minimal components
with gr.Blocks() as demo:
# Header
with gr.Group(elem_classes="calc-header"):
gr.Markdown("# ๐งฎ Calculator", elem_classes="calc-title")
gr.Markdown(
"Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)",
elem_classes="calc-subtitle"
)
# Calculator card
with gr.Group(elem_classes="calc-card"):
# First number
num1 = gr.Number(
label="First Number",
value=0,
elem_classes="calc-input"
)
# Operation selector (simplified to radio)
operation = gr.Radio(
choices=[
("โ", "add"),
("โ", "subtract"),
("โ๏ธ", "multiply"),
("โ", "divide")
],
value="add",
label="Operation",
elem_classes="operation-btns"
)
# Second number
num2 = gr.Number(
label="Second Number",
value=0,
elem_classes="calc-input"
)
# Result display
result = gr.Number(
label="Result",
interactive=False,
elem_classes="result-box"
)
# Examples section
with gr.Group(elem_classes="examples-section"):
gr.Markdown("**Try these examples**", elem_classes="examples-title")
gr.Examples(
examples=[
[45, "add", 3],
[100, "subtract", 25],
[12, "multiply", 8],
[144, "divide", 12],
],
inputs=[num1, operation, num2],
outputs=[result],
fn=calculate,
cache_examples=False
)
# Auto-calculate on any input change
for component in [num1, num2, operation]:
component.change(
fn=calculate,
inputs=[num1, operation, num2],
outputs=[result],
api_visibility="public"
)
if __name__ == "__main__":
demo.launch(
theme=gr.themes.Soft(
primary_hue="indigo",
secondary_hue="blue",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="md",
spacing_size="md",
radius_size="lg"
).set(
body_background_fill="#f8fafc",
block_title_text_weight="600",
block_label_text_weight="600",
button_primary_background_fill="#6366f1",
button_primary_background_fill_hover="#4f46e5",
button_primary_text_color="white",
),
css=custom_css,
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
]
) |