|
import gradio as gr |
|
|
|
def convert_temperature(temp, unit): |
|
try: |
|
temp = float(temp) |
|
if unit == "Celsius": |
|
celsius = temp |
|
fahrenheit = (temp * 9/5) + 32 |
|
kelvin = temp + 273.15 |
|
elif unit == "Fahrenheit": |
|
celsius = (temp - 32) * 5/9 |
|
fahrenheit = temp |
|
kelvin = celsius + 273.15 |
|
else: |
|
celsius = temp - 273.15 |
|
fahrenheit = (celsius * 9/5) + 32 |
|
kelvin = temp |
|
|
|
return (f"{celsius:.2f} °C", f"{fahrenheit:.2f} °F", f"{kelvin:.2f} K") |
|
except ValueError: |
|
return "Please enter a valid number" |
|
|
|
|
|
with gr.Blocks() as app: |
|
gr.Markdown("# Temperature Converter") |
|
with gr.Row(): |
|
temp_input = gr.Textbox(label="Enter Temperature", placeholder="e.g., 25") |
|
unit_dropdown = gr.Dropdown( |
|
choices=["Celsius", "Fahrenheit", "Kelvin"], |
|
label="Select Unit", |
|
value="Celsius" |
|
) |
|
convert_button = gr.Button("Convert") |
|
|
|
with gr.Row(): |
|
celsius_output = gr.Textbox(label="Celsius") |
|
fahrenheit_output = gr.Textbox(label="Fahrenheit") |
|
kelvin_output = gr.Textbox(label="Kelvin") |
|
|
|
convert_button.click( |
|
fn=convert_temperature, |
|
inputs=[temp_input, unit_dropdown], |
|
outputs=[celsius_output, fahrenheit_output, kelvin_output] |
|
) |
|
|
|
|
|
app.launch(mcp=True) |
|
|