Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,216 +3,305 @@ import gradio as gr
|
|
| 3 |
|
| 4 |
|
| 5 |
# ============================================================
|
| 6 |
-
#
|
| 7 |
# ============================================================
|
| 8 |
|
| 9 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
"""
|
| 11 |
Perform the selected calculator operation.
|
| 12 |
"""
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
|
|
|
| 19 |
num1 = float(num1)
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
num2 = float(num2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
result = num1 + num2
|
| 31 |
|
|
|
|
| 32 |
elif operation == "Subtraction (-)":
|
| 33 |
-
if num2 is None or str(num2).strip() == "":
|
| 34 |
-
return "Please enter the second number.", ""
|
| 35 |
-
|
| 36 |
-
num2 = float(num2)
|
| 37 |
result = num1 - num2
|
| 38 |
|
|
|
|
| 39 |
elif operation == "Multiplication (×)":
|
| 40 |
-
if num2 is None or str(num2).strip() == "":
|
| 41 |
-
return "Please enter the second number.", ""
|
| 42 |
-
|
| 43 |
-
num2 = float(num2)
|
| 44 |
result = num1 * num2
|
| 45 |
|
|
|
|
| 46 |
elif operation == "Division (÷)":
|
| 47 |
-
if num2 is None or str(num2).strip() == "":
|
| 48 |
-
return "Please enter the second number.", ""
|
| 49 |
-
|
| 50 |
-
num2 = float(num2)
|
| 51 |
|
| 52 |
if num2 == 0:
|
| 53 |
return "Error: Cannot divide by zero.", ""
|
| 54 |
|
| 55 |
result = num1 / num2
|
| 56 |
|
|
|
|
| 57 |
elif operation == "Power (xʸ)":
|
| 58 |
-
if num2 is None or str(num2).strip() == "":
|
| 59 |
-
return "Please enter the exponent.", ""
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
# Scientific Functions
|
| 66 |
-
# ----------------------------------------------------
|
| 67 |
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
return "Error: Logarithm is defined only for positive numbers.", ""
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
return "Error: Natural logarithm is defined only for positive numbers.", ""
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
-
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
|
| 99 |
-
|
| 100 |
-
if math.isclose(
|
| 101 |
-
math.cos(math.radians(num1)),
|
| 102 |
-
0,
|
| 103 |
-
abs_tol=1e-12
|
| 104 |
-
):
|
| 105 |
-
return "Error: Tangent is undefined at this angle.", ""
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
|
| 114 |
-
|
| 115 |
-
return "Error: Factorial requires a whole number.", ""
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
|
| 122 |
-
|
| 123 |
-
return "Please select a valid operation.", ""
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
-
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
"Subtraction (-)",
|
| 135 |
-
"Multiplication (×)",
|
| 136 |
-
"Division (÷)",
|
| 137 |
-
"Power (xʸ)"
|
| 138 |
-
]:
|
| 139 |
-
symbol = {
|
| 140 |
-
"Addition (+)": "+",
|
| 141 |
-
"Subtraction (-)": "-",
|
| 142 |
-
"Multiplication (×)": "×",
|
| 143 |
-
"Division (÷)": "÷",
|
| 144 |
-
"Power (xʸ)": "^"
|
| 145 |
-
}[operation]
|
| 146 |
|
| 147 |
-
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
history = f"ln({format_number(num1)}) = {formatted_result}"
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
history = f"cos({format_number(num1)}°) = {formatted_result}"
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
|
|
|
| 166 |
|
| 167 |
-
|
| 168 |
-
history = f"{format_number(num1)}! = {formatted_result}"
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
|
| 176 |
-
return "Error: Please enter valid numbers.", ""
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
-
return "Error: Cannot divide by zero.", ""
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
|
|
|
| 186 |
|
|
|
|
| 187 |
|
| 188 |
-
|
| 189 |
-
"""
|
| 190 |
-
Format calculator results for a clean UI.
|
| 191 |
-
"""
|
| 192 |
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
|
| 197 |
-
return str(int(value))
|
| 198 |
|
| 199 |
-
|
|
|
|
| 200 |
|
|
|
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
|
| 207 |
-
|
| 208 |
-
return str(int(value))
|
| 209 |
|
| 210 |
-
|
|
|
|
|
|
|
| 211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
"""
|
| 215 |
-
Show
|
|
|
|
| 216 |
"""
|
| 217 |
|
| 218 |
binary_operations = [
|
|
@@ -224,6 +313,7 @@ def update_second_input(operation):
|
|
| 224 |
]
|
| 225 |
|
| 226 |
if operation in binary_operations:
|
|
|
|
| 227 |
return gr.update(
|
| 228 |
visible=True,
|
| 229 |
label="Second Number",
|
|
@@ -237,9 +327,7 @@ def update_second_input(operation):
|
|
| 237 |
|
| 238 |
|
| 239 |
def clear_calculator():
|
| 240 |
-
"""
|
| 241 |
-
Reset the calculator.
|
| 242 |
-
"""
|
| 243 |
|
| 244 |
return (
|
| 245 |
None,
|
|
@@ -254,8 +342,8 @@ def clear_calculator():
|
|
| 254 |
# Custom CSS
|
| 255 |
# ============================================================
|
| 256 |
|
| 257 |
-
|
| 258 |
-
/* Main
|
| 259 |
.gradio-container {
|
| 260 |
max-width: 1100px !important;
|
| 261 |
margin: auto !important;
|
|
@@ -264,24 +352,24 @@ css = """
|
|
| 264 |
/* Header */
|
| 265 |
.calculator-header {
|
| 266 |
text-align: center;
|
| 267 |
-
padding:
|
| 268 |
}
|
| 269 |
|
| 270 |
.calculator-title {
|
| 271 |
-
font-size:
|
| 272 |
font-weight: 800;
|
| 273 |
-
margin-bottom:
|
| 274 |
}
|
| 275 |
|
| 276 |
.calculator-subtitle {
|
| 277 |
font-size: 16px;
|
| 278 |
-
opacity: 0.
|
| 279 |
}
|
| 280 |
|
| 281 |
-
/*
|
| 282 |
.calc-card {
|
| 283 |
border-radius: 18px !important;
|
| 284 |
-
padding:
|
| 285 |
}
|
| 286 |
|
| 287 |
/* Result */
|
|
@@ -293,16 +381,17 @@ css = """
|
|
| 293 |
|
| 294 |
/* Buttons */
|
| 295 |
.calculate-btn {
|
| 296 |
-
font-size: 18px !important;
|
| 297 |
-
font-weight: 700 !important;
|
| 298 |
min-height: 52px !important;
|
|
|
|
|
|
|
| 299 |
}
|
| 300 |
|
| 301 |
.clear-btn {
|
| 302 |
min-height: 52px !important;
|
|
|
|
| 303 |
}
|
| 304 |
|
| 305 |
-
/* Operation
|
| 306 |
.operation-dropdown {
|
| 307 |
font-weight: 600 !important;
|
| 308 |
}
|
|
@@ -310,13 +399,13 @@ css = """
|
|
| 310 |
/* Footer */
|
| 311 |
.calculator-footer {
|
| 312 |
text-align: center;
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
opacity: 0.
|
| 316 |
font-size: 13px;
|
| 317 |
}
|
| 318 |
|
| 319 |
-
/* Mobile
|
| 320 |
@media (max-width: 700px) {
|
| 321 |
|
| 322 |
.calculator-title {
|
|
@@ -339,13 +428,11 @@ css = """
|
|
| 339 |
|
| 340 |
|
| 341 |
# ============================================================
|
| 342 |
-
# Gradio
|
| 343 |
# ============================================================
|
| 344 |
|
| 345 |
with gr.Blocks(
|
| 346 |
-
title="Scientific Calculator"
|
| 347 |
-
theme=gr.themes.Soft(),
|
| 348 |
-
css=css
|
| 349 |
) as app:
|
| 350 |
|
| 351 |
# --------------------------------------------------------
|
|
@@ -355,25 +442,30 @@ with gr.Blocks(
|
|
| 355 |
gr.HTML(
|
| 356 |
"""
|
| 357 |
<div class="calculator-header">
|
|
|
|
| 358 |
<div class="calculator-title">
|
| 359 |
Scientific Calculator
|
| 360 |
</div>
|
| 361 |
|
| 362 |
<div class="calculator-subtitle">
|
| 363 |
-
|
| 364 |
-
|
| 365 |
</div>
|
|
|
|
| 366 |
</div>
|
| 367 |
"""
|
| 368 |
)
|
| 369 |
|
| 370 |
# --------------------------------------------------------
|
| 371 |
-
# Calculator
|
| 372 |
# --------------------------------------------------------
|
| 373 |
|
| 374 |
with gr.Row():
|
| 375 |
|
| 376 |
-
#
|
|
|
|
|
|
|
|
|
|
| 377 |
with gr.Column(
|
| 378 |
scale=1,
|
| 379 |
elem_classes="calc-card"
|
|
@@ -411,19 +503,22 @@ with gr.Blocks(
|
|
| 411 |
|
| 412 |
with gr.Row():
|
| 413 |
|
| 414 |
-
|
| 415 |
"Calculate",
|
| 416 |
variant="primary",
|
| 417 |
elem_classes="calculate-btn"
|
| 418 |
)
|
| 419 |
|
| 420 |
-
|
| 421 |
"Clear",
|
| 422 |
variant="secondary",
|
| 423 |
elem_classes="clear-btn"
|
| 424 |
)
|
| 425 |
|
| 426 |
-
#
|
|
|
|
|
|
|
|
|
|
| 427 |
with gr.Column(
|
| 428 |
scale=1,
|
| 429 |
elem_classes="calc-card"
|
|
@@ -436,36 +531,40 @@ with gr.Blocks(
|
|
| 436 |
elem_classes="result-box"
|
| 437 |
)
|
| 438 |
|
| 439 |
-
|
| 440 |
label="Calculation",
|
| 441 |
value="",
|
| 442 |
interactive=False,
|
| 443 |
-
lines=
|
| 444 |
placeholder="Your calculation will appear here..."
|
| 445 |
)
|
| 446 |
|
| 447 |
gr.Markdown(
|
| 448 |
"""
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
)
|
| 470 |
|
| 471 |
# --------------------------------------------------------
|
|
@@ -475,28 +574,35 @@ with gr.Blocks(
|
|
| 475 |
gr.HTML(
|
| 476 |
"""
|
| 477 |
<div class="calculator-footer">
|
| 478 |
-
Scientific Calculator
|
| 479 |
</div>
|
| 480 |
"""
|
| 481 |
)
|
| 482 |
|
| 483 |
-
#
|
| 484 |
-
#
|
| 485 |
-
#
|
| 486 |
|
| 487 |
operation.change(
|
| 488 |
-
fn=
|
| 489 |
inputs=operation,
|
| 490 |
outputs=num2
|
| 491 |
)
|
| 492 |
|
| 493 |
-
|
| 494 |
fn=calculate,
|
| 495 |
-
inputs=[
|
| 496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
)
|
| 498 |
|
| 499 |
-
|
| 500 |
fn=clear_calculator,
|
| 501 |
inputs=[],
|
| 502 |
outputs=[
|
|
@@ -504,14 +610,18 @@ with gr.Blocks(
|
|
| 504 |
num2,
|
| 505 |
operation,
|
| 506 |
result,
|
| 507 |
-
|
| 508 |
]
|
| 509 |
)
|
| 510 |
|
| 511 |
|
| 512 |
# ============================================================
|
| 513 |
-
# Launch
|
| 514 |
# ============================================================
|
| 515 |
|
| 516 |
if __name__ == "__main__":
|
| 517 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
# ============================================================
|
| 6 |
+
# Helper Functions
|
| 7 |
# ============================================================
|
| 8 |
|
| 9 |
+
def format_number(value):
|
| 10 |
+
"""Format a number for clean display."""
|
| 11 |
+
|
| 12 |
+
if isinstance(value, int):
|
| 13 |
+
return str(value)
|
| 14 |
+
|
| 15 |
+
if isinstance(value, float):
|
| 16 |
+
if not math.isfinite(value):
|
| 17 |
+
return str(value)
|
| 18 |
+
|
| 19 |
+
if value.is_integer():
|
| 20 |
+
return str(int(value))
|
| 21 |
+
|
| 22 |
+
return f"{value:.12g}"
|
| 23 |
+
|
| 24 |
+
return str(value)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def calculate(operation, num1, num2):
|
| 28 |
"""
|
| 29 |
Perform the selected calculator operation.
|
| 30 |
"""
|
| 31 |
|
| 32 |
+
# --------------------------------------------------------
|
| 33 |
+
# Validate first number
|
| 34 |
+
# --------------------------------------------------------
|
| 35 |
+
|
| 36 |
+
if num1 is None:
|
| 37 |
+
return "Please enter a number.", ""
|
| 38 |
|
| 39 |
+
try:
|
| 40 |
num1 = float(num1)
|
| 41 |
+
except (ValueError, TypeError):
|
| 42 |
+
return "Error: Please enter a valid first number.", ""
|
| 43 |
|
| 44 |
+
# --------------------------------------------------------
|
| 45 |
+
# Binary Operations
|
| 46 |
+
# --------------------------------------------------------
|
| 47 |
|
| 48 |
+
binary_operations = {
|
| 49 |
+
"Addition (+)": "+",
|
| 50 |
+
"Subtraction (-)": "-",
|
| 51 |
+
"Multiplication (×)": "×",
|
| 52 |
+
"Division (÷)": "÷",
|
| 53 |
+
"Power (xʸ)": "^"
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
if operation in binary_operations:
|
| 57 |
|
| 58 |
+
if num2 is None:
|
| 59 |
+
return "Please enter the second number.", ""
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
num2 = float(num2)
|
| 63 |
+
except (ValueError, TypeError):
|
| 64 |
+
return "Error: Please enter a valid second number.", ""
|
| 65 |
+
|
| 66 |
+
# Addition
|
| 67 |
+
if operation == "Addition (+)":
|
| 68 |
result = num1 + num2
|
| 69 |
|
| 70 |
+
# Subtraction
|
| 71 |
elif operation == "Subtraction (-)":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
result = num1 - num2
|
| 73 |
|
| 74 |
+
# Multiplication
|
| 75 |
elif operation == "Multiplication (×)":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
result = num1 * num2
|
| 77 |
|
| 78 |
+
# Division
|
| 79 |
elif operation == "Division (÷)":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
if num2 == 0:
|
| 82 |
return "Error: Cannot divide by zero.", ""
|
| 83 |
|
| 84 |
result = num1 / num2
|
| 85 |
|
| 86 |
+
# Power
|
| 87 |
elif operation == "Power (xʸ)":
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
try:
|
| 90 |
+
result = num1 ** num2
|
| 91 |
+
except OverflowError:
|
| 92 |
+
return "Error: Result is too large.", ""
|
| 93 |
|
| 94 |
+
symbol = binary_operations[operation]
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
result_text = format_number(result)
|
| 97 |
|
| 98 |
+
calculation = (
|
| 99 |
+
f"{format_number(num1)} "
|
| 100 |
+
f"{symbol} "
|
| 101 |
+
f"{format_number(num2)} = "
|
| 102 |
+
f"{result_text}"
|
| 103 |
+
)
|
| 104 |
|
| 105 |
+
return result_text, calculation
|
| 106 |
|
| 107 |
+
# --------------------------------------------------------
|
| 108 |
+
# Square Root
|
| 109 |
+
# --------------------------------------------------------
|
| 110 |
|
| 111 |
+
if operation == "Square Root (√x)":
|
|
|
|
| 112 |
|
| 113 |
+
if num1 < 0:
|
| 114 |
+
return (
|
| 115 |
+
"Error: Square root is not defined "
|
| 116 |
+
"for negative numbers.",
|
| 117 |
+
""
|
| 118 |
+
)
|
| 119 |
|
| 120 |
+
result = math.sqrt(num1)
|
| 121 |
|
| 122 |
+
result_text = format_number(result)
|
|
|
|
| 123 |
|
| 124 |
+
calculation = (
|
| 125 |
+
f"√{format_number(num1)} = {result_text}"
|
| 126 |
+
)
|
| 127 |
|
| 128 |
+
return result_text, calculation
|
| 129 |
|
| 130 |
+
# --------------------------------------------------------
|
| 131 |
+
# Logarithm
|
| 132 |
+
# --------------------------------------------------------
|
| 133 |
|
| 134 |
+
if operation == "Logarithm (log₁₀)":
|
| 135 |
|
| 136 |
+
if num1 <= 0:
|
| 137 |
+
return (
|
| 138 |
+
"Error: Logarithm is defined only "
|
| 139 |
+
"for positive numbers.",
|
| 140 |
+
""
|
| 141 |
+
)
|
| 142 |
|
| 143 |
+
result = math.log10(num1)
|
| 144 |
|
| 145 |
+
result_text = format_number(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
+
calculation = (
|
| 148 |
+
f"log₁₀({format_number(num1)}) = "
|
| 149 |
+
f"{result_text}"
|
| 150 |
+
)
|
| 151 |
|
| 152 |
+
return result_text, calculation
|
| 153 |
|
| 154 |
+
# --------------------------------------------------------
|
| 155 |
+
# Natural Logarithm
|
| 156 |
+
# --------------------------------------------------------
|
| 157 |
|
| 158 |
+
if operation == "Natural Log (ln)":
|
|
|
|
| 159 |
|
| 160 |
+
if num1 <= 0:
|
| 161 |
+
return (
|
| 162 |
+
"Error: Natural logarithm is defined "
|
| 163 |
+
"only for positive numbers.",
|
| 164 |
+
""
|
| 165 |
+
)
|
| 166 |
|
| 167 |
+
result = math.log(num1)
|
| 168 |
|
| 169 |
+
result_text = format_number(result)
|
|
|
|
| 170 |
|
| 171 |
+
calculation = (
|
| 172 |
+
f"ln({format_number(num1)}) = "
|
| 173 |
+
f"{result_text}"
|
| 174 |
+
)
|
| 175 |
|
| 176 |
+
return result_text, calculation
|
| 177 |
|
| 178 |
+
# --------------------------------------------------------
|
| 179 |
+
# Sine
|
| 180 |
+
# --------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
if operation == "Sine (sin)":
|
| 183 |
|
| 184 |
+
radians = math.radians(num1)
|
| 185 |
+
result = math.sin(radians)
|
| 186 |
|
| 187 |
+
# Avoid values such as 6.123e-17 for sin(180)
|
| 188 |
+
if abs(result) < 1e-12:
|
| 189 |
+
result = 0.0
|
| 190 |
|
| 191 |
+
result_text = format_number(result)
|
|
|
|
| 192 |
|
| 193 |
+
calculation = (
|
| 194 |
+
f"sin({format_number(num1)}°) = "
|
| 195 |
+
f"{result_text}"
|
| 196 |
+
)
|
| 197 |
|
| 198 |
+
return result_text, calculation
|
|
|
|
| 199 |
|
| 200 |
+
# --------------------------------------------------------
|
| 201 |
+
# Cosine
|
| 202 |
+
# --------------------------------------------------------
|
| 203 |
|
| 204 |
+
if operation == "Cosine (cos)":
|
|
|
|
| 205 |
|
| 206 |
+
radians = math.radians(num1)
|
| 207 |
+
result = math.cos(radians)
|
| 208 |
|
| 209 |
+
# Avoid values such as 6.123e-17 for cos(90)
|
| 210 |
+
if abs(result) < 1e-12:
|
| 211 |
+
result = 0.0
|
| 212 |
|
| 213 |
+
result_text = format_number(result)
|
|
|
|
| 214 |
|
| 215 |
+
calculation = (
|
| 216 |
+
f"cos({format_number(num1)}°) = "
|
| 217 |
+
f"{result_text}"
|
| 218 |
+
)
|
| 219 |
|
| 220 |
+
return result_text, calculation
|
|
|
|
| 221 |
|
| 222 |
+
# --------------------------------------------------------
|
| 223 |
+
# Tangent
|
| 224 |
+
# --------------------------------------------------------
|
| 225 |
|
| 226 |
+
if operation == "Tangent (tan)":
|
| 227 |
|
| 228 |
+
radians = math.radians(num1)
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
+
# Tangent is undefined when cos(x) = 0
|
| 231 |
+
if abs(math.cos(radians)) < 1e-12:
|
| 232 |
+
return (
|
| 233 |
+
"Error: Tangent is undefined "
|
| 234 |
+
"at this angle.",
|
| 235 |
+
""
|
| 236 |
+
)
|
| 237 |
|
| 238 |
+
result = math.tan(radians)
|
|
|
|
| 239 |
|
| 240 |
+
if abs(result) < 1e-12:
|
| 241 |
+
result = 0.0
|
| 242 |
|
| 243 |
+
result_text = format_number(result)
|
| 244 |
|
| 245 |
+
calculation = (
|
| 246 |
+
f"tan({format_number(num1)}°) = "
|
| 247 |
+
f"{result_text}"
|
| 248 |
+
)
|
| 249 |
|
| 250 |
+
return result_text, calculation
|
|
|
|
| 251 |
|
| 252 |
+
# --------------------------------------------------------
|
| 253 |
+
# Factorial
|
| 254 |
+
# --------------------------------------------------------
|
| 255 |
|
| 256 |
+
if operation == "Factorial (n!)":
|
| 257 |
+
|
| 258 |
+
if num1 < 0:
|
| 259 |
+
return (
|
| 260 |
+
"Error: Factorial is not defined "
|
| 261 |
+
"for negative numbers.",
|
| 262 |
+
""
|
| 263 |
+
)
|
| 264 |
|
| 265 |
+
if not num1.is_integer():
|
| 266 |
+
return (
|
| 267 |
+
"Error: Factorial requires "
|
| 268 |
+
"a whole number.",
|
| 269 |
+
""
|
| 270 |
+
)
|
| 271 |
+
|
| 272 |
+
if num1 > 170:
|
| 273 |
+
return (
|
| 274 |
+
"Error: Number is too large "
|
| 275 |
+
"for factorial calculation.",
|
| 276 |
+
""
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
result = math.factorial(int(num1))
|
| 280 |
+
|
| 281 |
+
result_text = format_number(result)
|
| 282 |
+
|
| 283 |
+
calculation = (
|
| 284 |
+
f"{format_number(num1)}! = "
|
| 285 |
+
f"{result_text}"
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
return result_text, calculation
|
| 289 |
+
|
| 290 |
+
# --------------------------------------------------------
|
| 291 |
+
# Invalid Operation
|
| 292 |
+
# --------------------------------------------------------
|
| 293 |
+
|
| 294 |
+
return "Please select a valid operation.", ""
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
# ============================================================
|
| 298 |
+
# UI Functions
|
| 299 |
+
# ============================================================
|
| 300 |
+
|
| 301 |
+
def update_second_number(operation):
|
| 302 |
"""
|
| 303 |
+
Show the second number input only for
|
| 304 |
+
operations that require two numbers.
|
| 305 |
"""
|
| 306 |
|
| 307 |
binary_operations = [
|
|
|
|
| 313 |
]
|
| 314 |
|
| 315 |
if operation in binary_operations:
|
| 316 |
+
|
| 317 |
return gr.update(
|
| 318 |
visible=True,
|
| 319 |
label="Second Number",
|
|
|
|
| 327 |
|
| 328 |
|
| 329 |
def clear_calculator():
|
| 330 |
+
"""Reset all calculator fields."""
|
|
|
|
|
|
|
| 331 |
|
| 332 |
return (
|
| 333 |
None,
|
|
|
|
| 342 |
# Custom CSS
|
| 343 |
# ============================================================
|
| 344 |
|
| 345 |
+
custom_css = """
|
| 346 |
+
/* Main container */
|
| 347 |
.gradio-container {
|
| 348 |
max-width: 1100px !important;
|
| 349 |
margin: auto !important;
|
|
|
|
| 352 |
/* Header */
|
| 353 |
.calculator-header {
|
| 354 |
text-align: center;
|
| 355 |
+
padding: 25px 10px 15px 10px;
|
| 356 |
}
|
| 357 |
|
| 358 |
.calculator-title {
|
| 359 |
+
font-size: 38px;
|
| 360 |
font-weight: 800;
|
| 361 |
+
margin-bottom: 8px;
|
| 362 |
}
|
| 363 |
|
| 364 |
.calculator-subtitle {
|
| 365 |
font-size: 16px;
|
| 366 |
+
opacity: 0.7;
|
| 367 |
}
|
| 368 |
|
| 369 |
+
/* Calculator cards */
|
| 370 |
.calc-card {
|
| 371 |
border-radius: 18px !important;
|
| 372 |
+
padding: 22px !important;
|
| 373 |
}
|
| 374 |
|
| 375 |
/* Result */
|
|
|
|
| 381 |
|
| 382 |
/* Buttons */
|
| 383 |
.calculate-btn {
|
|
|
|
|
|
|
| 384 |
min-height: 52px !important;
|
| 385 |
+
font-size: 17px !important;
|
| 386 |
+
font-weight: 700 !important;
|
| 387 |
}
|
| 388 |
|
| 389 |
.clear-btn {
|
| 390 |
min-height: 52px !important;
|
| 391 |
+
font-size: 17px !important;
|
| 392 |
}
|
| 393 |
|
| 394 |
+
/* Operation selector */
|
| 395 |
.operation-dropdown {
|
| 396 |
font-weight: 600 !important;
|
| 397 |
}
|
|
|
|
| 399 |
/* Footer */
|
| 400 |
.calculator-footer {
|
| 401 |
text-align: center;
|
| 402 |
+
padding: 15px;
|
| 403 |
+
margin-top: 15px;
|
| 404 |
+
opacity: 0.6;
|
| 405 |
font-size: 13px;
|
| 406 |
}
|
| 407 |
|
| 408 |
+
/* Mobile */
|
| 409 |
@media (max-width: 700px) {
|
| 410 |
|
| 411 |
.calculator-title {
|
|
|
|
| 428 |
|
| 429 |
|
| 430 |
# ============================================================
|
| 431 |
+
# Gradio Application
|
| 432 |
# ============================================================
|
| 433 |
|
| 434 |
with gr.Blocks(
|
| 435 |
+
title="Scientific Calculator"
|
|
|
|
|
|
|
| 436 |
) as app:
|
| 437 |
|
| 438 |
# --------------------------------------------------------
|
|
|
|
| 442 |
gr.HTML(
|
| 443 |
"""
|
| 444 |
<div class="calculator-header">
|
| 445 |
+
|
| 446 |
<div class="calculator-title">
|
| 447 |
Scientific Calculator
|
| 448 |
</div>
|
| 449 |
|
| 450 |
<div class="calculator-subtitle">
|
| 451 |
+
A simple and powerful calculator for
|
| 452 |
+
arithmetic and scientific operations.
|
| 453 |
</div>
|
| 454 |
+
|
| 455 |
</div>
|
| 456 |
"""
|
| 457 |
)
|
| 458 |
|
| 459 |
# --------------------------------------------------------
|
| 460 |
+
# Main Calculator
|
| 461 |
# --------------------------------------------------------
|
| 462 |
|
| 463 |
with gr.Row():
|
| 464 |
|
| 465 |
+
# ====================================================
|
| 466 |
+
# Calculator Input Section
|
| 467 |
+
# ====================================================
|
| 468 |
+
|
| 469 |
with gr.Column(
|
| 470 |
scale=1,
|
| 471 |
elem_classes="calc-card"
|
|
|
|
| 503 |
|
| 504 |
with gr.Row():
|
| 505 |
|
| 506 |
+
calculate_button = gr.Button(
|
| 507 |
"Calculate",
|
| 508 |
variant="primary",
|
| 509 |
elem_classes="calculate-btn"
|
| 510 |
)
|
| 511 |
|
| 512 |
+
clear_button = gr.Button(
|
| 513 |
"Clear",
|
| 514 |
variant="secondary",
|
| 515 |
elem_classes="clear-btn"
|
| 516 |
)
|
| 517 |
|
| 518 |
+
# ====================================================
|
| 519 |
+
# Result Section
|
| 520 |
+
# ====================================================
|
| 521 |
+
|
| 522 |
with gr.Column(
|
| 523 |
scale=1,
|
| 524 |
elem_classes="calc-card"
|
|
|
|
| 531 |
elem_classes="result-box"
|
| 532 |
)
|
| 533 |
|
| 534 |
+
calculation = gr.Textbox(
|
| 535 |
label="Calculation",
|
| 536 |
value="",
|
| 537 |
interactive=False,
|
| 538 |
+
lines=2,
|
| 539 |
placeholder="Your calculation will appear here..."
|
| 540 |
)
|
| 541 |
|
| 542 |
gr.Markdown(
|
| 543 |
"""
|
| 544 |
+
### Supported Operations
|
| 545 |
+
|
| 546 |
+
**Arithmetic**
|
| 547 |
+
|
| 548 |
+
- Addition
|
| 549 |
+
- Subtraction
|
| 550 |
+
- Multiplication
|
| 551 |
+
- Division
|
| 552 |
+
- Power
|
| 553 |
+
|
| 554 |
+
**Scientific**
|
| 555 |
+
|
| 556 |
+
- Square Root
|
| 557 |
+
- Logarithm
|
| 558 |
+
- Natural Log
|
| 559 |
+
- Sine
|
| 560 |
+
- Cosine
|
| 561 |
+
- Tangent
|
| 562 |
+
- Factorial
|
| 563 |
+
|
| 564 |
+
**Trigonometry**
|
| 565 |
+
|
| 566 |
+
Angles are entered in **degrees**.
|
| 567 |
+
"""
|
| 568 |
)
|
| 569 |
|
| 570 |
# --------------------------------------------------------
|
|
|
|
| 574 |
gr.HTML(
|
| 575 |
"""
|
| 576 |
<div class="calculator-footer">
|
| 577 |
+
Scientific Calculator · Python + Gradio
|
| 578 |
</div>
|
| 579 |
"""
|
| 580 |
)
|
| 581 |
|
| 582 |
+
# ========================================================
|
| 583 |
+
# Event Handlers
|
| 584 |
+
# ========================================================
|
| 585 |
|
| 586 |
operation.change(
|
| 587 |
+
fn=update_second_number,
|
| 588 |
inputs=operation,
|
| 589 |
outputs=num2
|
| 590 |
)
|
| 591 |
|
| 592 |
+
calculate_button.click(
|
| 593 |
fn=calculate,
|
| 594 |
+
inputs=[
|
| 595 |
+
operation,
|
| 596 |
+
num1,
|
| 597 |
+
num2
|
| 598 |
+
],
|
| 599 |
+
outputs=[
|
| 600 |
+
result,
|
| 601 |
+
calculation
|
| 602 |
+
]
|
| 603 |
)
|
| 604 |
|
| 605 |
+
clear_button.click(
|
| 606 |
fn=clear_calculator,
|
| 607 |
inputs=[],
|
| 608 |
outputs=[
|
|
|
|
| 610 |
num2,
|
| 611 |
operation,
|
| 612 |
result,
|
| 613 |
+
calculation
|
| 614 |
]
|
| 615 |
)
|
| 616 |
|
| 617 |
|
| 618 |
# ============================================================
|
| 619 |
+
# Launch
|
| 620 |
# ============================================================
|
| 621 |
|
| 622 |
if __name__ == "__main__":
|
| 623 |
+
|
| 624 |
+
app.launch(
|
| 625 |
+
theme=gr.themes.Soft(),
|
| 626 |
+
css=custom_css
|
| 627 |
+
)
|