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