Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import math
|
|
|
|
| 3 |
|
| 4 |
history = []
|
| 5 |
|
| 6 |
def calculate(expression):
|
| 7 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
expression = expression.replace('^', '**')
|
| 9 |
safe_dict = {
|
| 10 |
'sin': math.sin, 'cos': math.cos, 'tan': math.tan,
|
|
@@ -23,6 +28,9 @@ def calculate(expression):
|
|
| 23 |
def update_expression(expression, value):
|
| 24 |
if expression == "0" and value not in ['.', '(', ')']:
|
| 25 |
return value
|
|
|
|
|
|
|
|
|
|
| 26 |
return expression + value
|
| 27 |
|
| 28 |
def backspace(expression):
|
|
@@ -37,6 +45,7 @@ css = """
|
|
| 37 |
.number-button { background-color: #e0e0e0; }
|
| 38 |
.basic-op-button { background-color: #ffffff; }
|
| 39 |
.advanced-op-button { background-color: #ffa500; }
|
|
|
|
| 40 |
"""
|
| 41 |
|
| 42 |
with gr.Blocks(css=css) as iface:
|
|
@@ -50,33 +59,36 @@ with gr.Blocks(css=css) as iface:
|
|
| 50 |
|
| 51 |
with gr.Row():
|
| 52 |
with gr.Column(scale=3):
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
with gr.Row():
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
with gr.Row():
|
| 60 |
-
btn_0 = gr.Button("0", elem_classes=["calculator-button", "number-button"])
|
| 61 |
-
btn_dot = gr.Button(".", elem_classes=["calculator-button", "number-button"])
|
| 62 |
-
btn_equals = gr.Button("=", elem_classes=["calculator-button", "basic-op-button"])
|
| 63 |
|
| 64 |
with gr.Column(scale=2):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
|
| 73 |
with gr.Column(scale=2):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
# Define button click events
|
| 82 |
for btn in [btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import math
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
history = []
|
| 6 |
|
| 7 |
def calculate(expression):
|
| 8 |
try:
|
| 9 |
+
# Input validation
|
| 10 |
+
if not re.match(r'^[\d\s\+\-\*/\(\)\^\.\w]+$', expression):
|
| 11 |
+
raise ValueError("Invalid characters in expression")
|
| 12 |
+
|
| 13 |
expression = expression.replace('^', '**')
|
| 14 |
safe_dict = {
|
| 15 |
'sin': math.sin, 'cos': math.cos, 'tan': math.tan,
|
|
|
|
| 28 |
def update_expression(expression, value):
|
| 29 |
if expression == "0" and value not in ['.', '(', ')']:
|
| 30 |
return value
|
| 31 |
+
# Automatically add brackets for functions
|
| 32 |
+
if value in ['sin', 'cos', 'tan', 'log', 'ln', 'sqrt']:
|
| 33 |
+
return expression + value + '('
|
| 34 |
return expression + value
|
| 35 |
|
| 36 |
def backspace(expression):
|
|
|
|
| 45 |
.number-button { background-color: #e0e0e0; }
|
| 46 |
.basic-op-button { background-color: #ffffff; }
|
| 47 |
.advanced-op-button { background-color: #ffa500; }
|
| 48 |
+
.calculator-box { border: 1px solid #ccc; padding: 10px; margin: 5px; border-radius: 5px; }
|
| 49 |
"""
|
| 50 |
|
| 51 |
with gr.Blocks(css=css) as iface:
|
|
|
|
| 59 |
|
| 60 |
with gr.Row():
|
| 61 |
with gr.Column(scale=3):
|
| 62 |
+
with gr.Box(elem_classes=["calculator-box"]):
|
| 63 |
+
# Number pad
|
| 64 |
+
for i in range(3):
|
| 65 |
+
with gr.Row():
|
| 66 |
+
for j in range(3):
|
| 67 |
+
num = 7 - 3*i + j
|
| 68 |
+
globals()[f"btn_{num}"] = gr.Button(str(num), elem_classes=["calculator-button", "number-button"])
|
| 69 |
with gr.Row():
|
| 70 |
+
btn_0 = gr.Button("0", elem_classes=["calculator-button", "number-button"])
|
| 71 |
+
btn_dot = gr.Button(".", elem_classes=["calculator-button", "number-button"])
|
| 72 |
+
btn_equals = gr.Button("=", elem_classes=["calculator-button", "basic-op-button"])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
with gr.Column(scale=2):
|
| 75 |
+
with gr.Box(elem_classes=["calculator-box"]):
|
| 76 |
+
# Basic operations
|
| 77 |
+
for op, symbol in [("add", "+"), ("sub", "-"), ("mul", "*"), ("div", "/")]:
|
| 78 |
+
globals()[f"btn_{op}"] = gr.Button(symbol, elem_classes=["calculator-button", "basic-op-button"])
|
| 79 |
+
btn_left_paren = gr.Button("(", elem_classes=["calculator-button", "basic-op-button"])
|
| 80 |
+
btn_right_paren = gr.Button(")", elem_classes=["calculator-button", "basic-op-button"])
|
| 81 |
+
btn_clear = gr.Button("C", elem_classes=["calculator-button", "basic-op-button"])
|
| 82 |
+
btn_backspace = gr.Button("⌫", elem_classes=["calculator-button", "basic-op-button"])
|
| 83 |
|
| 84 |
with gr.Column(scale=2):
|
| 85 |
+
with gr.Box(elem_classes=["calculator-box"]):
|
| 86 |
+
# Advanced operations
|
| 87 |
+
for func in ["sin", "cos", "tan", "log", "ln", "sqrt"]:
|
| 88 |
+
globals()[f"btn_{func}"] = gr.Button(func, elem_classes=["calculator-button", "advanced-op-button"])
|
| 89 |
+
btn_power = gr.Button("^", elem_classes=["calculator-button", "advanced-op-button"])
|
| 90 |
+
btn_pi = gr.Button("π", elem_classes=["calculator-button", "advanced-op-button"])
|
| 91 |
+
btn_e = gr.Button("e", elem_classes=["calculator-button", "advanced-op-button"])
|
| 92 |
|
| 93 |
# Define button click events
|
| 94 |
for btn in [btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9,
|