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|
| 14 |
+
"m": 1.0,
|
| 15 |
+
"cm": 0.01,
|
| 16 |
+
"mm": 0.001,
|
| 17 |
+
"km": 1000.0,
|
| 18 |
+
"ft": 0.3048,
|
| 19 |
+
"in": 0.0254,
|
| 20 |
+
"yd": 0.9144,
|
| 21 |
+
"mile": 1609.344,
|
| 22 |
+
},
|
| 23 |
+
"Volume": {
|
| 24 |
+
"L": 1.0,
|
| 25 |
+
"mL": 0.001,
|
| 26 |
+
"m^3": 1000.0,
|
| 27 |
+
"gal (US)": 3.785411784,
|
| 28 |
+
"cup (US)": 0.2365882365,
|
| 29 |
+
"fl oz (US)": 0.0295735295625,
|
| 30 |
+
},
|
| 31 |
+
"Time": {
|
| 32 |
+
"s": 1.0,
|
| 33 |
+
"min": 60.0,
|
| 34 |
+
"hr": 3600.0,
|
| 35 |
+
"day": 86400.0,
|
| 36 |
+
},
|
| 37 |
+
"Temperature": ["C", "F", "K"]
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# --- Temperature conversion helper ---
|
| 41 |
+
def convert_temperature(value, from_u, to_u):
|
| 42 |
+
if from_u == "C":
|
| 43 |
+
c = float(value)
|
| 44 |
+
elif from_u == "F":
|
| 45 |
+
c = (float(value) - 32.0) * 5.0 / 9.0
|
| 46 |
+
elif from_u == "K":
|
| 47 |
+
c = float(value) - 273.15
|
| 48 |
+
else:
|
| 49 |
+
raise ValueError(f"Unknown temperature unit: {from_u}")
|
| 50 |
+
if to_u == "C":
|
| 51 |
+
return c
|
| 52 |
+
elif to_u == "F":
|
| 53 |
+
return c * 9.0 / 5.0 + 32.0
|
| 54 |
+
elif to_u == "K":
|
| 55 |
+
return c + 273.15
|
| 56 |
+
else:
|
| 57 |
+
raise ValueError(f"Unknown temperature unit: {to_u}")
|
| 58 |
+
|
| 59 |
+
# --- Generic conversion for linear categories ---
|
| 60 |
+
def convert_value(value, from_u, to_u, category):
|
| 61 |
+
if category == "Temperature":
|
| 62 |
+
return convert_temperature(value, from_u, to_u)
|
| 63 |
+
units_map = UNITS[category]
|
| 64 |
+
if from_u not in units_map or to_u not in units_map:
|
| 65 |
+
raise ValueError("Unit not available in chosen category.")
|
| 66 |
+
value_in_base = float(value) * units_map[from_u]
|
| 67 |
+
result = value_in_base / units_map[to_u]
|
| 68 |
+
return result
|
| 69 |
+
|
| 70 |
+
# --- Build Gradio UI ---
|
| 71 |
+
with gr.Blocks() as demo:
|
| 72 |
+
gr.Markdown("## Unit Converter (Gradio — Hugging Face Space)\nConvert common units — no API key needed.")
|
| 73 |
+
with gr.Row():
|
| 74 |
+
category = gr.Dropdown(choices=list(UNITS.keys()), value="Length", label="Category")
|
| 75 |
+
with gr.Row():
|
| 76 |
+
value_in = gr.Number(value=1, label="Value")
|
| 77 |
+
from_unit = gr.Dropdown(choices=list(UNITS["Length"].keys()), label="From unit")
|
| 78 |
+
to_unit = gr.Dropdown(choices=list(UNITS["Length"].keys()), label="To unit")
|
| 79 |
+
precision = gr.Slider(minimum=0, maximum=10, step=1, value=4, label="Decimal places")
|
| 80 |
+
with gr.Row():
|
| 81 |
+
convert_btn = gr.Button("Convert")
|
| 82 |
+
clear_btn = gr.Button("Clear history")
|
| 83 |
+
result = gr.Textbox(label="Result", interactive=False)
|
| 84 |
+
history_box = gr.Textbox(label="History (last 20)", interactive=False)
|
| 85 |
+
history_state = gr.State([])
|
| 86 |
+
|
| 87 |
+
def update_units(selected_category):
|
| 88 |
+
if isinstance(UNITS[selected_category], dict):
|
| 89 |
+
choices = list(UNITS[selected_category].keys())
|
| 90 |
+
else:
|
| 91 |
+
choices = UNITS[selected_category]
|
| 92 |
+
default_from = choices[0]
|
| 93 |
+
default_to = choices[1] if len(choices) > 1 else choices[0]
|
| 94 |
+
return gr.update(choices=choices, value=default_from), gr.update(choices=choices, value=default_to)
|
| 95 |
+
|
| 96 |
+
category.change(fn=update_units, inputs=category, outputs=[from_unit, to_unit])
|
| 97 |
+
|
| 98 |
+
def do_convert(val, from_u, to_u, cat, prec, hist):
|
| 99 |
+
try:
|
| 100 |
+
res = convert_value(val, from_u, to_u, cat)
|
| 101 |
+
rounded = round(res, int(prec))
|
| 102 |
+
text = f"{val} {from_u} = {rounded} {to_u}"
|
| 103 |
+
hist = hist or []
|
| 104 |
+
hist.append(text)
|
| 105 |
+
hist = hist[-20:]
|
| 106 |
+
return text, "\n".join(hist), hist
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return f"Error: {e}", "\n".join(hist or []), hist
|
| 109 |
+
|
| 110 |
+
convert_btn.click(
|
| 111 |
+
fn=do_convert,
|
| 112 |
+
inputs=[value_in, from_unit, to_unit, category, precision, history_state],
|
| 113 |
+
outputs=[result, history_box, history_state]
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
def clear_history(hist):
|
| 117 |
+
return "", "", []
|
| 118 |
+
clear_btn.click(fn=clear_history, inputs=[history_state], outputs=[result, history_box, history_state])
|
| 119 |
+
|
| 120 |
+
# IMPORTANT for Spaces: don't use share=True
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.launch()
|