File size: 2,754 Bytes
dc368b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

# Conversion logic
def convert_units(value, from_unit, to_unit, category):
    conversions = {
        "Length": {
            "Meter": 1,
            "Kilometer": 1000,
            "Centimeter": 0.01,
            "Millimeter": 0.001,
            "Mile": 1609.34,
            "Yard": 0.9144,
            "Foot": 0.3048,
            "Inch": 0.0254,
        },
        "Weight": {
            "Kilogram": 1,
            "Gram": 0.001,
            "Milligram": 0.000001,
            "Pound": 0.453592,
            "Ounce": 0.0283495,
        },
        "Temperature": None  # handled separately
    }

    if category == "Temperature":
        if from_unit == to_unit:
            return value
        elif from_unit == "Celsius" and to_unit == "Fahrenheit":
            return (value * 9/5) + 32
        elif from_unit == "Fahrenheit" and to_unit == "Celsius":
            return (value - 32) * 5/9
        elif from_unit == "Celsius" and to_unit == "Kelvin":
            return value + 273.15
        elif from_unit == "Kelvin" and to_unit == "Celsius":
            return value - 273.15
        elif from_unit == "Fahrenheit" and to_unit == "Kelvin":
            return (value - 32) * 5/9 + 273.15
        elif from_unit == "Kelvin" and to_unit == "Fahrenheit":
            return (value - 273.15) * 9/5 + 32

    else:
        base_value = value * conversions[category][from_unit]
        result = base_value / conversions[category][to_unit]
        return result


def update_units(category):
    if category == "Length":
        units = ["Meter", "Kilometer", "Centimeter", "Millimeter", "Mile", "Yard", "Foot", "Inch"]
    elif category == "Weight":
        units = ["Kilogram", "Gram", "Milligram", "Pound", "Ounce"]
    elif category == "Temperature":
        units = ["Celsius", "Fahrenheit", "Kelvin"]
    return gr.update(choices=units, value=units[0]), gr.update(choices=units, value=units[1])


with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("## 🌡️ Universal Unit Converter\nConvert between different unit categories easily!")

    with gr.Row():
        category = gr.Dropdown(["Length", "Weight", "Temperature"], label="Select Category", value="Length")

    with gr.Row():
        value = gr.Number(label="Enter Value", value=1)
    
    with gr.Row():
        from_unit = gr.Dropdown([], label="From Unit")
        to_unit = gr.Dropdown([], label="To Unit")

    category.change(fn=update_units, inputs=category, outputs=[from_unit, to_unit])

    convert_btn = gr.Button("Convert")
    output = gr.Textbox(label="Converted Value")

    convert_btn.click(fn=convert_units, 
                      inputs=[value, from_unit, to_unit, category], 
                      outputs=output)

demo.launch()