Download app.py from STARxMOUT/check: direct link, hf CLI and curl.
- Browser
- Download file 3.44 kB
-
https://huggingface.co/spaces/STARxMOUT/check/resolve/main/app.py
- Command line
-
hf download hf://spaces/STARxMOUT/check/app.py
-
curl -L -o app.py https://huggingface.co/spaces/STARxMOUT/check/resolve/main/app.py
3.44 kB
| 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() | |