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