unit_converter / app.py
samajid80's picture
Create app.py
a119a07 verified
Raw History Blame Contribute Delete
3.39 kB
# app.py
import gradio as gr
# ---------- Unit definitions ----------
length_units = {
"Meter": 1,
"Kilometer": 1000,
"Centimeter": 0.01,
"Millimeter": 0.001,
"Mile": 1609.34,
"Yard": 0.9144,
"Foot": 0.3048,
"Inch": 0.0254
}
weight_units = {
"Kilogram": 1,
"Gram": 0.001,
"Milligram": 0.000001,
"Pound": 0.453592,
"Ounce": 0.0283495
}
mass_units = {
"Tonne": 1000,
"Kilogram": 1,
"Gram": 0.001,
"Milligram": 0.000001,
"Pound": 0.453592,
"Ounce": 0.0283495
}
volume_units = {
"Liter": 1,
"Milliliter": 0.001,
"Cubic meter": 1000,
"Cubic centimeter": 0.001,
"Gallon (US)": 3.78541,
"Quart": 0.946353,
"Pint": 0.473176,
"Cup": 0.24
}
area_units = {
"Square meter": 1,
"Square kilometer": 1_000_000,
"Square centimeter": 0.0001,
"Square millimeter": 0.000001,
"Hectare": 10_000,
"Acre": 4046.86,
"Square mile": 2_589_988,
"Square yard": 0.836127,
"Square foot": 0.092903,
"Square inch": 0.00064516
}
temperature_units = ["Celsius", "Fahrenheit", "Kelvin"]
unit_categories = {
"Length": length_units,
"Weight": weight_units,
"Mass": mass_units,
"Volume": volume_units,
"Area": area_units,
"Temperature": temperature_units
}
# ---------- Conversion logic ----------
def convert_temperature(value, from_unit, to_unit):
val = float(value)
if from_unit == to_unit:
return val
# -> Celsius
if from_unit == "Fahrenheit":
val = (val - 32) * 5 / 9
elif from_unit == "Kelvin":
val = val - 273.15
# Celsius -> target
if to_unit == "Fahrenheit":
return (val * 9 / 5) + 32
elif to_unit == "Kelvin":
return val + 273.15
else:
return val
def convert_unit(value, from_unit, to_unit, category):
try:
value = float(value)
except Exception:
return "❌ Please enter a valid number."
if category == "Temperature":
return f"{convert_temperature(value, from_unit, to_unit):.2f}"
factor_dict = unit_categories[category]
value_in_base = value * factor_dict[from_unit]
converted_value = value_in_base / factor_dict[to_unit]
return f"{converted_value:.6g}"
# ---------- Gradio UI ----------
def update_units(category):
units = unit_categories[category]
if category == "Temperature":
return gr.Dropdown.update(choices=units, value=units[0]), gr.Dropdown.update(choices=units, value=units[1])
else:
ul = list(units.keys())
return gr.Dropdown.update(choices=ul, value=ul[0]), gr.Dropdown.update(choices=ul, value=ul[1])
with gr.Blocks() as demo:
gr.Markdown("## 🌍 Unit Converter — Length, Weight, Mass, Volume, Area, Temperature (Offline)")
category = gr.Radio(list(unit_categories.keys()), value="Length", label="Category")
input_value = gr.Textbox(label="Value", placeholder="e.g. 12.5")
from_unit = gr.Dropdown(list(length_units.keys()), label="From")
to_unit = gr.Dropdown(list(length_units.keys()), label="To")
out = gr.Textbox(label="Result", interactive=False)
convert_btn = gr.Button("Convert")
convert_btn.click(convert_unit, inputs=[input_value, from_unit, to_unit, category], outputs=out)
category.change(fn=update_units, inputs=category, outputs=[from_unit, to_unit])
if __name__ == "__main__":
demo.launch()