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 "❄️ It's very cold outside!"
elif 10 <= celsius < 25:
return "🌤️ The weather is pleasant."
elif 25 <= celsius < 35:
return "☀️ It's warm."
else:
return "🔥 It's hot!"
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"✅ Result: {result}
{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("