Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# Unit Converter Gradio app — ready for Hugging Face Spaces.
|
| 3 |
+
# Save this file at the root of your Space repo as "app.py".
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# ---------- Unit maps (base units: meters for length, grams for mass) ----------
|
| 8 |
+
LENGTH_TO_M = {
|
| 9 |
+
"mm": 1e-3,
|
| 10 |
+
"cm": 1e-2,
|
| 11 |
+
"m": 1.0,
|
| 12 |
+
"km": 1e3,
|
| 13 |
+
"in": 0.0254,
|
| 14 |
+
"ft": 0.3048,
|
| 15 |
+
"yd": 0.9144,
|
| 16 |
+
"mi": 1609.344,
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
MASS_TO_G = {
|
| 20 |
+
"mg": 1e-3,
|
| 21 |
+
"g": 1.0,
|
| 22 |
+
"kg": 1e3,
|
| 23 |
+
"oz": 28.349523125,
|
| 24 |
+
"lb": 453.59237,
|
| 25 |
+
"st": 6350.29318, # stone (optional example)
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
# ---------- Helper functions ----------
|
| 29 |
+
def safe_round(x, decimals):
|
| 30 |
+
try:
|
| 31 |
+
return round(float(x), int(decimals))
|
| 32 |
+
except Exception:
|
| 33 |
+
return x
|
| 34 |
+
|
| 35 |
+
def convert_generic(value, from_unit, to_unit, factor_map, decimals):
|
| 36 |
+
if value is None:
|
| 37 |
+
return "Please enter a number."
|
| 38 |
+
try:
|
| 39 |
+
value = float(value)
|
| 40 |
+
except Exception:
|
| 41 |
+
return "Input must be a number."
|
| 42 |
+
|
| 43 |
+
if from_unit not in factor_map or to_unit not in factor_map:
|
| 44 |
+
return "Unsupported unit selected."
|
| 45 |
+
|
| 46 |
+
# convert -> base -> target
|
| 47 |
+
base = value * factor_map[from_unit]
|
| 48 |
+
result = base / factor_map[to_unit]
|
| 49 |
+
result = safe_round(result, decimals)
|
| 50 |
+
return f"{result} {to_unit}"
|
| 51 |
+
|
| 52 |
+
def convert_length(value, from_unit, to_unit, decimals):
|
| 53 |
+
return convert_generic(value, from_unit, to_unit, LENGTH_TO_M, decimals)
|
| 54 |
+
|
| 55 |
+
def convert_mass(value, from_unit, to_unit, decimals):
|
| 56 |
+
return convert_generic(value, from_unit, to_unit, MASS_TO_G, decimals)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# ---------- Gradio UI ----------
|
| 60 |
+
with gr.Blocks(title="Unit Converter (Length & Mass)") as demo:
|
| 61 |
+
gr.Markdown("# 🧰 Unit Converter — Length & Mass\nConvert mm/cm/m/ft etc. and g/kg/lb/oz etc.")
|
| 62 |
+
with gr.Tab("Length"):
|
| 63 |
+
val_l = gr.Number(label="Value", value=1.0)
|
| 64 |
+
fr_l = gr.Dropdown(choices=list(LENGTH_TO_M.keys()), value="cm", label="From")
|
| 65 |
+
to_l = gr.Dropdown(choices=list(LENGTH_TO_M.keys()), value="m", label="To")
|
| 66 |
+
dec_l = gr.Slider(0, 10, value=3, step=1, label="Decimal places")
|
| 67 |
+
btn_l = gr.Button("Convert")
|
| 68 |
+
out_l = gr.Textbox(label="Result", interactive=False)
|
| 69 |
+
examples_l = gr.Examples(
|
| 70 |
+
examples=[
|
| 71 |
+
[150, "cm", "m", 4],
|
| 72 |
+
[2, "m", "ft", 4],
|
| 73 |
+
[12, "in", "cm", 3],
|
| 74 |
+
],
|
| 75 |
+
inputs=[val_l, fr_l, to_l, dec_l],
|
| 76 |
+
label="Examples (click to fill)"
|
| 77 |
+
)
|
| 78 |
+
btn_l.click(convert_length, [val_l, fr_l, to_l, dec_l], out_l)
|
| 79 |
+
|
| 80 |
+
with gr.Tab("Mass"):
|
| 81 |
+
val_m = gr.Number(label="Value", value=1000.0)
|
| 82 |
+
fr_m = gr.Dropdown(choices=list(MASS_TO_G.keys()), value="g", label="From")
|
| 83 |
+
to_m = gr.Dropdown(choices=list(MASS_TO_G.keys()), value="lb", label="To")
|
| 84 |
+
dec_m = gr.Slider(0, 10, value=4, step=1, label="Decimal places")
|
| 85 |
+
btn_m = gr.Button("Convert")
|
| 86 |
+
out_m = gr.Textbox(label="Result", interactive=False)
|
| 87 |
+
examples_m = gr.Examples(
|
| 88 |
+
examples=[
|
| 89 |
+
[500, "g", "lb", 4],
|
| 90 |
+
[2, "kg", "lb", 4],
|
| 91 |
+
[1, "lb", "g", 2],
|
| 92 |
+
],
|
| 93 |
+
inputs=[val_m, fr_m, to_m, dec_m],
|
| 94 |
+
label="Examples (click to fill)"
|
| 95 |
+
)
|
| 96 |
+
btn_m.click(convert_mass, [val_m, fr_m, to_m, dec_m], out_m)
|
| 97 |
+
|
| 98 |
+
gr.Markdown("**Tip:** Add more units by editing the `LENGTH_TO_M` and `MASS_TO_G` dicts at the top.")
|
| 99 |
+
|
| 100 |
+
# IMPORTANT: DO NOT use share=True on Spaces (works for Colab only).
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|