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