Spaces:
Sleeping
Sleeping
File size: 5,412 Bytes
195fc5c |
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 |
import gradio as gr
def calculate(num1, operation, num2):
"""Perform calculation based on selected operation."""
operations = {
"โ Add": lambda a, b: a + b,
"โ Subtract": lambda a, b: a - b,
"โ๏ธ Multiply": lambda a, b: a * b,
"โ Divide": lambda a, b: a / b if b != 0 else None
}
if operation == "โ Divide" and num2 == 0:
raise gr.Error("Cannot divide by zero!")
result = operations[operation](num1, num2)
return result
# Custom CSS for mobile-first, modern design
custom_css = """
/* Mobile-first responsive design */
.calculator-container {
max-width: 480px;
margin: 0 auto;
padding: 1rem;
}
.header-section {
text-align: center;
margin-bottom: 2rem;
}
.header-section h1 {
font-size: clamp(2rem, 5vw, 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;
}
.calculator-card {
background: rgba(255, 255, 255, 0.05);
border-radius: 1.5rem;
padding: 2rem 1.5rem;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
backdrop-filter: blur(10px);
}
/* Input styling */
.input-group {
margin-bottom: 1.5rem;
}
/* Button styling */
button.primary {
width: 100%;
padding: 1rem;
font-size: 1.125rem;
font-weight: 600;
border-radius: 0.75rem;
transition: all 0.3s ease;
}
button.primary:hover {
transform: translateY(-2px);
box-shadow: 0 8px 16px rgba(102, 126, 234, 0.4);
}
/* Result display */
.result-display {
font-size: 2rem;
font-weight: 700;
text-align: center;
padding: 1.5rem;
background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
border-radius: 1rem;
margin: 1.5rem 0;
}
/* Examples section */
.examples-section {
margin-top: 2rem;
padding-top: 2rem;
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
/* Mobile optimization */
@media (max-width: 640px) {
.calculator-card {
padding: 1.5rem 1rem;
}
.header-section h1 {
font-size: 1.75rem;
}
}
/* Smooth transitions */
* {
transition: all 0.2s ease;
}
"""
with gr.Blocks(fill_height=True) as demo:
gr.HTML("""
<div class="header-section">
<h1>๐งฎ Calculator</h1>
<p style="color: #888; font-size: 0.95rem;">
Simple & elegant calculator built with
<a href="https://huggingface.co/spaces/akhaliq/anycoder"
target="_blank"
style="color: #667eea; text-decoration: none; font-weight: 600;">
anycoder
</a>
</p>
</div>
""")
with gr.Column(elem_classes="calculator-container"):
# Input Section
num1 = gr.Number(
label="First Number",
value=0,
container=True,
scale=1
)
operation = gr.Radio(
choices=["โ Add", "โ Subtract", "โ๏ธ Multiply", "โ Divide"],
value="โ Add",
label="Operation",
container=True
)
num2 = gr.Number(
label="Second Number",
value=0,
container=True,
scale=1
)
# Calculate Button
calc_btn = gr.Button(
"Calculate",
variant="primary",
size="lg",
scale=1
)
# Result Display
result = gr.Number(
label="Result",
interactive=False,
container=True,
scale=1,
show_label=True
)
# Examples Section
gr.Markdown("### Quick Examples", elem_classes="examples-header")
gr.Examples(
examples=[
[100, "โ Add", 50],
[75, "โ Subtract", 25],
[12, "โ๏ธ Multiply", 8],
[144, "โ Divide", 12],
],
inputs=[num1, operation, num2],
outputs=result,
fn=calculate,
cache_examples=False,
label=None
)
# Event Handlers
calc_btn.click(
fn=calculate,
inputs=[num1, operation, num2],
outputs=result,
api_visibility="public",
api_name="calculate"
)
# Also trigger on Enter key
num2.submit(
fn=calculate,
inputs=[num1, operation, num2],
outputs=result
)
if __name__ == "__main__":
demo.launch(
theme=gr.themes.Soft(
primary_hue="indigo",
secondary_hue="purple",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="lg"
).set(
button_primary_background_fill="linear-gradient(135deg, #667eea 0%, #764ba2 100%)",
button_primary_background_fill_hover="linear-gradient(135deg, #5568d3 0%, #6a3f8f 100%)",
block_title_text_weight="600",
block_label_text_weight="500",
),
css=custom_css,
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
]
) |