File size: 1,972 Bytes
a0c24f2
 
 
9b601cc
 
 
 
 
 
 
 
 
 
 
a0c24f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a0ae12b
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
import gradio as gr

def convert_temperature(temp, unit):
    """
    Convert a temperature value between Celsius, Fahrenheit, and Kelvin.

    Args:
        temp (str or float): The temperature value to convert.
        unit (str): The unit of the input temperature ('Celsius', 'Fahrenheit', or 'Kelvin').

    Returns:
        tuple: A tuple containing three strings with the temperature in Celsius, Fahrenheit, and Kelvin,
               formatted to two decimal places. If input is invalid, returns an error message.
    """
    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:  # Kelvin
            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"

# Define the Gradio interface
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]
    )

# Launch the app with MCP setting
app.launch(mcp_server=True)