Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# -------------------------------
|
| 4 |
+
# Conversion Functions
|
| 5 |
+
# -------------------------------
|
| 6 |
+
def kg_to_g(value): return value * 1000
|
| 7 |
+
def g_to_kg(value): return value / 1000
|
| 8 |
+
|
| 9 |
+
def m_to_cm(value): return value * 100
|
| 10 |
+
def cm_to_m(value): return value / 100
|
| 11 |
+
def m_to_ft(value): return value * 3.280839895013123
|
| 12 |
+
def ft_to_m(value): return value / 3.280839895013123
|
| 13 |
+
|
| 14 |
+
def c_to_f(value): return (value * 9/5) + 32
|
| 15 |
+
def f_to_c(value): return (value - 32) * 5/9
|
| 16 |
+
|
| 17 |
+
# -------------------------------
|
| 18 |
+
# Conversion Dispatcher
|
| 19 |
+
# -------------------------------
|
| 20 |
+
def convert_value(category, from_unit, to_unit, input_value):
|
| 21 |
+
try:
|
| 22 |
+
x = float(input_value)
|
| 23 |
+
except:
|
| 24 |
+
return "⚠️ Please enter a valid numeric value."
|
| 25 |
+
|
| 26 |
+
if category == "Mass":
|
| 27 |
+
if from_unit == "kg" and to_unit == "g":
|
| 28 |
+
return f"{x} kg = {kg_to_g(x)} g"
|
| 29 |
+
elif from_unit == "g" and to_unit == "kg":
|
| 30 |
+
return f"{x} g = {g_to_kg(x)} kg"
|
| 31 |
+
elif from_unit == to_unit:
|
| 32 |
+
return f"{x} {from_unit} = {x} {to_unit}"
|
| 33 |
+
else:
|
| 34 |
+
return "Conversion not supported for Mass."
|
| 35 |
+
|
| 36 |
+
elif category == "Length":
|
| 37 |
+
if from_unit == "m" and to_unit == "cm":
|
| 38 |
+
return f"{x} m = {m_to_cm(x)} cm"
|
| 39 |
+
elif from_unit == "cm" and to_unit == "m":
|
| 40 |
+
return f"{x} cm = {cm_to_m(x)} m"
|
| 41 |
+
elif from_unit == "m" and to_unit == "ft":
|
| 42 |
+
return f"{x} m = {round(m_to_ft(x), 4)} ft"
|
| 43 |
+
elif from_unit == "ft" and to_unit == "m":
|
| 44 |
+
return f"{x} ft = {round(ft_to_m(x), 4)} m"
|
| 45 |
+
elif from_unit == to_unit:
|
| 46 |
+
return f"{x} {from_unit} = {x} {to_unit}"
|
| 47 |
+
else:
|
| 48 |
+
return "Conversion not supported for Length."
|
| 49 |
+
|
| 50 |
+
elif category == "Temperature":
|
| 51 |
+
if from_unit == "°C" and to_unit == "°F":
|
| 52 |
+
return f"{x} °C = {round(c_to_f(x), 2)} °F"
|
| 53 |
+
elif from_unit == "°F" and to_unit == "°C":
|
| 54 |
+
return f"{x} °F = {round(f_to_c(x), 2)} °C"
|
| 55 |
+
elif from_unit == to_unit:
|
| 56 |
+
return f"{x} {from_unit} = {x} {to_unit}"
|
| 57 |
+
else:
|
| 58 |
+
return "Conversion not supported for Temperature."
|
| 59 |
+
|
| 60 |
+
else:
|
| 61 |
+
return "Unknown category."
|
| 62 |
+
|
| 63 |
+
# -------------------------------
|
| 64 |
+
# Gradio Interface
|
| 65 |
+
# -------------------------------
|
| 66 |
+
categories = ["Mass", "Length", "Temperature"]
|
| 67 |
+
units_map = {
|
| 68 |
+
"Mass": ["kg", "g"],
|
| 69 |
+
"Length": ["m", "cm", "ft"],
|
| 70 |
+
"Temperature": ["°C", "°F"]
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
def update_units(cat):
|
| 74 |
+
return (
|
| 75 |
+
gr.update(choices=units_map[cat], value=units_map[cat][0]),
|
| 76 |
+
gr.update(choices=units_map[cat], value=units_map[cat][1])
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
with gr.Blocks(title="Unit Converter") as demo:
|
| 80 |
+
gr.Markdown("# 🧮 Unit Converter\n### Convert mass, length, and temperature easily!\nBuilt with **Python + Gradio** (no APIs).")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
category = gr.Dropdown(label="Category", choices=categories, value="Mass")
|
| 84 |
+
from_unit = gr.Dropdown(label="From unit", choices=units_map["Mass"], value="kg")
|
| 85 |
+
to_unit = gr.Dropdown(label="To unit", choices=units_map["Mass"], value="g")
|
| 86 |
+
|
| 87 |
+
value = gr.Textbox(label="Value to convert", placeholder="Enter a number like 10")
|
| 88 |
+
convert_btn = gr.Button("Convert")
|
| 89 |
+
output = gr.Textbox(label="Result")
|
| 90 |
+
|
| 91 |
+
category.change(update_units, inputs=category, outputs=[from_unit, to_unit])
|
| 92 |
+
convert_btn.click(convert_value, inputs=[category, from_unit, to_unit, value], outputs=output)
|
| 93 |
+
|
| 94 |
+
demo.launch()
|