Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
# === Utility Functions ===
|
| 5 |
+
|
| 6 |
+
def convert_number(input_str):
|
| 7 |
+
try:
|
| 8 |
+
if '× 10^' in input_str:
|
| 9 |
+
base_str, exponent_str = input_str.split('× 10^')
|
| 10 |
+
base = float(base_str.strip())
|
| 11 |
+
exponent = int(exponent_str.strip())
|
| 12 |
+
result = base * (10 ** exponent)
|
| 13 |
+
return str(result)
|
| 14 |
+
elif 'e' in input_str.lower():
|
| 15 |
+
result = float(input_str)
|
| 16 |
+
return str(result)
|
| 17 |
+
else:
|
| 18 |
+
num = float(input_str)
|
| 19 |
+
if num == 0:
|
| 20 |
+
return "0 × 10^0"
|
| 21 |
+
exponent = int(math.floor(math.log10(abs(num))))
|
| 22 |
+
base = num / (10 ** exponent)
|
| 23 |
+
base = round(base, 6)
|
| 24 |
+
return f"{base} × 10^{exponent}"
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error: {e}"
|
| 27 |
+
|
| 28 |
+
def parse_standard_form(s):
|
| 29 |
+
try:
|
| 30 |
+
base, exponent = s.split("× 10^")
|
| 31 |
+
base = float(base.strip())
|
| 32 |
+
exponent = int(exponent.strip())
|
| 33 |
+
return base, exponent
|
| 34 |
+
except:
|
| 35 |
+
return None, None
|
| 36 |
+
|
| 37 |
+
def standard_form_calculator(num1_str, num2_str, operation):
|
| 38 |
+
a, m = parse_standard_form(num1_str)
|
| 39 |
+
b, n = parse_standard_form(num2_str)
|
| 40 |
+
if a is None or b is None:
|
| 41 |
+
return "Invalid input format. Use format like: 3.2 × 10^4"
|
| 42 |
+
if operation == "multiply":
|
| 43 |
+
base = a * b
|
| 44 |
+
exponent = m + n
|
| 45 |
+
elif operation == "divide":
|
| 46 |
+
base = a / b
|
| 47 |
+
exponent = m - n
|
| 48 |
+
else:
|
| 49 |
+
return "Invalid operation"
|
| 50 |
+
|
| 51 |
+
while base >= 10:
|
| 52 |
+
base /= 10
|
| 53 |
+
exponent += 1
|
| 54 |
+
while base < 1:
|
| 55 |
+
base *= 10
|
| 56 |
+
exponent -= 1
|
| 57 |
+
|
| 58 |
+
return f"{round(base, 6)} × 10^{exponent}"
|
| 59 |
+
|
| 60 |
+
# === Gradio Interface ===
|
| 61 |
+
|
| 62 |
+
# Converter UI
|
| 63 |
+
with gr.Blocks() as convert_ui:
|
| 64 |
+
gr.Markdown("## 🔁 Standard ↔ Ordinary Conversion")
|
| 65 |
+
input_box = gr.Textbox(label="Enter number (e.g., '2.3 × 10^4', '1e-3', or '23000')")
|
| 66 |
+
output_box = gr.Textbox(label="Result", interactive=False)
|
| 67 |
+
convert_btn = gr.Button("Convert")
|
| 68 |
+
convert_btn.click(fn=convert_number, inputs=input_box, outputs=output_box)
|
| 69 |
+
|
| 70 |
+
# Calculator UI
|
| 71 |
+
with gr.Blocks() as calculator_ui:
|
| 72 |
+
gr.Markdown("## ✖️➗ Standard Form Calculator")
|
| 73 |
+
num1_input = gr.Textbox(label="First Number (e.g., '3.2 × 10^4')", value="3.2 × 10^4")
|
| 74 |
+
num2_input = gr.Textbox(label="Second Number (e.g., '2 × 10^3')", value="2 × 10^3")
|
| 75 |
+
operation_input = gr.Radio(["multiply", "divide"], label="Choose Operation")
|
| 76 |
+
calc_result = gr.Textbox(label="Result", interactive=False)
|
| 77 |
+
calc_btn = gr.Button("Calculate")
|
| 78 |
+
calc_btn.click(fn=standard_form_calculator, inputs=[num1_input, num2_input, operation_input], outputs=calc_result)
|
| 79 |
+
|
| 80 |
+
# Main app with Tab selection
|
| 81 |
+
with gr.Blocks() as app:
|
| 82 |
+
gr.Markdown("## 🔢 Standard Form Helper Tool")
|
| 83 |
+
tabs = gr.Tabs()
|
| 84 |
+
with tabs:
|
| 85 |
+
with gr.TabItem("Convert"):
|
| 86 |
+
convert_ui.render()
|
| 87 |
+
with gr.TabItem("Calculator"):
|
| 88 |
+
calculator_ui.render()
|
| 89 |
+
|
| 90 |
+
# Launch the app
|
| 91 |
+
app.launch()
|