Spaces:
Sleeping
Sleeping
| 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() | |