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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def calculator(num1, operation, num2):
|
| 4 |
+
"""Perform basic arithmetic operations."""
|
| 5 |
+
if operation == "โ Add":
|
| 6 |
+
return num1 + num2
|
| 7 |
+
elif operation == "โ Subtract":
|
| 8 |
+
return num1 - num2
|
| 9 |
+
elif operation == "โ๏ธ Multiply":
|
| 10 |
+
return num1 * num2
|
| 11 |
+
elif operation == "โ Divide":
|
| 12 |
+
if num2 == 0:
|
| 13 |
+
raise gr.Error("Cannot divide by zero!")
|
| 14 |
+
return num1 / num2
|
| 15 |
+
|
| 16 |
+
# Custom CSS for modern, mobile-friendly design
|
| 17 |
+
custom_css = """
|
| 18 |
+
/* Container styling */
|
| 19 |
+
.gradio-container {
|
| 20 |
+
max-width: 500px !important;
|
| 21 |
+
margin: 0 auto !important;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
/* Header styling */
|
| 25 |
+
.header-container {
|
| 26 |
+
text-align: center;
|
| 27 |
+
padding: 1.5rem 0 1rem 0;
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
.header-container h1 {
|
| 31 |
+
font-size: 2rem;
|
| 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 |
+
/* Input/Output styling */
|
| 47 |
+
.input-group {
|
| 48 |
+
background: rgba(255, 255, 255, 0.05);
|
| 49 |
+
border-radius: 1rem;
|
| 50 |
+
padding: 1.5rem;
|
| 51 |
+
margin-bottom: 1rem;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
/* Button styling */
|
| 55 |
+
.calculate-btn button {
|
| 56 |
+
width: 100%;
|
| 57 |
+
height: 3.5rem;
|
| 58 |
+
font-size: 1.125rem;
|
| 59 |
+
font-weight: 600;
|
| 60 |
+
border-radius: 0.75rem;
|
| 61 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 62 |
+
transition: all 0.3s ease;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
.calculate-btn button:hover {
|
| 66 |
+
transform: translateY(-2px);
|
| 67 |
+
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
/* Result styling */
|
| 71 |
+
.result-display {
|
| 72 |
+
background: linear-gradient(135deg, rgba(102, 126, 234, 0.1) 0%, rgba(118, 75, 162, 0.1) 100%);
|
| 73 |
+
border-radius: 1rem;
|
| 74 |
+
padding: 1rem;
|
| 75 |
+
margin: 1rem 0;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
/* Radio button styling */
|
| 79 |
+
.radio-group {
|
| 80 |
+
display: grid !important;
|
| 81 |
+
grid-template-columns: repeat(2, 1fr) !important;
|
| 82 |
+
gap: 0.75rem !important;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
/* Examples styling */
|
| 86 |
+
.examples-container {
|
| 87 |
+
margin-top: 1.5rem;
|
| 88 |
+
padding-top: 1.5rem;
|
| 89 |
+
border-top: 1px solid rgba(255, 255, 255, 0.1);
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
/* Mobile responsive */
|
| 93 |
+
@media (max-width: 640px) {
|
| 94 |
+
.header-container h1 {
|
| 95 |
+
font-size: 1.75rem;
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
.input-group {
|
| 99 |
+
padding: 1rem;
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
.calculate-btn button {
|
| 103 |
+
height: 3rem;
|
| 104 |
+
font-size: 1rem;
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
# Create the Gradio app
|
| 110 |
+
with gr.Blocks(fill_height=False) as demo:
|
| 111 |
+
# Header
|
| 112 |
+
with gr.Column(elem_classes="header-container"):
|
| 113 |
+
gr.Markdown("# ๐งฎ Calculator")
|
| 114 |
+
gr.Markdown(
|
| 115 |
+
"Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)",
|
| 116 |
+
elem_classes="header-link"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Input Section
|
| 120 |
+
with gr.Column(elem_classes="input-group"):
|
| 121 |
+
num1 = gr.Number(
|
| 122 |
+
label="First Number",
|
| 123 |
+
placeholder="0",
|
| 124 |
+
value=0,
|
| 125 |
+
container=True,
|
| 126 |
+
scale=1
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
operation = gr.Radio(
|
| 130 |
+
choices=["โ Add", "โ Subtract", "โ๏ธ Multiply", "โ Divide"],
|
| 131 |
+
value="โ Add",
|
| 132 |
+
label="Operation",
|
| 133 |
+
elem_classes="radio-group"
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
num2 = gr.Number(
|
| 137 |
+
label="Second Number",
|
| 138 |
+
placeholder="0",
|
| 139 |
+
value=0,
|
| 140 |
+
container=True,
|
| 141 |
+
scale=1
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Calculate Button
|
| 145 |
+
with gr.Column(elem_classes="calculate-btn"):
|
| 146 |
+
submit_btn = gr.Button(
|
| 147 |
+
"Calculate",
|
| 148 |
+
variant="primary",
|
| 149 |
+
size="lg"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
# Result Section
|
| 153 |
+
with gr.Column(elem_classes="result-display"):
|
| 154 |
+
result = gr.Number(
|
| 155 |
+
label="Result",
|
| 156 |
+
interactive=False,
|
| 157 |
+
container=True,
|
| 158 |
+
scale=1
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
# Examples Section
|
| 162 |
+
with gr.Column(elem_classes="examples-container"):
|
| 163 |
+
gr.Markdown("### Quick Examples")
|
| 164 |
+
examples = gr.Examples(
|
| 165 |
+
examples=[
|
| 166 |
+
[45, "โ Add", 3],
|
| 167 |
+
[100, "โ Subtract", 25],
|
| 168 |
+
[12, "โ๏ธ Multiply", 8],
|
| 169 |
+
[144, "โ Divide", 12],
|
| 170 |
+
],
|
| 171 |
+
inputs=[num1, operation, num2],
|
| 172 |
+
outputs=[result],
|
| 173 |
+
fn=calculator,
|
| 174 |
+
cache_examples=False
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# Event handlers
|
| 178 |
+
submit_btn.click(
|
| 179 |
+
fn=calculator,
|
| 180 |
+
inputs=[num1, operation, num2],
|
| 181 |
+
outputs=[result],
|
| 182 |
+
api_visibility="public"
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
# Also trigger on Enter key
|
| 186 |
+
num1.submit(
|
| 187 |
+
fn=calculator,
|
| 188 |
+
inputs=[num1, operation, num2],
|
| 189 |
+
outputs=[result]
|
| 190 |
+
)
|
| 191 |
+
|
| 192 |
+
num2.submit(
|
| 193 |
+
fn=calculator,
|
| 194 |
+
inputs=[num1, operation, num2],
|
| 195 |
+
outputs=[result]
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
if __name__ == "__main__":
|
| 199 |
+
demo.launch(
|
| 200 |
+
theme=gr.themes.Soft(
|
| 201 |
+
primary_hue="indigo",
|
| 202 |
+
secondary_hue="purple",
|
| 203 |
+
neutral_hue="slate",
|
| 204 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 205 |
+
text_size="lg",
|
| 206 |
+
spacing_size="md",
|
| 207 |
+
radius_size="lg"
|
| 208 |
+
).set(
|
| 209 |
+
button_primary_background_fill="*primary_600",
|
| 210 |
+
button_primary_background_fill_hover="*primary_700",
|
| 211 |
+
block_title_text_weight="600",
|
| 212 |
+
block_label_text_weight="500",
|
| 213 |
+
),
|
| 214 |
+
css=custom_css,
|
| 215 |
+
footer_links=[
|
| 216 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 217 |
+
]
|
| 218 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=6.0
|
| 2 |
+
requests
|
| 3 |
+
Pillow
|