File size: 4,330 Bytes
7d305e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import gradio as gr

def cel_fah(c):
    return (c * 1.8) + 32

def fah_cel(f):
    return (f - 32) / 1.8

def cel_kel(c):
    return c + 273.15

def kel_cel(k):
    return k - 273.15

def fah_kel(f):
    return (f - 32) / 1.8 + 273.15

def kel_fah(k):
    return (k - 273.15) * 1.8 + 32

def explain_temp(temp, unit):
    celsius = temp
    if unit == "F":
        celsius = fah_cel(temp)
    elif unit == "K":
        celsius = kel_cel(temp)

    if celsius < 10:
        return "❄️ <span style='color:#1e90ff'>It's very cold outside!</span>"
    elif 10 <= celsius < 25:
        return "🌀️ <span style='color:#2ecc71'>The weather is pleasant.</span>"
    elif 25 <= celsius < 35:
        return "β˜€οΈ <span style='color:#e67e22'>It's warm.</span>"
    else:
        return "πŸ”₯ <span style='color:#e74c3c'>It's hot!</span>"

def temperature_converter(choice, value):
    if choice == "Celsius β†’ Fahrenheit":
        result = f"{cel_fah(value):.2f} Β°F"
        explain = explain_temp(value, "C")
    elif choice == "Celsius β†’ Kelvin":
        result = f"{cel_kel(value):.2f} K"
        explain = explain_temp(value, "C")
    elif choice == "Fahrenheit β†’ Celsius":
        converted = fah_cel(value)
        result = f"{converted:.2f} Β°C"
        explain = explain_temp(value, "F")
    elif choice == "Fahrenheit β†’ Kelvin":
        converted = fah_kel(value)
        result = f"{converted:.2f} K"
        explain = explain_temp(value, "F")
    elif choice == "Kelvin β†’ Celsius":
        converted = kel_cel(value)
        result = f"{converted:.2f} Β°C"
        explain = explain_temp(value, "K")
    elif choice == "Kelvin β†’ Fahrenheit":
        converted = kel_fah(value)
        result = f"{converted:.2f} Β°F"
        explain = explain_temp(value, "K")
    else:
        return "⚠️ Invalid choice"
    
    return f"<b>βœ… Result:</b> {result}<br><br>{explain}"

choices = [
    "Celsius β†’ Fahrenheit",
    "Celsius β†’ Kelvin",
    "Fahrenheit β†’ Celsius",
    "Fahrenheit β†’ Kelvin",
    "Kelvin β†’ Celsius",
    "Kelvin β†’ Fahrenheit"
]

with gr.Blocks(css="""
    body {
        background: linear-gradient(135deg, #c3ec52, #0ba29d);
    }
    .title {
        font-size: 45px;
        font-weight: bold;
        text-align: center;
        margin-bottom: 10px;
    }
    .title span {
        background: linear-gradient(90deg, #00c6ff, #0072ff);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }
    .desc {
        color: #003566;
        text-align: center;
        font-size: 20px;
        font-style: italic;
        margin-bottom: 30px;
    }
    .card {
        background: white;
        border-radius: 20px;
        padding: 30px;
        box-shadow: 0px 8px 25px rgba(0,0,0,0.15);
        margin: auto;
        width: 60%;
        transition: transform 0.3s ease;
    }
    .card:hover {
        transform: scale(1.02);
    }
    .gr-textbox textarea {
        background-color: #f0f9ff;
        color: #222;
        font-weight: bold;
        font-size: 18px;
        border-radius: 10px;
    }
    .gr-button {
        background: linear-gradient(90deg, #36d1dc, #5b86e5) !important;
        color: white !important;
        font-size: 18px !important;
        font-weight: bold !important;
        border-radius: 14px !important;
        transition: 0.3s;
    }
    .gr-button:hover {
        background: linear-gradient(90deg, #5b86e5, #36d1dc) !important;
        transform: scale(1.08);
    }
    .footer {
        margin-top: 30px;
        text-align: center;
        color: #222;
        font-size: 14px;
        font-style: italic;
    }
""" ) as demo:
    
    gr.HTML("<div class='title'>🌑️ <span>Temperature Converter</span></div>")
    gr.HTML("<div class='desc'>Convert between Celsius, Fahrenheit, and Kelvin with live explanation ✨</div>")
    
    with gr.Column(elem_classes="card"):
        choice = gr.Dropdown(choices, label="🌐 Choose Conversion")
        value = gr.Number(label="✏️ Enter Temperature")
        result = gr.HTML(label="βœ… Result with Explanation")
        btn = gr.Button("πŸš€ Convert")
    
    btn.click(temperature_converter, inputs=[choice, value], outputs=result)
    
    gr.HTML("<div class='footer'>πŸ’‘ Developed with ❀️ using Python & Gradio</div>")

demo.launch()