BasitAliii commited on
Commit
11db94d
·
verified ·
1 Parent(s): d21ad6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +140 -77
app.py CHANGED
@@ -3,11 +3,11 @@ import gradio as gr
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)
@@ -25,9 +25,7 @@ def format_number(value):
25
 
26
 
27
  def calculate(operation, num1, num2):
28
- """
29
- Perform the selected calculator operation.
30
- """
31
 
32
  # --------------------------------------------------------
33
  # Validate first number
@@ -42,18 +40,58 @@ def calculate(operation, num1, num2):
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.", ""
@@ -63,41 +101,72 @@ def calculate(operation, num1, 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
  )
@@ -108,7 +177,7 @@ def calculate(operation, num1, num2):
108
  # Square Root
109
  # --------------------------------------------------------
110
 
111
- if operation == "Square Root (√x)":
112
 
113
  if num1 < 0:
114
  return (
@@ -122,7 +191,8 @@ def calculate(operation, num1, num2):
122
  result_text = format_number(result)
123
 
124
  calculation = (
125
- f"√{format_number(num1)} = {result_text}"
 
126
  )
127
 
128
  return result_text, calculation
@@ -131,7 +201,7 @@ def calculate(operation, num1, num2):
131
  # Logarithm
132
  # --------------------------------------------------------
133
 
134
- if operation == "Logarithm (log₁₀)":
135
 
136
  if num1 <= 0:
137
  return (
@@ -152,15 +222,15 @@ def calculate(operation, num1, num2):
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
 
@@ -179,12 +249,13 @@ def calculate(operation, num1, num2):
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
 
@@ -201,12 +272,13 @@ def calculate(operation, num1, num2):
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
 
@@ -223,11 +295,11 @@ def calculate(operation, num1, num2):
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 "
@@ -253,7 +325,7 @@ def calculate(operation, num1, num2):
253
  # Factorial
254
  # --------------------------------------------------------
255
 
256
- if operation == "Factorial (n!)":
257
 
258
  if num1 < 0:
259
  return (
@@ -295,14 +367,11 @@ def calculate(operation, num1, num2):
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 = [
308
  "Addition (+)",
@@ -313,11 +382,10 @@ def update_second_number(operation):
313
  ]
314
 
315
  if operation in binary_operations:
316
-
317
  return gr.update(
318
  visible=True,
319
- label="Second Number",
320
- placeholder="Enter second number"
321
  )
322
 
323
  return gr.update(
@@ -327,7 +395,7 @@ def update_second_number(operation):
327
 
328
 
329
  def clear_calculator():
330
- """Reset all calculator fields."""
331
 
332
  return (
333
  None,
@@ -343,13 +411,11 @@ def clear_calculator():
343
  # ============================================================
344
 
345
  custom_css = """
346
- /* Main container */
347
  .gradio-container {
348
  max-width: 1100px !important;
349
  margin: auto !important;
350
  }
351
 
352
- /* Header */
353
  .calculator-header {
354
  text-align: center;
355
  padding: 25px 10px 15px 10px;
@@ -366,20 +432,17 @@ custom_css = """
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 */
376
  .result-box textarea {
377
  font-size: 28px !important;
378
  font-weight: 700 !important;
379
  text-align: center !important;
380
  }
381
 
382
- /* Buttons */
383
  .calculate-btn {
384
  min-height: 52px !important;
385
  font-size: 17px !important;
@@ -391,12 +454,10 @@ custom_css = """
391
  font-size: 17px !important;
392
  }
393
 
394
- /* Operation selector */
395
  .operation-dropdown {
396
  font-weight: 600 !important;
397
  }
398
 
399
- /* Footer */
400
  .calculator-footer {
401
  text-align: center;
402
  padding: 15px;
@@ -405,7 +466,6 @@ custom_css = """
405
  font-size: 13px;
406
  }
407
 
408
- /* Mobile */
409
  @media (max-width: 700px) {
410
 
411
  .calculator-title {
@@ -448,8 +508,8 @@ with gr.Blocks(
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>
@@ -457,13 +517,13 @@ with gr.Blocks(
457
  )
458
 
459
  # --------------------------------------------------------
460
- # Main Calculator
461
  # --------------------------------------------------------
462
 
463
  with gr.Row():
464
 
465
  # ====================================================
466
- # Calculator Input Section
467
  # ====================================================
468
 
469
  with gr.Column(
@@ -516,7 +576,7 @@ with gr.Blocks(
516
  )
517
 
518
  # ====================================================
519
- # Result Section
520
  # ====================================================
521
 
522
  with gr.Column(
@@ -580,7 +640,7 @@ Angles are entered in **degrees**.
580
  )
581
 
582
  # ========================================================
583
- # Event Handlers
584
  # ========================================================
585
 
586
  operation.change(
@@ -616,12 +676,15 @@ Angles are entered in **degrees**.
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
  )
 
3
 
4
 
5
  # ============================================================
6
+ # Calculator Logic
7
  # ============================================================
8
 
9
  def format_number(value):
10
+ """Format numbers for clean display."""
11
 
12
  if isinstance(value, int):
13
  return str(value)
 
25
 
26
 
27
  def calculate(operation, num1, num2):
28
+ """Perform the selected calculator operation."""
 
 
29
 
30
  # --------------------------------------------------------
31
  # Validate first number
 
40
  return "Error: Please enter a valid first number.", ""
41
 
42
  # --------------------------------------------------------
43
+ # Basic Operations
44
  # --------------------------------------------------------
45
 
46
+ if operation == "Addition (+)":
 
 
 
 
 
 
47
 
48
+ if num2 is None:
49
+ return "Please enter the second number.", ""
50
+
51
+ try:
52
+ num2 = float(num2)
53
+ except (ValueError, TypeError):
54
+ return "Error: Please enter a valid second number.", ""
55
+
56
+ result = num1 + num2
57
+
58
+ result_text = format_number(result)
59
+
60
+ calculation = (
61
+ f"{format_number(num1)} + "
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
+ try:
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
+ calculation = (
85
+ f"{format_number(num1)} - "
86
+ f"{format_number(num2)} = "
87
+ f"{result_text}"
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.", ""
 
101
  except (ValueError, TypeError):
102
  return "Error: Please enter a valid second number.", ""
103
 
104
+ result = num1 * num2
 
 
105
 
106
+ result_text = format_number(result)
 
 
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
+ try:
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
  result_text = format_number(result)
134
 
135
  calculation = (
136
+ f"{format_number(num1)} ÷ "
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
+ try:
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
+ if isinstance(result, complex):
161
+ return (
162
+ "Error: Complex results are not supported.",
163
+ ""
164
+ )
165
+
166
+ result_text = format_number(result)
167
+
168
+ calculation = (
169
+ f"{format_number(num1)} ^ "
170
  f"{format_number(num2)} = "
171
  f"{result_text}"
172
  )
 
177
  # Square Root
178
  # --------------------------------------------------------
179
 
180
+ elif operation == "Square Root (√x)":
181
 
182
  if num1 < 0:
183
  return (
 
191
  result_text = format_number(result)
192
 
193
  calculation = (
194
+ f"√{format_number(num1)} = "
195
+ f"{result_text}"
196
  )
197
 
198
  return result_text, calculation
 
201
  # Logarithm
202
  # --------------------------------------------------------
203
 
204
+ elif operation == "Logarithm (log₁₀)":
205
 
206
  if num1 <= 0:
207
  return (
 
222
  return result_text, calculation
223
 
224
  # --------------------------------------------------------
225
+ # Natural Log
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
 
 
249
  # Sine
250
  # --------------------------------------------------------
251
 
252
+ elif operation == "Sine (sin)":
253
 
254
  radians = math.radians(num1)
255
+
256
  result = math.sin(radians)
257
 
258
+ # Remove floating-point noise.
259
  if abs(result) < 1e-12:
260
  result = 0.0
261
 
 
272
  # Cosine
273
  # --------------------------------------------------------
274
 
275
+ elif operation == "Cosine (cos)":
276
 
277
  radians = math.radians(num1)
278
+
279
  result = math.cos(radians)
280
 
281
+ # Remove floating-point noise.
282
  if abs(result) < 1e-12:
283
  result = 0.0
284
 
 
295
  # Tangent
296
  # --------------------------------------------------------
297
 
298
+ elif operation == "Tangent (tan)":
299
 
300
  radians = math.radians(num1)
301
 
302
+ # cos(x) = 0 means tan(x) is undefined.
303
  if abs(math.cos(radians)) < 1e-12:
304
  return (
305
  "Error: Tangent is undefined "
 
325
  # Factorial
326
  # --------------------------------------------------------
327
 
328
+ elif operation == "Factorial (n!)":
329
 
330
  if num1 < 0:
331
  return (
 
367
 
368
 
369
  # ============================================================
370
+ # UI Helper Functions
371
  # ============================================================
372
 
373
  def update_second_number(operation):
374
+ """Show second input only when required."""
 
 
 
375
 
376
  binary_operations = [
377
  "Addition (+)",
 
382
  ]
383
 
384
  if operation in binary_operations:
 
385
  return gr.update(
386
  visible=True,
387
+ value=None,
388
+ label="Second Number"
389
  )
390
 
391
  return gr.update(
 
395
 
396
 
397
  def clear_calculator():
398
+ """Clear the calculator."""
399
 
400
  return (
401
  None,
 
411
  # ============================================================
412
 
413
  custom_css = """
 
414
  .gradio-container {
415
  max-width: 1100px !important;
416
  margin: auto !important;
417
  }
418
 
 
419
  .calculator-header {
420
  text-align: center;
421
  padding: 25px 10px 15px 10px;
 
432
  opacity: 0.7;
433
  }
434
 
 
435
  .calc-card {
436
  border-radius: 18px !important;
437
  padding: 22px !important;
438
  }
439
 
 
440
  .result-box textarea {
441
  font-size: 28px !important;
442
  font-weight: 700 !important;
443
  text-align: center !important;
444
  }
445
 
 
446
  .calculate-btn {
447
  min-height: 52px !important;
448
  font-size: 17px !important;
 
454
  font-size: 17px !important;
455
  }
456
 
 
457
  .operation-dropdown {
458
  font-weight: 600 !important;
459
  }
460
 
 
461
  .calculator-footer {
462
  text-align: center;
463
  padding: 15px;
 
466
  font-size: 13px;
467
  }
468
 
 
469
  @media (max-width: 700px) {
470
 
471
  .calculator-title {
 
508
  </div>
509
 
510
  <div class="calculator-subtitle">
511
+ Perform arithmetic and scientific calculations
512
+ quickly and accurately.
513
  </div>
514
 
515
  </div>
 
517
  )
518
 
519
  # --------------------------------------------------------
520
+ # Main Interface
521
  # --------------------------------------------------------
522
 
523
  with gr.Row():
524
 
525
  # ====================================================
526
+ # Input Panel
527
  # ====================================================
528
 
529
  with gr.Column(
 
576
  )
577
 
578
  # ====================================================
579
+ # Result Panel
580
  # ====================================================
581
 
582
  with gr.Column(
 
640
  )
641
 
642
  # ========================================================
643
+ # Events
644
  # ========================================================
645
 
646
  operation.change(
 
676
 
677
 
678
  # ============================================================
679
+ # Launch for Hugging Face Spaces
680
  # ============================================================
681
 
682
  if __name__ == "__main__":
683
 
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
  )