Spaces:
Sleeping
Sleeping
File size: 3,486 Bytes
7f97e7c | 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 96 97 98 99 100 101 102 103 | # app.py
# Unit Converter Gradio app — ready for Hugging Face Spaces.
# Save this file at the root of your Space repo as "app.py".
import gradio as gr
# ---------- Unit maps (base units: meters for length, grams for mass) ----------
LENGTH_TO_M = {
"mm": 1e-3,
"cm": 1e-2,
"m": 1.0,
"km": 1e3,
"in": 0.0254,
"ft": 0.3048,
"yd": 0.9144,
"mi": 1609.344,
}
MASS_TO_G = {
"mg": 1e-3,
"g": 1.0,
"kg": 1e3,
"oz": 28.349523125,
"lb": 453.59237,
"st": 6350.29318, # stone (optional example)
}
# ---------- Helper functions ----------
def safe_round(x, decimals):
try:
return round(float(x), int(decimals))
except Exception:
return x
def convert_generic(value, from_unit, to_unit, factor_map, decimals):
if value is None:
return "Please enter a number."
try:
value = float(value)
except Exception:
return "Input must be a number."
if from_unit not in factor_map or to_unit not in factor_map:
return "Unsupported unit selected."
# convert -> base -> target
base = value * factor_map[from_unit]
result = base / factor_map[to_unit]
result = safe_round(result, decimals)
return f"{result} {to_unit}"
def convert_length(value, from_unit, to_unit, decimals):
return convert_generic(value, from_unit, to_unit, LENGTH_TO_M, decimals)
def convert_mass(value, from_unit, to_unit, decimals):
return convert_generic(value, from_unit, to_unit, MASS_TO_G, decimals)
# ---------- Gradio UI ----------
with gr.Blocks(title="Unit Converter (Length & Mass)") as demo:
gr.Markdown("# 🧰 Unit Converter — Length & Mass\nConvert mm/cm/m/ft etc. and g/kg/lb/oz etc.")
with gr.Tab("Length"):
val_l = gr.Number(label="Value", value=1.0)
fr_l = gr.Dropdown(choices=list(LENGTH_TO_M.keys()), value="cm", label="From")
to_l = gr.Dropdown(choices=list(LENGTH_TO_M.keys()), value="m", label="To")
dec_l = gr.Slider(0, 10, value=3, step=1, label="Decimal places")
btn_l = gr.Button("Convert")
out_l = gr.Textbox(label="Result", interactive=False)
examples_l = gr.Examples(
examples=[
[150, "cm", "m", 4],
[2, "m", "ft", 4],
[12, "in", "cm", 3],
],
inputs=[val_l, fr_l, to_l, dec_l],
label="Examples (click to fill)"
)
btn_l.click(convert_length, [val_l, fr_l, to_l, dec_l], out_l)
with gr.Tab("Mass"):
val_m = gr.Number(label="Value", value=1000.0)
fr_m = gr.Dropdown(choices=list(MASS_TO_G.keys()), value="g", label="From")
to_m = gr.Dropdown(choices=list(MASS_TO_G.keys()), value="lb", label="To")
dec_m = gr.Slider(0, 10, value=4, step=1, label="Decimal places")
btn_m = gr.Button("Convert")
out_m = gr.Textbox(label="Result", interactive=False)
examples_m = gr.Examples(
examples=[
[500, "g", "lb", 4],
[2, "kg", "lb", 4],
[1, "lb", "g", 2],
],
inputs=[val_m, fr_m, to_m, dec_m],
label="Examples (click to fill)"
)
btn_m.click(convert_mass, [val_m, fr_m, to_m, dec_m], out_m)
gr.Markdown("**Tip:** Add more units by editing the `LENGTH_TO_M` and `MASS_TO_G` dicts at the top.")
# IMPORTANT: DO NOT use share=True on Spaces (works for Colab only).
if __name__ == "__main__":
demo.launch()
|